重复注解与类型注解

来源:互联网 发布:强力卸载软件 编辑:程序博客网 时间:2024/06/05 03:11
/**
* 重复注解和类型注解
*/
public classTestAnnotation {
// 4、
@MyAnnotation("hello")
@MyAnnotation("world")
public void show(){

}
@Test
public void test1()throwsException {
Class<TestAnnotation> clazz = TestAnnotation.class;
Method m1 = clazz.getMethod("show");
MyAnnotation[] mas = m1.getAnnotationsByType(MyAnnotation.class);
for(MyAnnotationmyAnnotation : mas) {
System.out.println(myAnnotation.value());
}
}
}
// 如果忘了怎么写,随便找一个进去,复制一下比如@SuppressWarnings()
// 3、
@Repeatable(MyAnnotations.class)// 想用重复注解,需要repeatable修饰
// 1、
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)// 生命周期
@interfaceMyAnnotation{
Stringvalue()default"coco";
}
// 2、
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)// 生命周期
@interfaceMyAnnotations{
MyAnnotation[]value();
}
原创粉丝点击