Using annotation with @Target(TYPE_USE) on type declaration

Gunnar Morling gunnar at hibernate.org
Tue Jun 27 14:54:45 UTC 2017


Hi,

I've an annotation type whose only supported target is ElementType.TYPE_USE:

    @Target(ElementType.TYPE_USE)
    public @interface MyAnno {}

To my surprise I can use this annotation on the definition of a class,
whereas I'd have expected a compilation error:

    @MyAnno
    public class Foo {}

I can't seem to find the JLS wording that'd suggest that TYPE_USE
allows usage of the annotation on a type definition.

"9.6.4.1 @Target" [1] mentions eight declaration contexts (which don't
seem to apply here as per the supported element type) and 16 type
contexts which are described in "4.11. Where Types Are Used" [2]. They
are represented by TYPE_USE, but none of these seem to match the the
declaration of type Foo.

Why is it that specifying @MyAnno on the declaration of Foo does not
raise a compiler error?

Thanks for any pointers,

--Gunnar

[1] https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.6.4.1
[2] https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.11


More information about the compiler-dev mailing list