Final strings in annotations applying to class members

Alex Buckley alex.buckley at oracle.com
Tue Jan 28 12:32:47 PST 2014


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