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


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


Method類的java.lang.reflect.Method.getDeclaredAnnotations()方法返回僅在方法上聲明的注釋,並忽略方法繼承的注釋。如果沒有在方法上直接聲明的注釋,則返回的注釋數組為空。返回數組的修改對返回給其他調用方的數組無效。當所有返回的批注數組由不同的調用方調用時,它們彼此獨立。

用法:

public Annotation[] getDeclaredAnnotations()

返回值:此方法返回直接存在於此元素上的注釋數組



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

程序1:該程序打印僅在方法上聲明的注釋,並使用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 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方法。在主方法的方法對象中創建包含注釋的其他類的方法。使用getDeclaredAnnotations()方法創建方法對象後,將收集僅在方法上聲明的注釋,並且在收集注釋後,Info程序將打印結果。

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




相關用法


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