java 获取类上的注解值_java 获取类,属性变量,方法,方法参数上注解的值等...

本文详细介绍了Java注解的使用,包括获取类、属性变量、方法以及方法参数上注解的值。通过示例展示了如何利用反射API获取注解信息,包括@Orange、@Banana、@Apple和@Cherry等自定义注解的实践。同时,提供了获取方法参数注解值的方法`getMethodParameterNamesByAnnotation`。
摘要由CSDN通过智能技术生成

一:获取类上注解的值

定义注解@Target(ElementType.TYPE)用于类,接口等

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

public @interface Orange {

String getName();

String getValue();

}

获取

@Orange(getName = "3333",getValue = "4444")

public class ParameterNameTest {

。。。

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

if(clazz.isAnnotationPresent(Orange.class)){

// 获取 "类" 上的注解

Orange getAnnotation = clazz.getAnnotation(Orange.class);

System.out.println("\"类\"上的注解值获取到第一个 :"

+ getAnnotation.getName()+ ",第二个:"+ getAnnotation.getValue());

}

}

返回

"类"上的注解值获取到第一个 :3333,第二个:4444

二:获取属性变量上注解的值

定义注解@Target(ElementType.FIELD)用于属性变量

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Banana {

String length();

String price();

}

获取

public class ParameterNameTest {

@Banana(length = "6666", price = "$888")

String something = "other information";

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

// 获取 "属性变量" 上的注解的值

Field[] fields = clazz.getDeclaredFields();

for(Field field: fields){

if(field.isAnnotationPresent(Banana.class)){

Banana bananaAnnotation = field.getAnnotation(Banana.class);

System.out.println("\"属性变量\"上的注解值获取到第一个 :"

+ bananaAnnotation.length()+ ",第二个:"+ bananaAnnotation.price());

}

}

}

}

返回

"属性变量"上的注解值获取到第一个 :6666,第二个:$888

三: 获取方法上注解的值

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Apple {

String color();

String number();

}

获取

public class ParameterNameTest {

@Apple(color = "红色", number = "5555")

public void method1(){

// ...

}

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

// 获取 "方法"上的注解的值

Method[] methods = clazz.getDeclaredMethods();

for (Method method: methods){

if(method.isAnnotationPresent(Apple.class)){

Apple appleAnnotation = method.getAnnotation(Apple.class);

System.out.println("\"方法\"上的注解值获取到第一个 :"

+ appleAnnotation.color()+ ",第二个:"+ appleAnnotation.number());

}

}

}

}

返回

"方法"上的注解值获取到第一个 :红色,第二个:5555

三: 获取" 方法参数 " 上注解的值

@Target(ElementType.PARAMETER)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Cherry {

String value();

}

获取

public class ParameterNameTest {

public void method2(@Cherry("1111") String param1, @Cherry("2222") String param2) {

System.out.println(param1 + param2);

}

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

// 获取 "方法参数" 上的注解的值

Method method = clazz.getDeclaredMethod("method2", String.class, String.class);

String[] parameterNames = getMethodParameterNamesByAnnotation(method);

System.out.println("\"方法参数\"上的注解值获取到"+Arrays.toString(parameterNames));

}

/**

* 获取给 "方法参数" 进行注解的值

*

* @param method 要获取参数名的方法

* @return 按参数顺序排列的参数名列表

*/

public static String[] getMethodParameterNamesByAnnotation(Method method) {

Annotation[][] parameterAnnotations = method.getParameterAnnotations();

if (parameterAnnotations == null || parameterAnnotations.length == 0) {

return null;

}

String[] parameterNames = new String[parameterAnnotations.length];

int i = 0;

for (Annotation[] parameterAnnotation : parameterAnnotations) {

for (Annotation annotation : parameterAnnotation) {

if (annotation instanceof Cherry) {

Cherry param = (Cherry) annotation;

parameterNames[i++] = param.value();

}

}

}

return parameterNames;

}

}

返回

"方法参数"上的注解值获取到[1111, 2222]

小结:主要使用的API是Class类中的实现接口AnnotatedElement的方法

isAnnotationPresent --- 检测该元素是否被对应注解修饰

default boolean isAnnotationPresent(Class extends Annotation> annotationClass) {

return getAnnotation(annotationClass) != null;

}

getAnnotation --- 获取注解对象

T getAnnotation(Class annotationClass);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值