Java8对注解处理提供了两点改进:可重复得注解及可用于类型得注解。

    1. @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
    2. @Retention(RetentionPolicy.RUNTIME)
    3. public @interface MyAnnotations {
    4. MyAnnotation[] value();
    5. }
    6. @Repeatable(MyAnnotations.class)
    7. @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, TYPE_PARAMETER})
    8. @Retention(RetentionPolicy.RUNTIME)
    9. public @interface MyAnnotation {
    10. String value() default "atguigu";
    11. }
    12. public class TestAnnotation {
    13. @Test
    14. public void test1() throws NoSuchMethodException {
    15. Class<TestAnnotation> clazz = TestAnnotation.class;
    16. Method show = clazz.getMethod("show");
    17. MyAnnotation[] annotations = show.getAnnotationsByType(MyAnnotation.class);
    18. for (MyAnnotation annotation : annotations) {
    19. System.out.println(annotation.value());
    20. }
    21. }
    22. @MyAnnotation("Hello")
    23. @MyAnnotation("World")
    24. public void show(@MyAnnotation("abc") String str) {
    25. }
    26. }