Method Class | getDeclaredAnnotations() method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

key: getField
value: getting field attribute

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.