Consider:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE_USE })
@interface A {}
@A
final class B {}
This compiles.
How, either using java.lang.reflect.*
constructs or javax.lang.model.*
constructs, do I read @A
?
A commenter didn't understand what I meant by "read". Here is what I mean.
Using reflection as an example, cls.getAnnotations()
will, unexpectedly, return a collection featuring @A
(incorrectly) as a declaration annotation, because it returns declaration annotations (@A
is not listed as being able to be applied to ElementType.TYPE
, but that's where it "shows up" here). Type annotations are returned by AnnotatedType
implementations. But I see no way to get an AnnotatedType
for a Class
.
That's fine; sometimes reflection doesn't give you everything (I've been told elsewhere). But using javax.lang.model.*
classes, I can't get this annotation either. I would expect it to appear in:
elements.getTypeElement("B").asType().getAnnotationMirrors();
…but that List
is empty. (This might be due to https://bugs.openjdk.org/browse/JDK-8225377.)
In case it matters, when looking at the actual .class
file, I see no occurrence of the string RuntimeVisibleTypeAnnotations
, suggesting that @A
is not, in fact, actually recorded or retained as a type use annotation in the .class
file. I also see RuntimeVisibleAnnotations
and @A
appears after it, so it would seem that this annotation is incorrectly recorded as a declaration/element annotation. I will probably file another JDK bug.
I'm also initiating a discussion on compiler-dev: https://mail.openjdk.org/pipermail/compiler-dev/2023-February/022184.html