Unfortunately, this part of the Reflection API is horrible. The base types do not have the necessary query methods and there is no Visitor API or such alike. So any code trying to do a full introspection has no choice but to perform lots of instanceof
checks, to handle all possible cases.
If you know beforehand that the method’s type should be a parameterized type and you only want to check the annotations of its first type argument, you can do it a bit simpler, ignoring all other possible cases:
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.Method;
import java.util.*;
public class Main {
public static void main(String[] args) throws NoSuchMethodException {
Method m = Foo.class.getDeclaredMethod("bar", List.class);
var at = m.getAnnotatedParameterTypes()[0];
var ata = ((AnnotatedParameterizedType)at).getAnnotatedActualTypeArguments()[0];
// get all annotations
for(var a: ata.getAnnotations()) {
System.out.println(a);
}
// or check the presence of a known annotation
System.out.println(ata.getAnnotation(Important.class) != null);
}
class Foo {
void bar(List<@Important String> b) {}
}
}
Demo on Ideone
@Important()
true