Is a TYPE_USE annotation stored as such in a class file?

Alex Buckley alex.buckley at oracle.com
Thu Feb 16 22:41:33 UTC 2023


Correct. The `@A` in `@A class B {}` is a declaration annotation, hence 
appears in a RuntimeVisibleAnnotations attribute in B.class.

The next line in 9.7.4 is:

   A type annotation is an annotation that applies to a type
   (or any part of a type), and whose annotation interface is
   applicable in type contexts.

Since this `@A` doesn't apply to a type, it's not a type annotation, 
hence there is no RuntimeVisibleTypeAnnotations attribute.

The ability of a TYPE_USE annotation interface (`A`) to beget 
declaration annotations in certain locations is a feature rooted in the 
broader context of JSR 308, involving a desire to take older annotation 
interfaces (e.g., with target ElementType.TYPE) and write their 
annotations in both old (declaration) contexts and new (type) contexts.

Alex

On 2/16/2023 1:59 PM, Liam Miller-Cushon wrote:
> I think this is deliberate and covered by 
> https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.7.4 
> <https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.7.4>
> 
>     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
> 
> 
>   But I'm not completely sure, and welcome corrections to my reading of 
> the spec :)
> 
> On Thu, Feb 16, 2023 at 1:31 PM Laird Nelson <ljnelson at gmail.com 
> <mailto:ljnelson at gmail.com>> wrote:
> 
>     Another TYPE_USE question. Consider:
> 
>     @Retention(RetentionPolicy.RUNTIME)
>     @Target({ ElementType.TYPE_USE })
>     @interface A {}
> 
>     @A class B{}
> 
>     I am surprised to see @A in the returned array from
>     B.class.getAnnotations() since it is not an element annotation.
> 
>     Furthermore, I do not see the string "RuntimeVisibleTypeAnnotations"
>     in the class file for B (which is why I'm writing to the
>     compiler-dev list). I am no VM/bytecode person but I would expect to
>     see that if a runtime-visible type use annotation is being recorded,
>     yes? Although I do not see a place in the target_info structure that
>     would to my naive eyes be suitable for this particular usage.
> 
>     Is @A's usage here recorded in the wrong place as the wrong sort of
>     thing?  This has a whiff of
>     https://bugs.openjdk.org/browse/JDK-8030751
>     <https://bugs.openjdk.org/browse/JDK-8030751> to it but I wanted to
>     confirm.
> 
>     Thanks,
>     Laird
> 


More information about the compiler-dev mailing list