Final strings in annotations applying to class members

Joel Borggren-Franck joel.franck at oracle.com
Wed Jan 29 05:34:25 PST 2014


John, Alex,

Thanks for the bug report. I filed:

https://bugs.openjdk.java.net/browse/JDK-8033132

cheers
/Joel

On 2014-01-28, Alex Buckley wrote:
> Looks like a javac bug. The element value CONST_STR is indeed a
> constant expression (gives the simple name of a constant variable),
> and the type of CONST_STR is a String, so JLS 9.7.1 says all your
> annotations are legal.
> 
> Alex
> 
> On 1/28/2014 8:14 AM, John Spicer wrote:
> >Javac 8 accepts:
> >
> >@interface Annot {
> >     String str();
> >}
> >
> >class Test {
> >     final String CONST_STR="something";
> >
> >     @Annot( str = CONST_STR )
> >     class Foo {}
> >
> >     @Annot( str = CONST_STR )
> >     void f(){}
> >}
> >
> >It rejects only the second annotation use in the example below (complaining that CONST_STR cannot be referenced from a static context).
> >
> >Given that CONST_STR is a constant expression, I could see allowing both uses.   Is there a rationale for allowing only the use on the class?
> >
> >Thanks,
> >
> >John.
> >
> >@interface Annot {
> >     String str();
> >}
> >
> >class Test {
> >     final String CONST_STR="something";
> >
> >     @Annot( str = CONST_STR )
> >     static class Foo {}
> >
> >     @Annot( str = CONST_STR )
> >     static void f(){}
> >}
> >
> >


More information about the compiler-dev mailing list