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

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

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

Method 类的Java.lang.reflect.Method.getParameterAnnotations()方法返回一个二维 Annotation 数组,表示 Method 对象的参数注释。如果 Method 不包含参数,则将返回一个空数组。如果 Method 包含一个或多个参数,则将返回一个二维 Annotation 数组。这个二维数组的嵌套数组对于没有注释的参数将为空。

在编译时,强制和综合参数被添加到数组中。强制参数是在源中隐式声明的参数,而综合参数是在源中既不隐式也不显式声明的参数。

该方法返回的注解对象是可序列化的。可以轻松修改此方法返回的数组数组。

句法:

public Annotation[][] getParameterAnnotations()

返回值:该方法返回一个二维Annotation数组,表示Method对象的形式参数和隐式参数上的注释。

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

示例 1:对作为输入给出的特定方法使用 getParameterAnnotations()。

该程序包含一个方法名称 setManyValues(),其中包含参数注释。所以这个程序将通过 setManyValues() 方法获取所有参数 Annotations 包含的内容。

// Java program to demonstrate how to
// apply getParameterAnnotations() method
// of Method Class.
  
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
        try {
            // create class object
            Class classobj = GFG.class;
  
            // create method object of setManyValues
            Method setManyValueObject = null;
  
            Method[] methods = classobj.getMethods();
  
            for (Method m : methods) {
                if (m.getName().equals("setManyValues"))
                    setManyValueObject = m;
            }
  
            // get Annotation of parameter
            Annotation[][] Arrayannotations = setManyValueObject
                                                  .getParameterAnnotations();
  
            System.out.println("Method Name: "
                               + setManyValueObject.getName());
  
            // print parameter annotation
            for (Annotation[] annotationRow : Arrayannotations) {
                for (Annotation annotation : annotationRow) {
                    AnnotationDemo anndemo = (AnnotationDemo)annotation;
                    System.out.println("key of annotation: "
                                       + anndemo.key());
                    System.out.println("value of annotation: "
                                       + anndemo.value());
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    // method name setManyValues
    public void
        setManyValues(@AnnotationDemo(key = "methodParameter1",
                                      value = "Some value")
                      String parameter1)
    {
        System.out.println("setManyValues");
    }
}
  
// create a custom Annotation to apply on method
@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationDemo {
  
    // This annotation has two attributes.
    public String key();
  
    public String value();
}
输出:
Method Name: setManyValues
key of annotation: methodParameter1
value of annotation: Some value

示例 2:如果方法包含 ParameterAnnotations,则对 GFG 类的方法使用 getParameterAnnotations()。

// Java program to demonstrate how to
// apply getParameterAnnotations() method
// of Method Class.
  
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
        try {
            // create class object
            Class classobj = GFG.class;
  
            // create list of method objects
            Method[] methods = classobj.getMethods();
  
            for (Method m : methods) {
  
                // print details for only getValues
                // and setValue method
                // so filter list of methods
                if (m.getName().equals("getValues")
                    || m.getName().equals("setValue")) {
  
                    // get Annotations of parameter
                    Annotation[][] Arrayannotations
                        = m
                              .getParameterAnnotations();
  
                    System.out.println("Method Name: "
                                       + m.getName());
  
                    // print parameter annotation
                    printAnnotation(Arrayannotations);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    // method name setValue with
    // no annotations at parameter
    public void setValue()
    {
        System.out.println("setValue");
    }
  
    // method name getValues with annotations at the parameter
    public String
        getValues(@AnnotationDemo(field1 = "GFG",
                                  field2 = "Works",
                                  field3 = "fine",
                                  field4 = "Hurray!!")
                  String value)
    {
        System.out.println("setManyValues");
        return value;
    }
  
    public static void
        printAnnotation(Annotation[][] Arrayannotations)
    {
        System.out.println("Annotation length: "
                           + Arrayannotations.length);
  
        // print parameter annotation
        for (Annotation[] annotationRow : Arrayannotations) {
            for (Annotation annotation : annotationRow) {
  
                AnnotationDemo anndemo = (AnnotationDemo)annotation;
                System.out.println("field1 of annotation: "
                                   + anndemo.field1());
                System.out.println("field2 of annotation: "
                                   + anndemo.field2());
                System.out.println("field3 of annotation: "
                                   + anndemo.field3());
                System.out.println("field4 of annotation: "
                                   + anndemo.field4());
            }
        }
    }
}
  
// create a custom Annotation to apply on method
@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationDemo {
  
    // This annotation has many attributes.
    public String field1();
    public String field2();
    public String field3();
    public String field4();
}
输出:
Method Name: getValues
Annotation length: 1
field1 of annotation: GFG
field2 of annotation: Works
field3 of annotation: fine
field4 of annotation: Hurray!!
Method Name: setValue
Annotation length: 0

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