A bit of a necessary story: with Java 6 (when the Mirror API was released) Java annotations could not be used on anything, but on the same top-level elements accessible through reflection. You could annotate classes, methods, and fields, but you could not annotate type arguments ( List<String> ) or local variables ( String value = ... ). Sun / Oracle engineers confirmed this limitation, and so-called "type annotations" appeared in Java 8.
Type annotations can be targeted at the type of something: the type of the local variable, the type of the component of the array, the type of the type of the variable, and even the type of return (the subsequent annotation is placed similarly, but differs from the old school annotations on the method!). Type annotations are created using the new @Target : ElementType # TYPE_USE value .
When people of Kotlin write
List<String?>
It really means
List<@Nullable String>
which can be read as: "list of Stable String elements".
Since the type itself is targeted, you expect to get annotations by looking at its original TypeMirror (don't worry about erased or captured TypeMirrors, they donβt have enough connection to the source code to save annotations). By the way, the Mirror API was reorganized, as a result, a new interface appeared AnnotatedConstruct and it is convenient to make TypeMirror its descendant.
Now the bad news: by the time Java 8 was released, support for checking type annotations was apparently not ready for production, so it was running. JSR has been rewritten to mean that "TypeMirror # getAnnotationMirrors" should return nothing.
Support bits that have been removed from the public API are still available through the Oracle-specific Tree API provider (only supported in javac). The Mirror type returned by Tree # getTypeMirror may contain the annotation as you expect. But since this is a mistake, you can only get annotations through a series of hacks, and ultimately it wonβt work all the time (for example, in the case of nested type arguments). See this question for some research in this direction.
The fix for this mess was combined in Java 9. I have not tested it yet, but it looks like TypeMirror # getAnnotationMirrors may finally work. Planning for backup fixes for the old version of Java is not planned.
user1643723
source share