- java.lang.reflect 包類
- java.lang.reflect - 主頁
- java.lang.reflect - AccessibleObject
- java.lang.reflect - Array
- java.lang.reflect - Constructor<T>
- java.lang.reflect - Field
- java.lang.reflect - Method
- java.lang.reflect - Modifier
- java.lang.reflect - Proxy
- java.lang.reflect 包額外內容
- java.lang.reflect - 介面
- java.lang.reflect - 異常
- java.lang.reflect - 錯誤
- java.lang.reflect 實用資源
- java.lang.reflect - 快速指南
- java.lang.reflect - 實用資源
- java.lang.reflect - 討論
java.lang.reflect.Method.getParameterAnnotations() 方法示例
說明
java.lang.reflect.Method.getParameterAnnotations() 方法返回一個數組,其表示此 Method 物件所表示方法的正式引數的宣告順序的註釋。(如果底層方法無引數,則返回長度為 0 的陣列。如果方法有一個或多個引數,則為每個沒有註釋的引數返回一個長度為 0 的巢狀陣列。)返回陣列中包含的註釋物件都是可序列化的。此方法的呼叫方可以自由修改返回的陣列;它對返回給其他呼叫方的陣列沒有影響。
宣告
以下是 java.lang.reflect.Method.getParameterAnnotations() 方法的宣告。
public Annotation[][] getParameterAnnotations()
返回
底層成員的簡單名稱。
示例
以下示例顯示了 java.lang.reflect.Method.getParameterAnnotations() 方法的用法。
現場演示package com.tutorialspoint;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
public class MethodDemo {
public static void main(String[] args) {
Method[] methods = SampleClass.class.getMethods();
Annotation[][] annotations = methods[1].getParameterAnnotations();
for(Annotation[] annotation1 : annotations){
for(Annotation annotation : annotation1){
if(annotation instanceof CustomAnnotation){
CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
System.out.println("name: " + customAnnotation.name());
System.out.println("value: " + customAnnotation.value());
}
}
}
}
}
@CustomAnnotation(name = "SampleClass", value = "Sample Class Annotation")
class SampleClass {
private String sampleField;
public String getSampleField() {
return sampleField;
}
public void setSampleField(@CustomAnnotation(name="sampleClassMethod",
value = "Sample Method Annotation") String sampleField) {
this.sampleField = sampleField;
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
public String name();
public String value();
}
讓我們編譯並執行上述程式,這將產生以下結果 −
name: sampleClassMethod value: Sample Method Annotation
java_reflect_method.htm
廣告