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


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