Search code examples
javareflections

Find the methods of a particular class matching an annotation


I have two annotations, we'll call them @Foo and @Bar. @Foo is a class (aka type)-level annotation and @Bar is a method-level annotation. My intent is for a given @Foo class, iterate over all the @Bar methods. What is the most appropriate way to do this? I'm using the Google Reflections API for Java, at the moment.

Thanks.


Solution

  • Wero's comment worked: "you can simply use fooClass.getDeclaredMethods() to enumerate the (declared) methods and method.getAnnotation(Bar.class) != null to filter for the bar methods"