Final strings in annotations applying to class members
Remi Forax
forax at univ-mlv.fr
Wed Jan 29 11:08:19 PST 2014
On 01/29/2014 07:59 PM, Alex Buckley wrote:
> I admit that the definition of "constant variable" in JLS 4.12.4 is
> always worth double-checking: "A variable of primitive type or type
> String, that is final and initialized with a compile-time constant
> expression (§15.28), is called a constant variable."
>
> The point being that a compiler can statically determine the value of
> the constant variable CONST_STR for the purpose of emitting an
> annotation attribute in a class file - even though the variable
> declaration is not 'static'.
>
> Alex
>
yes.
at least this is how it works now.
Rémi
> On 1/29/2014 9:07 AM, Jonathan Gibbons wrote:
>> Just checking ...
>>
>> CONSTR_STR is declared "final String", not "static final String" ...
>>
>> -- Jon
>>
>>
>> On 01/29/2014 05:34 AM, Joel Borggren-Franck wrote:
>>> 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