Unnecessary CHECKCAST converting to a supertype

Issue #262 resolved
Jesper Öqvist
created an issue

ExtendJ 8.1.0-4-ge296850 Java SE 7

In the bytecode generated for the following code, there are two unnecessary CHECKCAST to convert a String to Object:

import java.util.*;
public class An4 {
  public static void main(String[] args) {
    Map<String, String> score = new HashMap<String, String>();
    score.put("Pancake Batter".toLowerCase(), "very nice");
  }
}

Expected result: expected the following bytecode (no casts needed):

         0: new           #2                  // class java/util/HashMap
         3: dup
         4: invokespecial #3                  // Method java/util/HashMap."<init>":()V
         7: astore_1
         8: aload_1
         9: ldc           #4                  // String Pancake Batter
        11: invokevirtual #5                  // Method java/lang/String.toLowerCase:()Ljava/lang/String;
        14: ldc           #6                  // String very nice
        16: invokeinterface #7,  3            // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
        21: pop
        22: return

Actual result: two unnecessary casts are generated (at pc 14 and 19):

         0: new           #20                 // class java/util/HashMap
         3: dup
         4: invokespecial #21                 // Method java/util/HashMap."<init>":()V
         7: astore_1
         8: aload_1
         9: ldc           #23                 // String Pancake Batter
        11: invokevirtual #29                 // Method java/lang/String.toLowerCase:()Ljava/lang/String;
        14: checkcast     #4                  // class java/lang/Object
        17: ldc           #31                 // String very nice
        19: checkcast     #4                  // class java/lang/Object
        22: invokeinterface #37,  3           // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
        27: pop
        28: return

Comments (2)

  1. Log in to comment