Using annotation with @Target(TYPE_USE) on type declaration

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


I've an annotation type whose only supported target is 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:

    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.

" @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,



More information about the compiler-dev mailing list