JDK-6857918 : javac cannot handle fields in @interface: will generate "is missing ".
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u14
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-07-07
  • Updated: 2013-05-30
  • Resolved: 2013-05-30
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
8Resolved
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
javac on apple box: 1.6.0_13
javac in windows box: 1.6.0_14 (latest at time of writing)

ADDITIONAL OS VERSION INFORMATION :
Tested on both mac os x with apple VM and windows with sun VM.
Apple box: Darwin hostname.local 9.7.0 Darwin Kernel Version 9.7.0: Tue Mar 31 22:52:17 PDT 2009; root:xnu-1228.12.14~1/RELEASE_I386 i386
Win box: Microsoft Windows XP [Version 5.1.2600]


A DESCRIPTION OF THE PROBLEM :
//Stuff the following snippet in a file and compile it:

//This code has been carefully distilled to be the simplest possible thing that causes
//the problem.

//Compiling this on javac 1.6.0_13 causes the following problem:
// annotation A is missing <clinit>

@interface A {
        //Compile time constants will NOT trigger the error, but 'null' is not a CTC.
        String B = null;
}

class C {
        private @A int d;
}


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the code given in the description. Javac prints an error even though the code is valid.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should compile normally. To be specific, there *IS* a <clinit>, which you can see by compiling the @interface separately, and using javap to see that there's a static block that initializes the constant. Therefore, javac is erroneously reporting that the @interface has no <clinit>.

ACTUAL -
See "Error Messages".

ERROR MESSAGES/STACK TRACES THAT OCCUR :
foo.java:13: annotation A is missing <clinit>
	private @A int d;
	        ^
1 error



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
@interface A {
        //Compile time constants will NOT trigger the error, but 'null' is not a CTC.
        String B = null;
}

class C {
        private @A int d;
}


---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Move the constants into another class.
Copying comments from http://bugs.openjdk.java.net/show_bug.cgi?id=100199

Description From Rideg S��ndor 2011-08-06 16:02:43 PDT

TESTED VERSIONS: 1.7.0, 1.6.0_26

DESCRIPTION:
try to complie the following code:

@interface MyAnnotation {
        Object OBJECT = new Object();        
}

@MyAnnotation()
public class MainClass {}

RESULT:
>javac MainClass.java 
 MainClass.java:6: annotation MyAnnotation is missing <clinit>
 @MyAnnotation()
 ^
 1 error

NOTE: I've found the same bugreport at
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6857918

Comment #1 From Rideg S��ndor 2011-08-06 16:27:34 PDT

Created an attachment (id=226) [details]
diff against jdk7-b147

With this fix <clinit> wont be treaten as a member when validating annotation
in
com.sun.tools.javac.comp.Check.validateAnnotation().

Comment #2 From Tim Bell 2012-07-09 19:53:16 PDT

Closing.  This is SUNBUG 6857918

Comments
SUGGESTED FIX See attached patch from OpenJDK bugzilla.
23-07-2012