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

Incorrect handling of type annotations on enclosing instances of anonymous class supertypes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • 12
    • None
    • tools

      The handling of type annotations on the enclosing instance type of anonymous class supertypes is incorrect. Consider this example:

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

      class X {

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

          class Inner {}

          private void f() {
              class Local extends @TA(3) X.@TA(4) Inner {}
              new Local();
          }

          private void g() {
              new @TA(3) X.@TA(4) Inner() {};
          }
      }
      ```

      Of the type annotations on the local class' supertype, @TA(4) is on the inner type 'Inner' (with location=[INNER_TYPE]) and @TA(3) is on the enclosing instance X.

      $ javap -v -p X\$1Local
      ...
      class X$1Local extends X$Inner
      ...
      RuntimeVisibleTypeAnnotations:
        0: #17(#18=I#19): CLASS_EXTENDS, type_index=65535, location=[INNER_TYPE]
          X$TA(
            value=4
          )
        1: #17(#18=I#20): CLASS_EXTENDS, type_index=65535
          X$TA(
            value=3
          )

      On the anonymous class' synthetic declaration, the annotations on the supertype are incorrectly both placed on the inner type 'Inner' (with location=[INNER_TYPE]). @TA(3) should be on the enclosing instance instead with an empty `location=[]`, the same as for the local class.

      $ javap -v -p X\$1
      ...
      class X$1 extends X$Inner
      ...
      RuntimeVisibleTypeAnnotations:
        0: #17(#18=I#19): CLASS_EXTENDS, type_index=65535, location=[INNER_TYPE]
          X$TA(
            value=4
          )
        1: #17(#18=I#20): CLASS_EXTENDS, type_index=65535, location=[INNER_TYPE]
          X$TA(
            value=3
          )

            cushon Liam Miller-Cushon
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: