How do I get all annotations?

To obtains all annotations for classes, methods, constructors, or fields we use the getAnnotations()method. This method returns an array of Annotation

In the following example we tried to read all annotations from the sayHi method. First we need to obtain the method object it self. Because the sayHi method has parameters, we need to pass not only the method name to the getMethod method, but we also need to pass the parameter’s type.

The getAnnotations method returns only annotation that has a RetentionPolicy.RUNTIME. Because other retention policy doesn’t allow the annotation to available at runtime.

package org.kodejava.example.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class GetAllAnnotation {
    private Map<string, string=""> data = new HashMap<string, string="">();

    @SuppressWarnings("unchecked")
    public static void main(String args[]) {
        GetAllAnnotation demo = new GetAllAnnotation();
        demo.sayHi("001", "Alice");
        demo.sayHi("004", "Malory");

        try {
            Class clazz = demo.getClass();

            //
            // To get the sayHi() method we need to pass not only the 
            // method name but also its parameters type so the the 
            // getMethod() method return the correct method for us to 
            // use.
            //
            Method method = clazz.getMethod("sayHi", String.class,
                    String.class);

            //
            // Get all annotations from the sayHi() method. But this 
            // actually will only return one annotation. Because only 
            // the HelloAnno annotation that has RUNTIME retention 
            // policy, which means that the other annotations associated 
            // with sayHi() method is not available at runtime.
            //
            Annotation[] annotations = method.getAnnotations();
            for (Annotation anno : annotations) {
                System.out.println("Type: " + anno.annotationType());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    @MyAnno("Hi")
    @HelloAnno(value = "Hello", greetTo = "Everyone")
    public void sayHi(String dataId, String name) {
        Map data = getData();
        if (data.containsKey(dataId)) {
            System.out.println("Hello " + data.get(dataId));
        } else {
            data.put(dataId, name);
        }
    }

    private Map<string, string=""> getData() {
        data.put("001", "Alice");
        data.put("002", "Bob");
        data.put("003", "Carol");
        return data;
    }
}
package org.kodejava.example.annotation;

public @interface MyAnno {
    String value();
}

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply