Community
Participate
Working Groups
Version: 3.1.1 Build id: M20050929-0840 The code below compiles, but fails at runtime with the exception given following the code. The Sun JDK instead fails to compile the same code, giving the error at the bottom. Ideally the annotation would simply be compiled with the enum value TWO instead of FOO, but the Java Language Specification (section 9.7) does say that only enum constants are allowed here. == MyAnnotation.java == import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.RUNTIME; @Retention(RUNTIME) public @interface MyAnnotation { public MyEnum value(); } == MyEnum.java == public enum MyEnum { ONE, TWO, THREE } == MyClass.java == @MyAnnotation(MyMain.FOO) public class MyClass { } == MyMain.java == public class MyMain { public static final MyEnum FOO = MyEnum.TWO; public static void main(String[] args) { MyAnnotation annotation = MyClass.class.getAnnotation(MyAnnotation.class); System.out.println(annotation.value().toString()); } } == java MyMain (Eclipse 3.1.1) == Exception in thread "main" java.lang.EnumConstantNotPresentException: MyEnum.FOO at sun.reflect.annotation.EnumConstantNotPresentExceptionProxy.generateException(EnumConstantNotPresentExceptionProxy.java:28) at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:56) at $Proxy1.value(Unknown Source) at MyMain.main(MyMain.java:6) == javac *.java (Sun JDK 1.5.0_05) == MyClass.java:1: an enum annotation value must be an enum constant @MyAnnotation(MyMain.FOO) public class MyClass { ^ 1 error
Created attachment 45920 [details] Proposed fix This fixes the following problem + improved the error message to include (enum) + fix the bug for the array initializer that can contain the null literal.
Created attachment 45921 [details] Regression tests
Philippe, Could you please review it? I can adapt it for 3.2.1 if needed by changing back the error message.
+1 for 3.2.2
*** Bug 156891 has been marked as a duplicate of this bug. ***
Created attachment 49948 [details] Proposed fix More complete fix. This also covers the name reference that refers to an array initializer with a better error message. Philippe, Let me know what you think. I can take care of releasing it.
Created attachment 49974 [details] Better patch I believe this patch is more consistent with other error messages.
Created attachment 49975 [details] Updated regression tests
Fix looks good, however for 3.2 maintenance no new pb ID can be introduced. As to make it internal, or reuse an existing error msg (suboptimal, but no API change).
This is the patch for 3.3 stream (HEAD). I'll prepare another one for 3.2 maintenance.
Fixed and released in HEAD. Regression tests added in org.eclipse.jdt.core.tests.compiler.regression.AnnotationTest#test208/213.
Released for 3.3M2.
Created attachment 50002 [details] Patch for 3.2 maintenance
Created attachment 50003 [details] Regression tests for 3.2 maintenance branch
Created attachment 50036 [details] Regression tests for 3.2 maintenance branch Missed one test case in org.eclipse.jdt.core.tests.dom.ASTConverter15Test#test0209.
Reopen to fix for 3.2.2.
In 3.2.2 the error message is not optimal since we have to reuse existing error messages. In HEAD the error message is much better.
Created attachment 51098 [details] New patch for 3.2 maintenance Remove new error message to reuse only the existing ones.
Created attachment 51099 [details] Updated regression tests
Released for 3.2.2. Regression tests added in org.eclipse.jdt.core.tests.compiler.regression.AnnotationTest#test208/211
Verified for 3.3 M2 using build I200609220010.
Verified for 3.2.2 using build M20070112-1200.