-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAnnotation.java
More file actions
44 lines (32 loc) · 1.18 KB
/
TestAnnotation.java
File metadata and controls
44 lines (32 loc) · 1.18 KB
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
41
42
43
44
package Java8Study.Annotation;
import org.junit.Test;
import java.lang.reflect.Method;
/**
* 重复注解 与 类型注解
*
* 重复注解使用时,注意使用容器,并用 Repeatable 注解告知是哪个容器
* 通过反射拿到注解实例时,注意是注解数组
*
* 类型注解,在 注解的 @Target 里要加上 TYPE_PARAMETER,表示注解可以修饰 参数类型
*
* 修饰参数类型,可以很方便地对参数进行检查
*
*
*/
public class TestAnnotation {
// 需要使用容器类 ,实现重复注解
@MyAnnotation("我是重复注解1")
@MyAnnotation("我是重复注解2")
public void show(@MyAnnotation("我可以注解类型") String str){
}
@Test
public void test1() throws NoSuchMethodException {
//通过反射 拿到修饰方法的注解,因为是重复注解,可以拿到多个
Class<TestAnnotation> clazz = TestAnnotation.class;
Method method = clazz.getMethod("show");
MyAnnotation[] myAnnotations = method.getAnnotationsByType(MyAnnotation.class);
for(MyAnnotation myAnnotation : myAnnotations){
System.out.println(myAnnotation.value());
}
}
}