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.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.lang.annotation.Annotation;
@Retention (RetentionPolicy.RUNTIME)
@interface customAnnotation {
public String key();
public String value();
}
public class GFG {
@customAnnotation (key = "AvengersLeader" ,
value = "CaptainAmerica" )
public static void
getCustomAnnotation()
{
try {
Class c = GFG. class ;
Method[] methods = c.getMethods();
Method method = null ;
for (Method m : methods) {
if (m.getName().equals( "getCustomAnnotation" ))
method = m;
}
Annotation[] annotation = method.getDeclaredAnnotations();
for (Annotation a : annotation) {
customAnnotation self = (customAnnotation)a;
System.out.println( "Annotation details" );
System.out.println( "key: " + self.key());
System.out.println( "value: " + self.value());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
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.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.lang.annotation.Annotation;
@Retention (RetentionPolicy.RUNTIME)
@interface SelfCreatedAnnotation {
public String key();
public String value();
}
class GFGDemoClass {
private String field;
@SelfCreatedAnnotation (key = "getField" ,
value = "getting field attribute" )
public String
getField()
{
return field;
}
}
public class GFG {
public static void main(String[] args)
{
Method[] methods = GFGDemoClass. class .getMethods();
Annotation[] annotation = methods[ 0 ]
.getDeclaredAnnotations();
for (Annotation a : annotation) {
SelfCreatedAnnotation self = (SelfCreatedAnnotation)a;
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–