重复注解
注解类
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Repeatable(MyAnnotationContainer.class)//注解容器类 需要这个才能重复注解
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "zhangsan";
}
注解容器类
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationContainer {
MyAnnotation[] value();
}
测试类
import java.lang.reflect.Method;
public class TestAnnotation {
@MyAnnotation("ccc")
@MyAnnotation("bbb")
public void aa(){
}
public static void main(String[] args) throws NoSuchMethodException {
Class<TestAnnotation> clazz = TestAnnotation.class;
Method m1 = clazz.getMethod("aa");
MyAnnotation[] myAnnotations = m1.getAnnotationsByType(MyAnnotation.class);
for (MyAnnotation e:myAnnotations){
System.out.println(e.value());
}
}
}
类型注解
添加这个 就可以使用类型注解了