Java annotations with runtime retention (that is those you can get through reflection) are only accessible directly from the element on which the annotation is present. So you are going to have to loop through the methods and check for the annotations like you have in your example.
If you need to do a lot of annotation processing at the class level I suggest you create a utility class that does that:
public class AnnotationUtils {
public static int countMethodsWithAnnotation(Class<?> klass,
Class<?> annotation) {
int count = 0;
for (Method m : klass.getMethods()) {
if (m.isAnnotationPresent(annotation)) {
count++;
}
}
return count;
}
// Other methods for custom annotation processing
}
You could then use the utility class to get the information you want in one method call as you require in the rest of your code:
int count = AnnotationUtils.countMethodsWithAnnotation(ParseTree.class,
Analyze.class);