Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8162815

unnecessary object creation in reflection

XMLWordPrintable

    • b31
    • 9
    • b131
    • generic
    • generic
    • Not verified

      The fix for issue JDK-5043030 is incorrect. The generated code for reflection still allocates objects. It even makes the situation worse in cases when primitive wrapper classes' valueOf() allocates new instance. In such case the code allocates two objects. The fix for JDK-5043030 replaced invocation of constructor method with invocation of valueOf(), but it failed to remove actual object allocation. Take a look at the current bytecode of 'invoke' method below:

      {
        public java.lang.Object invoke(java.lang.Object, java.lang.Object[]) throws java.lang.reflect.InvocationTargetException;
          descriptor: (Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
          flags: ACC_PUBLIC
          Code:
            stack=7, locals=3, args_size=3
               0: new #99 // class java/lang/Long
               3: dup
               4: aload_1
               5: ifnonnull 16
               8: new #18 // class java/lang/NullPointerException
              11: dup
              12: invokespecial #26 // Method java/lang/NullPointerException."<init>":()V
              15: athrow
              16: aload_1
              17: checkcast #6 // class java/lang/management/ThreadInfo
              20: aload_2
              21: ifnull 40
              24: aload_2
              25: arraylength
              26: sipush 0
              29: if_icmpeq 40
              32: new #20 // class java/lang/IllegalArgumentException
              35: dup
              36: invokespecial #27 // Method java/lang/IllegalArgumentException."<init>":()V
              39: athrow
              40: invokevirtual #10 // Method java/lang/management/ThreadInfo.getBlockedCount:()J
              43: invokestatic #102 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
              46: areturn
              47: invokespecial #40 // Method java/lang/Object.toString:()Ljava/lang/String;
              50: new #20 // class java/lang/IllegalArgumentException
              53: dup_x1
              54: swap
              55: invokespecial #30 // Method java/lang/IllegalArgumentException."<init>":(Ljava/lang/String;)V
              58: athrow
              59: new #22 // class java/lang/reflect/InvocationTargetException
              62: dup_x1
              63: swap
              64: invokespecial #33 // Method java/lang/reflect/InvocationTargetException."<init>":(Ljava/lang/Throwable;)V
              67: athrow
            Exception table:
               from to target type
                  16 40 47 Class java/lang/ClassCastException
                  16 40 47 Class java/lang/NullPointerException
                  40 43 59 Class java/lang/Throwable
          Exceptions:
            throws java.lang.reflect.InvocationTargetException


      The first bytecode instruction allocates instance of java.lang.Long, which is not used and return value of java.lang.Long.valueOf() is used (at offset 43).

            redestad Claes Redestad
            thurka Tomáš Hůrka
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: