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

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

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: 12
    • Component/s: tools

      Description

      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
          )

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: