當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Method類 getParameterAnnotations()用法及代碼示例


Method類的java.lang.reflect.Method.getParameterAnnotations()方法返回一個二維Annotation數組,該數組表示Method對象的參數注釋。如果該方法不包含任何參數,則將返回一個空數組。如果方法包含一個或多個參數,則將返回二維注釋數組。對於沒有注釋的參數,此二維數組的嵌套數組將為空。

在編譯時,將強製參數和綜合參數添加到陣列中。強製參數是在源中隱式聲明的參數,而綜合參數是在源中既不隱式也不顯式聲明的參數。

此方法返回的注釋對象是可序列化的。通過此方法返回的數組的數組可以輕鬆修改。



用法:

public Annotation[][] getParameterAnnotations()

返回值:此方法返回二維注釋數組,該數組表示Method對象的形式參數和隱式參數上的注釋。

以下示例程序旨在說明Method類的getParameterAnnotations()方法:

範例1:將getParameterAnnotations()用於指定為輸入的特定方法。

該程序包含一個方法名稱setManyValues(),其中包含參數注釋。因此,該程序將通過setManyValues()方法獲取包含的所有參數注釋。

// 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,則將getParameterAnnotations()用於GFG類的方法。

// 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://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getParameterAnnotations-




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getParameterAnnotations() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。