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


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


Method類的java.lang.reflect.Method.getAnnotation(Class <T>注記類)方法返回指定類型的方法對象的注解(如果存在),則將其作為參數傳遞給參數,否則為null。這是獲取Method對象注釋的重要方法。

用法:

public <T extends Annotation> T getAnnotation(Class<T> annotationClass)

參數:此方法采用強製性參數注釋類,它是注釋類型的Class對象。



返回值:如果此元素上存在指定的注釋類型,則此方法返回該方法的注釋,否則返回null。

異常:如果給定的注釋類為null,則此方法引發NullPointerException

以下示例程序旨在說明Method類的getAnnotation(Class注解類)方法:

範例1:此程序將打印指定注釋類型的方法注釋,該注釋作為參數提供給表示GFG類的getCustomAnnotation()方法的“方法對象”的getAnnotation()。

在該示例中,使用單個類,並且該類包含兩種方法,它們是主要方法和帶有注釋的方法。

// Program Demonstrate getAnnotation(Class<T> annotationClass) method 
// of Method Class. 
  
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.reflect.Method; 
  
// create a custom Annotation 
@Retention(RetentionPolicy.RUNTIME) 
@interface Annotation { 
  
    // 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 
    @Annotation(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 Annotation of Method object m by passing 
            // Annotation class object as parameter 
            Annotation anno = method.getAnnotation(Annotation.class); 
  
            // print Annotation Details 
            System.out.println("Annotation for Method Object"
                               + " having name:" + method.getName()); 
            System.out.println("Key Attribute of Annotation:"
                               + anno.key()); 
            System.out.println("Value Attribute of Annotation:"
                               + anno.value()); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
  
    // create main method 
    public static void main(String args[]) 
    { 
        getCustomAnnotation(); 
    } 
}
輸出:
Annotation for Method Object having name:getCustomAnnotation
Key Attribute of Annotation:AvengersLeader
Value Attribute of Annotation:CaptainAmerica

範例2:此程序將打印指定注釋類型的方法注釋,該注釋作為參數提供給表示GFG類的getCustomAnnotation()方法的“方法對象”的getAnnotation()。

在此示例中,使用了兩個類。一個類包含main方法,該方法創建方法對象並應用getAnnotation()方法,而另一個類包含帶有某些注釋的方法。

// Program Demonstrate getAnnotation(Class<T> annotationClass) method 
// of Method Class. 
  
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.reflect.Method; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get array Method objects 
        Method[] methods = GFGDemoClass.class.getMethods(); 
  
        // get Annotation 
        SelfCreatedAnnotation annotation = methods[0] 
                                               .getAnnotation( 
                                                   SelfCreatedAnnotation 
                                                       .class); 
  
        // Print annotation attribute 
        System.out.println("key:" + annotation.key()); 
        System.out.println("value:" + annotation.value()); 
    } 
} 
  
// Another class on which we want to apply the annotation 
class GFGDemoClass { 
    private String field; 
  
    // create annotation 
    @SelfCreatedAnnotation(key = "getField", 
                           value = "getting field attribute") 
    public String 
    getField() 
    { 
        return field; 
    } 
} 
  
// create custom annotation having two values 
@Retention(RetentionPolicy.RUNTIME) 
@interface SelfCreatedAnnotation { 
    public String key(); 
    public String value(); 
}
輸出:
key:getField
value:getting field attribute

參考: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getAnnotation-java.lang.Class-




相關用法


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