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

Type annotations on anonymous classes in initializer blocks not written to class file

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 21
    • 8, 9, 10, 11
    • tools
    • b10

      In the following example RuntimeVisibleTypeAnnotations attributes are emitted for the annotations on the `new Local()` expressions, but not for the `new Object() {}` expressions.

      If those same expression appear inside instance or static methods (rather than instance or static initializer blocks) then 'NEW' type annotations are emitted for both.

      I think this is related to JDK-8136419. Note that the logic added by the fix for that bug [1] is not used by the code path that propagates type annotations from the supertype of an anonymous class instantation expression to the desugared instantiation expression of the anonymous class' synthetic declaration [2].

      [1] http://hg.openjdk.java.net/jdk/jdk/file/0d28f82ecac6/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java#l388
      [2] http://hg.openjdk.java.net/jdk/jdk/file/0d28f82ecac6/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java#l1311

      ```
      import java.lang.annotation.*;

      class X {

          @Retention(RetentionPolicy.RUNTIME)
          @Target(ElementType.TYPE_USE)
          public @interface TA {
              int value() default 0;
          }

          {
            class Local {}
            new @TA(1) Local();
            new @TA(0) Object() {};
          }

          static {
            class Local {}
            new @TA(3) Local();
            new @TA(2) Object() {};
          }
      }
      ```

      javap -v -p X
      ...
        X();
          descriptor: ()V
          flags: (0x0000)
          Code:
            stack=3, locals=1, args_size=1
               0: aload_0
               1: invokespecial #1 // Method java/lang/Object."<init>":()V
               4: new #2 // class X$1Local
               7: dup
               8: aload_0
               9: invokespecial #3 // Method X$1Local."<init>":(LX;)V
              12: pop
              13: new #4 // class X$1
              16: dup
              17: aload_0
              18: invokespecial #5 // Method X$1."<init>":(LX;)V
              21: pop
              22: return
            LineNumberTable:
              line 3: 0
              line 13: 4
              line 14: 13
              line 15: 22
            RuntimeVisibleTypeAnnotations:
              0: #21(#22=I#23): NEW, offset=4, location=[INNER_TYPE]
                X$TA(
                  value=1
                )

        static {};
          descriptor: ()V
          flags: (0x0008) ACC_STATIC
          Code:
            stack=2, locals=0, args_size=0
               0: new #6 // class X$2Local
               3: dup
               4: invokespecial #7 // Method X$2Local."<init>":()V
               7: pop
               8: new #8 // class X$2
              11: dup
              12: invokespecial #9 // Method X$2."<init>":()V
              15: pop
              16: return
            LineNumberTable:
              line 19: 0
              line 20: 8
              line 21: 16
            RuntimeVisibleTypeAnnotations:
              0: #21(#22=I#25): NEW, offset=0
                X$TA(
                  value=3
                )

            vromero Vicente Arturo Romero Zaldivar
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: