📜  方法类 | Java中的 getDeclaredAnnotations() 方法

📅  最后修改于: 2022-05-13 01:55:33.502000             🧑  作者: Mango

方法类 | Java中的 getDeclaredAnnotations() 方法

Method 类的Java.lang.reflect.Method.getDeclaredAnnotations()方法返回仅在方法上声明的注解,并忽略方法继承的注解。如果方法上没有直接声明注解,则返回的注解数组为空。返回数组的修改不会对返回给其他调用者的数组产生影响。当不同的调用者调用它们时,所有返回的注释数组都是相互独立的。

句法:

public Annotation[] getDeclaredAnnotations()

返回值:此方法返回直接存在于此元素上的注释数组

下面的程序说明了 Method 类的 getDeclaredAnnotations() 方法:

程序 1:此程序仅打印在方法上声明的注释,并忽略方法对象上使用 getDeclaredAnnotations() 方法继承的注释。

// 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();
    }
}
输出:
Annotation details
key: AvengersLeader
value: CaptainAmerica

程序 2:此程序仅打印在方法上声明的注解,并忽略通过在其他类的 Method 对象上使用 getDeclaredAnnotations() 方法的方法继承的注解。

在这个程序中有两个不同的类。一个类包含带有一些注释的方法,另一个类包含主要方法。在 main Method 方法对象中为包含注解的其他类的方法创建。使用 getDeclaredAnnotations() 方法创建方法对象后,仅在方法上声明的注释被收集,并且在收集注释信息程序后打印结果。

// 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());
        }
    }
}
输出:
key: getField
value: getting field attribute

参考: https: Java/lang/reflect/Method.html#getDeclaredAnnotations–