Say we have an annotated entity
@XmlRootElement
public class Product {
String name;
@XmlElement(name="sss")
public void setName(String name) {
this.name = name;
}
}
The following is the sss code using the java Reflection API . Here "product" is an object of class Product
import java.lang.reflect.Method;
...
Method m = product.getClass().getMethod("setName",String.class);
XmlElement a = m.getAnnotation(XmlElement.class);
String nameValue = a.name();
System.out.println(nameValue);
If you need to get the @XmlElement attribute of an annotation from a private field , you can use something like this:
Field nameField = product.getClass().getDeclaredField("name");
nameField.setAccessible(true);
XmlElement a = nameField.getAnnotation(XmlElement.class);
String nameValue = a.name();
System.out.println(nameValue);
source
share