JDK-8054448 : (ann) Cannot reference field of inner class in an anonymous class
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u11
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2014-08-02
  • Updated: 2015-06-04
  • Resolved: 2014-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 JDK 9
8u40Fixed 9 b37Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux cnl1478 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Annotation values on an inner class in an anonymous class cannot reference attributes of that inner class.

REGRESSION.  Last worked in version 7u65

ADDITIONAL REGRESSION INFORMATION: 
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Invoke javac with the attached source file as an argument

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should compile just fine.
ACTUAL -
Compilation fails with one error.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
.../J8Issue.java:13: error: cannot find symbol
            @Annotation(AnonymousClass.ID)
                                      ^
  symbol:   variable ID
  location: class <anonymous OuterBaseClass>.AnonymousClass
1 error

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class J8Issue
{
    public static void main(String[] args) {
        Object withAttribute = new OuterBaseClass()
        {
            private void method() {
                // This compiles OK in Java 7 update 50, 60 and 65, and Java 8 update 5 and 11
                System.out.println(AnonymousClass.ID);
            }

            // This compiles OK in Java 7 update 50, 60 and 65
            // This WONT compile in Java 8 update 5 and 11
            @Annotation(AnonymousClass.ID)
            abstract class AnonymousClass
            {
                static final String    ID    = "B";
            }
        };
    }
}

class OuterBaseClass
{
}

@interface Annotation
{
    String value();
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Copy the value of the referenced field and put it at the Annotation's value. That requires duplication, and is therefore not preferable.