Bug 149751

Summary: Enum-valued annotation element handles non-enum constant badly
Product: [Eclipse Project] JDT Reporter: Justin T. Sampson <justin>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3 CC: jfaerman, philippe_mulet
Version: 3.1.1   
Target Milestone: 3.2.2   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Proposed fix
none
Regression tests
none
Proposed fix
none
Better patch
none
Updated regression tests
none
Patch for 3.2 maintenance
none
Regression tests for 3.2 maintenance branch
none
Regression tests for 3.2 maintenance branch
none
New patch for 3.2 maintenance
none
Updated regression tests none

Description Justin T. Sampson CLA 2006-07-05 17:39:46 EDT
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
Comment 1 Olivier Thomann CLA 2006-07-07 10:34:32 EDT
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.
Comment 2 Olivier Thomann CLA 2006-07-07 10:35:22 EDT
Created attachment 45921 [details]
Regression tests
Comment 3 Olivier Thomann CLA 2006-07-07 10:44:15 EDT
Philippe,

Could you please review it? I can adapt it for 3.2.1 if needed by changing back the error message.
Comment 4 Philipe Mulet CLA 2006-09-11 03:20:53 EDT
+1 for 3.2.2
Comment 5 Olivier Thomann CLA 2006-09-12 11:35:58 EDT
*** Bug 156891 has been marked as a duplicate of this bug. ***
Comment 6 Olivier Thomann CLA 2006-09-12 12:20:13 EDT
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.
Comment 7 Olivier Thomann CLA 2006-09-12 15:07:14 EDT
Created attachment 49974 [details]
Better patch

I believe this patch is more consistent with other error messages.
Comment 8 Olivier Thomann CLA 2006-09-12 15:07:45 EDT
Created attachment 49975 [details]
Updated regression tests
Comment 9 Philipe Mulet CLA 2006-09-12 15:15:43 EDT
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).
Comment 10 Olivier Thomann CLA 2006-09-12 16:30:02 EDT
This is the patch for 3.3 stream (HEAD).
I'll prepare another one for 3.2 maintenance.
Comment 11 Olivier Thomann CLA 2006-09-12 22:20:01 EDT
Fixed and released in HEAD.
Regression tests added in org.eclipse.jdt.core.tests.compiler.regression.AnnotationTest#test208/213.
Comment 12 Olivier Thomann CLA 2006-09-12 22:21:10 EDT
Released for 3.3M2.
Comment 13 Olivier Thomann CLA 2006-09-12 22:46:48 EDT
Created attachment 50002 [details]
Patch for 3.2 maintenance
Comment 14 Olivier Thomann CLA 2006-09-12 22:47:19 EDT
Created attachment 50003 [details]
Regression tests for 3.2 maintenance branch
Comment 15 Olivier Thomann CLA 2006-09-13 10:23:00 EDT
Created attachment 50036 [details]
Regression tests for 3.2 maintenance branch

Missed one test case in org.eclipse.jdt.core.tests.dom.ASTConverter15Test#test0209.
Comment 16 Olivier Thomann CLA 2006-09-13 10:23:36 EDT
Reopen to fix for 3.2.2.
Comment 17 Olivier Thomann CLA 2006-09-28 11:58:44 EDT
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.
Comment 18 Olivier Thomann CLA 2006-09-28 12:04:45 EDT
Created attachment 51098 [details]
New patch for 3.2 maintenance

Remove new error message to reuse only the existing ones.
Comment 19 Olivier Thomann CLA 2006-09-28 12:05:08 EDT
Created attachment 51099 [details]
Updated regression tests
Comment 20 Olivier Thomann CLA 2006-09-28 12:07:51 EDT
Released for 3.2.2.
Regression tests added in org.eclipse.jdt.core.tests.compiler.regression.AnnotationTest#test208/211
Comment 21 Frederic Fusier CLA 2006-10-29 07:15:31 EST
Verified for 3.3 M2 using build I200609220010.
Comment 22 Maxime Daniel CLA 2007-01-15 09:50:53 EST
Verified for 3.2.2 using build M20070112-1200.