Java8新特性之重复注解与类型注解
重复注解
@Repeatable
@Repeatable(MyAnnotations.class)
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.LOCAL_VARIABLE,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "fjh";
}
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.LOCAL_VARIABLE,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotations {
MyAnnotation[] value();
}
@Test
public void test1() throws Exception {
Class<TestAnnotation> clazz = TestAnnotation.class;
Method method = clazz.getMethod("show");
MyAnnotation[] ma = method.getAnnotationsByType(MyAnnotation.class);
for (MyAnnotation myAnnotation : ma) {
System.out.println(myAnnotation.value());
}
}
@MyAnnotation("1")
@MyAnnotation("2")
public void show(){
}

类型注解
@Target({ElementType.TYPE_PARAMETER})

浙公网安备 33010602011771号