Java8对注解处理提供了两点改进:可重复得注解及可用于类型得注解。
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotations {MyAnnotation[] value();}@Repeatable(MyAnnotations.class)@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, TYPE_PARAMETER})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {String value() default "atguigu";}public class TestAnnotation {@Testpublic void test1() throws NoSuchMethodException {Class<TestAnnotation> clazz = TestAnnotation.class;Method show = clazz.getMethod("show");MyAnnotation[] annotations = show.getAnnotationsByType(MyAnnotation.class);for (MyAnnotation annotation : annotations) {System.out.println(annotation.value());}}@MyAnnotation("Hello")@MyAnnotation("World")public void show(@MyAnnotation("abc") String str) {}}
