Community
Participate
Working Groups
The following code does not compile using javac and compiles using eclipse: import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class Weird { public static final SomeAnnotParam[] myparams = {SomeAnnotParam.param1,SomeAnnotParam.param2}; @SomeAnnot(params=myparams) public void dothetrick(){} public static void main(String[] args)throws Exception { SomeAnnot sluck = Weird.class.getMethod("dothetrick", new Class[0]).getAnnotation(SomeAnnot.class); System.out.println(sluck.params()); } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface SomeAnnot { public SomeAnnotParam[] params(); } enum SomeAnnotParam { param1, param2, param3 }
If you write: @SomeAnnot(params= { SomeAnnotParam.param1, SomeAnnotParam.param2 } ) public void dothetrick(){} it works with javac.
(In reply to comment #1) > If you write: > @SomeAnnot(params= { SomeAnnotParam.param1, SomeAnnotParam.param2 } ) > public void dothetrick(){} > it works with javac. > That is not the point. If if does not compile using javac, it should not compile using eclipse too, right? But this code gives no error on eclipse.
The point is that the error message returned by javac is awkwards. X.java:11: incompatible types found : SomeAnnotParam[] required: SomeAnnotParam @SomeAnnot(params=myparams) ^ 1 error The types are not incompatibles. I believe the problem is the same than referring to a constant indirectly. For example, with: [import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class X { public static final SomeAnnotParam myparam = SomeAnnotParam.param1; @SomeAnnot(param=myparam) public void dothetrick(){} public static void main(String[] args)throws Exception { SomeAnnot sluck = X.class.getMethod("dothetrick", new Class[0]).getAnnotation(SomeAnnot.class); System.out.println(sluck.param()); } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface SomeAnnot { public SomeAnnotParam param(); } enum SomeAnnotParam { param1, param2, param3 }] you get: X.java:11: an enum annotation value must be an enum constant @SomeAnnot(param=myparam) ^ 1 error I'll investigate.
This is a case similar to bug 149751. This covers the array initializer case referenced through a name reference. Both should be fixed at the same time. Closing as dup of bug 149751. *** This bug has been marked as a duplicate of 149751 ***