[External] : Re: Is a TYPE_USE annotation stored as such in a class file?

Alex Buckley alex.buckley at oracle.com
Thu Feb 16 23:01:46 UTC 2023

On 2/16/2023 2:46 PM, Laird Nelson wrote:
> On Thu, Feb 16, 2023 at 2:43 PM Alex Buckley <alex.buckley at oracle.com 
> <mailto:alex.buckley at oracle.com>> wrote:
>     The annotation interface A has ElementType.TYPE_USE === applicable in
>     type contexts (
> If you have the time and for my own learning: which of the 17 type 
> contexts in 4.11 
> (https://docs.oracle.com/javase/specs/jls/se19/html/jls-4.html#jls-4.11 
> <https://docs.oracle.com/javase/specs/jls/se19/html/jls-4.html#jls-4.11>) is:
> class B
> ...such that @A is applicable in it?

The declaration `class B {}` is not a type context.

Here is the annotation `@A` being applied in some type contexts:

   class B extends    @A Foo {}
   class B implements @A Bar {}
   class B<@A T extends @A Number> {}

These applications of `@A` are legal -- they are _type annotations_ -- 
because they're applying to types (a.k.a. type uses -- of Foo, Bar, T, 
Number) and the annotation interface `A` is applicable in type contexts.

In contrast, you are applying `@A` to a declaration, `class B {}`. Your 
application of `@A` is legal - it is a _declaration annotation_ -- because:

   A declaration annotation is ... an annotation that applies to a class,
   interface, or type parameter declaration, and whose annotation
   interface is applicable in type contexts (§4.11).

That is, `A` being applicable in type contexts means `@A` can be applied 
very, very widely.


More information about the compiler-dev mailing list