@Target annotation with empty set
Alex Buckley
alex.buckley at oracle.com
Mon Mar 11 16:58:58 PDT 2013
See the javadoc for java.lang.annotation.Target. No @Target is not the same as an empty @Target. Since 2004.
JSR 308 tweaks the meaning of "No @Target", but that can be ignored here.
----- Original Message -----
From: steve.sides at oracle.com
To: enhanced-metadata-spec-discuss at openjdk.java.net
Sent: Monday, March 11, 2013 3:54:02 PM GMT -08:00 US/Canada Pacific
Subject: Re: @Target annotation with empty set
On 3/11/2013 2:16 PM, Alex Buckley wrote:
> On 3/11/2013 1:43 PM, Jonathan Gibbons wrote:
>> Is it ever reasonable to have a @Target annotation with an empty set?
>>
>> -- Jon
>>
>>
>> import java.lang.annotation.*;
>>
>> @Target({})
>> @interface TC { T[] value(); }
>>
>> @Repeatable(TC.class)
>> @interface T { }
>>
>> @T @T
>> class C { }
>
> Reasonable? No. Legal? Yes.
>
> I allude to something like this case in a note in the rep.annos spec:
>
> "If Foo has no @Target meta-annotation but FooContainer has an @Target
> meta-annotation, then @Foo may only be repeated on program elements
> where @FooContainer may appear."
So, is no @Target the same as and empty @Target?
-steve
>
> That's fine, and @T should not be able to repeat anywhere.
>
> To be clear, TC is a valid containing annotation type for T, because:
>
> "T is applicable to at least the same kinds of program element as TC;
> specifically, If the kinds of program element where T is applicable
> are represented by the set m1, and the kinds of program element where
> TC is applicable are represented by the set m2, then each kind in m2
> must occur in m1 ..."
>
> m2 is the empty set so the clause holds vacuously. @T cannot repeat
> because:
>
> "It is a compile-time error if a declaration is annotated with more
> than one annotation of a given annotation type, unless the annotation
> type is repeatable (§9.6), and the annotated declaration is a valid
> target (§9.6.3.1) of both the repeatable annotation type and the
> repeatable annotation type's containing annotation type."
>
> Alex
More information about the enhanced-metadata-spec-discuss
mailing list