java getannotation(),Java Method类 getAnnotation()用法及代码示例

Method类的java.lang.reflect.Method.getAnnotation(Class 注记类)方法返回指定类型的方法对象的注解(如果存在),则将其作为参数传递给参数,否则为null。这是获取Method对象注释的重要方法。

用法:

public T getAnnotation(Class annotationClass)

参数:此方法采用强制性参数注释类,它是注释类型的Class对象。

返回值:如果此元素上存在指定的注释类型,则此方法返回该方法的注释,否则返回null。

异常:如果给定的注释类为null,则此方法引发NullPointerException

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

范例1:此程序将打印指定注释类型的方法注释,该注释作为参数提供给表示GFG类的getCustomAnnotation()方法的“方法对象”的getAnnotation()。

在该示例中,使用单个类,并且该类包含两种方法,它们是主要方法和带有注释的方法。

// Program Demonstrate getAnnotation(Class 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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值