1、定义重复的注解容器注解
2、定义一个可以重复的注解
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@MyTest("ca")
@MyTest("cb")
public class Demo4 {
@MyTest("ma")
@MyTest("mb")
public void show() {
}
public static void main(String[] args) {
MyTest[] annotationsByType = Demo4.class.getAnnotationsByType(MyTest.class);
for (MyTest myTest : annotationsByType) {
System.out.println(myTest);
}
System.out.println("--------------------");
try {
MyTest[] shows = Demo4.class.getMethod("show").getAnnotationsByType(MyTest.class);
for (MyTest show : shows) {
System.out.println(show);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
/**
* 1、定义重复的注解容器注解
*/
@Retention(RetentionPolicy.RUNTIME)
@interface MyTests {
MyTest[] value();
}
/**
* 2、定义一个可以重复的注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyTests.class)
@interface MyTest {
String value();
}