当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。