Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

java获取类各类注解值

java获取类各类注解值

获取类上注解的值

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

1
2
3
4
5
6
7
8
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Orange {

String getName();

String getValue();
}

获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Orange(getName = "3333",getValue = "4444")
public class ParameterNameTest {

。。。

@Test
public void main() throws Exception {

Class<ParameterNameTest> clazz = ParameterNameTest.class;

if(clazz.isAnnotationPresent(Orange.class)){
// 获取 "类" 上的注解
Orange getAnnotation = clazz.getAnnotation(Orange.class);
System.out.println("\"类\"上的注解值获取到第一个 :"
+ getAnnotation.getName()+ ",第二个:"+ getAnnotation.getValue());
}
}
```java
返回

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

1
2
3
4
5
6
7
8
9
10
# 获取属性变量上注解的值
定义注解@Target(ElementType.FIELD)用于属性变量
```java
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Banana {

String length();
String price();
}

获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ParameterNameTest {

@Banana(length = "6666", price = "$888")
String something = "other information";

@Test
public void main() throws Exception {

Class<ParameterNameTest> 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());
}
}
}
}

返回

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

获取方法上注解的值

1
2
3
4
5
6
7
8
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Apple {

String color();

String number();
}

获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class ParameterNameTest {

@Apple(color = "红色", number = "5555")
public void method1(){
// ...
}

@Test
public void main() throws Exception {

Class<ParameterNameTest> 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());
}
}
}
}

返回

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

获取” 方法参数 “ 上注解的值

1
2
3
4
5
6
7
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Cherry {

String value();
}

获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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<ParameterNameTest> 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;
}
}

返回

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

小结

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

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

1
2
3
default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return getAnnotation(annotationClass) != null;
}

getAnnotation — 获取注解对象

1
<T extends Annotation> T getAnnotation(Class<T> annotationClass);