1 | initial version |
To obtain the value of an annotation by using reflection, follow these steps:
Class.forName(String)
method or the .class
syntax. For example: Class<MyAnnotation> annotationClass = MyAnnotation.class;
Class<MyClass> targetClass = MyClass.class;
MyAnnotation annotation = targetClass.getAnnotation(MyAnnotation.class);
String value = annotation.value();
Here, value()
is the method defined in the MyAnnotation
interface. Replace value()
with the name of the appropriate method if the annotation has other attributes.
Note: If the target element is a method or a field, you may need to use additional reflection methods to get the annotations on the parameters or the generic type of the field or method.