JDK-8023682 : Incorrect attributes emitted for anonymous class declaration
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2013-08-23
  • Updated: 2014-03-14
  • Resolved: 2013-10-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b115Fixed
Related Reports
Relates :  
Relates :  
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.
Comments
Verified with b115.
08-11-2013

Considered a blocker by development. Test coverage for annotations is good, SQE is OK to keep this on the critical watch list.
23-10-2013

The case with TYPE_USE target types, where the annotations are on the type parameters instead of the raw type (ie new Foo<@X String>() {};) will be handled as a separate issue: JDK-8027182
23-10-2013

ILW = MHH --> P2
23-10-2013

Upon closer inspection, this is an unrelated issue.
15-10-2013

Root cause of this is JDK-8026063
15-10-2013

Possibly addressed by fix for 8008762
02-10-2013