通过反射获取注解属性
博格巴世界杯 510 2025-10-21 03:52:05

1、获取类上注解的值

定义注解@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;

//判断orange是否是一个注解

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

// 获取 "类" 上的注解

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

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

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

}

}

返回

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

2、获取属性变量上注解的值

定义注解@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

3、获取方法上注解的值

定义方法上使用的注解

@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

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

定义注解

@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); 原文链接:https://blog.csdn.net/weixin_40001519/article/details/114525197

Copyright © 2022 98世界杯_乌拉圭世界杯 - cy078.com All Rights Reserved.