Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaMethod Class | getAnnotation() method in Java

Method Class | getAnnotation() method in Java

The java.lang.reflect.Method.getAnnotation(Class< T > annotationClass) method of Method class returns Method objects’ annotation for the specified type passed as parameter if such an annotation is present, else null. This is important method to get annotation for Method object. Syntax:

public <T extends Annotation> T getAnnotation(Class<T> annotationClass)

Parameter: This method takes a mandatory parameter annotationClass which is the Class object of the annotation type. Return Value: This method returns the method’s annotation for the specified annotation type if present on this element, else null. Exception: This method throws NullPointerException if the given annotation class is null Below program illustrates getAnnotation(Class annotationClass) method of Method class: Example 1: This program prints method’s annotation for the specified annotation type given as parameter to getAnnotation() of Method Object representing getCustomAnnotation() method of GFG class. In this example, a single class is used and class contains both methods which are main method and method with annotation. 

Java




// Program Demonstrate getAnnotation(Class<T> annotationClass) method
// of Method Class.
 
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
 
// create a custom Annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation {
 
    // This annotation has two attributes.
    public String key();
 
    public String value();
}
 
// create the Main Class
public class GFG {
 
    // call Annotation for method and pass values for annotation
    @Annotation(key = "AvengersLeader", value = "CaptainAmerica")
    public static void getCustomAnnotation()
    {
 
        try {
 
            // create class object for class name GFG
            Class c = GFG.class;
 
            // get method name getCustomAnnotation as Method object
            Method[] methods = c.getMethods();
            Method method = null;
            for (Method m : methods) {
                if (m.getName().equals("getCustomAnnotation"))
                    method = m;
            }
 
            // get Annotation of Method object m by passing
            // Annotation class object as parameter
            Annotation anno = method.getAnnotation(Annotation.class);
 
            // print Annotation Details
            System.out.println("Annotation for Method Object"
                               + " having name: " + method.getName());
            System.out.println("Key Attribute of Annotation: "
                               + anno.key());
            System.out.println("Value Attribute of Annotation: "
                               + anno.value());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    // create main method
    public static void main(String args[])
    {
        getCustomAnnotation();
    }
}


Output:

Annotation for Method Object having name: getCustomAnnotation
Key Attribute of Annotation: AvengersLeader
Value Attribute of Annotation: CaptainAmerica

Example 2: This program prints method’s annotation for the specified annotation type given as parameter to getAnnotation() of Method Object representing getCustomAnnotation() method of GFG class. In this example a two classes is used.One class contains main method which creates the method object and applying getAnnotation() method and other class contains method with some annotation. 

Java




// Program Demonstrate getAnnotation(Class<T> annotationClass) method
// of Method Class.
 
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // get array Method objects
        Method[] methods = GFGDemoClass.class.getMethods();
 
        // get Annotation
        SelfCreatedAnnotation annotation = methods[0]
                                               .getAnnotation(
                                                   SelfCreatedAnnotation
                                                       .class);
 
        // Print annotation attribute
        System.out.println("key: " + annotation.key());
        System.out.println("value: " + annotation.value());
    }
}
 
// Another class on which we want to apply the annotation
class GFGDemoClass {
    private String field;
 
    // create annotation
    @SelfCreatedAnnotation(key = "getField",
                           value = "getting field attribute")
    public String
    getField()
    {
        return field;
    }
}
 
// create custom annotation having two values
@Retention(RetentionPolicy.RUNTIME)
@interface SelfCreatedAnnotation {
    public String key();
    public String value();
}


Output:

key: getField
value: getting field attribute

Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getAnnotation-java.lang.Class-

RELATED ARTICLES

Most Popular

Recent Comments