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

Incorrect attributes emitted for anonymous class declaration

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2
    • 8
    • 8
    • tools
    • b115
    • Verified

    Description

      1. Type annotation in 'new' expression for anonymous class

      Consider this code:

        @Retention(RetentionPolicy.RUNTIME)
        @Target(ElementType.TYPE_USE)
        @interface X {}
        class C { void m() { new @X Foo() {}; } }

      The @X annotation is stored as a target_type=CLASS_EXTENDS structure in the RuntimeVisibleTypeAnnotations attribute of Foo$1.class. That's fine. But, the @X annotation is not stored as a target_type=NEW structure in the RuntimeVisibleTypeAnnotations attribute of m's method_info in C.class. That's an oversight by javac.

      2. Declaration annotation for anonymous class in 'new' expression

      Take the code above and change the annotation type's target to be TYPE:

        @Retention(RetentionPolicy.RUNTIME)
        @Target(ElementType.TYPE)
        @interface X {}
        class C { void m() { new @X Foo() {}; } }

      javac accepts this code, and emits a RuntimeVisibleAnnotations attribute for @X in Foo$1.class - but the code is illegal. The annotation type X is applicable in one declaration context - type declarations - yet the 'new' expression is a type context. It is a compile-time error if an annotation of type T decorates a type in a type context, but T is not applicable to type contexts.

      Attachments

        Issue Links

          Activity

            People

              emc Eric Mccorkle
              abuckley Alex Buckley
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: