Java 8对注解处理提供了两点改进:可重复的注解及可用于类型的注解
public class AnnotationTest {
//可重复的注解
@MyAnnotation("hello")
@MyAnnotation("word")
public void show(@MyAnnotation("abc") String str){//可用于类型的注解
}
@Test
public void test1() throws NoSuchMethodException {
Class<AnnotationTest> clazz = AnnotationTest.class;
Method m1 = clazz.getMethod("show");
MyAnnotation[] ma = m1.getAnnotationsByType(MyAnnotation.class);
for (MyAnnotation m :ma) {
System.out.println(m.value());
}
//hello
//word
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE,TYPE_PARAMETER})
public @interface MyAnnotations {
MyAnnotation[] value();
}
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE,TYPE_PARAMETER})
public @interface MyAnnotation {
String value() default "hi";
}
最后
以上就是年轻星月最近收集整理的关于重复注解与类型注解的全部内容,更多相关重复注解与类型注解内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复