Retrieve Java Annotation Attribute

  1. Obtain Method instance.
  2. Obtain annotation.
  3. Obtain annotation attribute value.

Something like:

Method m = getClass().getMethod("myMethod");
MyAnnotation a = m.getAnnotation(MyAnnotation.class);
MyValueType value1 = a.attribute1();

You’ll need to catch / handle the appropriate exceptions, of course. The above assumes you are indeed retrieving method from the current class (replace getClass() with Class.forName() otherwise) and the method in question is public (use getDeclaredMethods() if that’s not the case)

Leave a Comment