Create Entity Class
Create entities package and create new class named Student as below:
package entities;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
public class Student {
private String id;
private String name;
private int age;
@XmlElement(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Deprecated
public void say() {
System.out.println("Hello World");
}
}
Run Application
package reflection;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import entities.Student;
public class Main {
private static void displayAllAnnotations(Student student) {
Class studentClass = student.getClass();
for (Method method : studentClass.getDeclaredMethods()) {
System.out.println("Method Name: " + method.getName());
Annotation[] annotations = method.getAnnotations();
System.out.println("Count Annotations: " + annotations.length);
if (annotations.length > 0) {
for (Annotation annotation : annotations) {
System.out.println("\tAnnotation Name: " + annotation.annotationType().getName());
System.out.println("\tAnnotation Simple Name: " + annotation.annotationType().getSimpleName());
System.out.println("\tAnnotation Modifier: " + Modifier.toString(annotation.annotationType().getModifiers()));
}
}
System.out.println("========================================================================");
}
}
public static void main(String[] args) {
Student student = new Student();
displayAllAnnotations(student);
}
}
Output
Method Name: getName
Count Annotations: 1
Annotation Name: javax.xml.bind.annotation.XmlElement
Annotation Simple Name: XmlElement
Annotation Modifier: public abstract interface
========================================================================
Method Name: getId
Count Annotations: 1
Annotation Name: javax.xml.bind.annotation.XmlElement
Annotation Simple Name: XmlElement
Annotation Modifier: public abstract interface
========================================================================
Method Name: setName
Count Annotations: 0
========================================================================
Method Name: setId
Count Annotations: 0
========================================================================
Method Name: getAge
Count Annotations: 1
Annotation Name: javax.xml.bind.annotation.XmlElement
Annotation Simple Name: XmlElement
Annotation Modifier: public abstract interface
========================================================================
Method Name: say
Count Annotations: 1
Annotation Name: java.lang.Deprecated
Annotation Simple Name: Deprecated
Annotation Modifier: public abstract interface
========================================================================
Method Name: setAge
Count Annotations: 0
========================================================================