@Target annotation with empty set
Alex Buckley
alex.buckley at oracle.com
Mon Mar 11 14:16:13 PDT 2013
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."
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