Open In App

Method Class | getDeclaredAnnotations() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

The java.lang.reflect.Method.getDeclaredAnnotations() method of Method class returns annotations declared only on the method and ignores inherited annotations by method. If there are no annotations directly declared on the method, the returned array of annotation is empty. Modification of the returned array will have no effect on the arrays returned to other callers. All returned array of annotations are independent to each other when they called by the different caller.

Syntax:

public Annotation[] getDeclaredAnnotations()

Return Value: This method returns an array of annotations directly present on this element

Below program illustrates getDeclaredAnnotations() method of Method class:

Program 1: This Program prints annotations declared only on the method and ignores inherited annotations by method using getDeclaredAnnotations() method on Method object.




// Program Demonstrate getDeclaredAnnotations()
// method of Method Class.
  
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.lang.annotation.Annotation;
  
// create a custom Annotation
@Retention(RetentionPolicy.RUNTIME)
@interface customAnnotation {
  
    // 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
    @customAnnotation(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 an array of Annotations
            Annotation[] annotation = method.getDeclaredAnnotations();
  
            // get annotation from the array of annotation
            // and print the details
            for (Annotation a : annotation) {
  
                customAnnotation self = (customAnnotation)a;
                // Print annotation attribute
                System.out.println("Annotation details");
                System.out.println("key: " + self.key());
                System.out.println("value: " + self.value());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    // create main method
    public static void main(String args[])
    {
        getCustomAnnotation();
    }
}


Output:

Annotation details
key: AvengersLeader
value: CaptainAmerica

Program 2: This Program prints annotations declared only on the method and ignores inherited annotations by method using getDeclaredAnnotations() method on Method object of other class.

In this program there are two different classes. One class contains method with some annotation and other class contains main method. In main Method method object for the method of other class containing annotation is created. After creating method object using getDeclaredAnnotations() method annotations declared only on the method is collected and after collecting annotation Info program is printing the result.




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


Output:

key: getField
value: getting field attribute

Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getDeclaredAnnotations–



Last Updated : 05 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads