Consider this code:
public example(String s, int i, @Foo Bar bar) {
/* ... */
}
I want to check if the method has an annotation @Foo
getParameterAnnotations
returns an array with the length equals to the amount of method parameters. Each element in that array contains an array of annotations on that parameter.
So getParameterAnnotations()[2][0]
contains the first ([0]
) annotation of the third ([2]
) parameter.
If you only need to check if at least one parameter contains an annotation of a specific type, the method could look like this:
private boolean isAnyParameterAnnotated(Method method, Class> annotationType) {
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
for (Annotation[] annotations : paramAnnotations) {
for (Annotation an : annotations) {
if(an.annotationType().equals(annotationType)) {
return true;
}
}
}
return false;
}