Since there's no type literals in Java,
TypeToken
Type
List<String>
new TypeToken<List<String>>(){}.getType()
AnnotatedType
List<@NonNull String>
@NonNull List<@NonNull String>
AnnotatedType
TypeToken
Just create a type token - or any anonymous subclass with annotated type parameters - with the annotations and then use reflection to extract the annotated types.
Note that this only works for annotations with runtime retention.
Using the fullType()
method from the linked answer:
List<?> token = new ArrayList<@NonNull String>() {};
fullType("", token.getClass().getAnnotatedSuperclass());
prints
java.util.ArrayList
<
@org.checkerframework.checker.nullness.qual.NonNull()
java.lang.String
>