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

    写一个注解,如下

    1. @Retention(RetentionPolicy.RUNTIME)
    2. @Target({TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR,
    3. LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE})
    4. public @interface MyAnnotation {
    5. String value() default "编程指南";
    6. }
    7. //其中RetentionPolicy
    8. public enum RetentionPolicy {
    9. /**
    10. * Annotations are to be discarded by the compiler.
    11. */
    12. SOURCE,
    13. /**
    14. * Annotations are to be recorded in the class file by the compiler
    15. * but need not be retained by the VM at run time. This is the default
    16. * behavior.
    17. */
    18. CLASS,
    19. /**
    20. * Annotations are to be recorded in the class file by the compiler and
    21. * retained by the VM at run time, so they may be read reflectively.
    22. *
    23. * @see java.lang.reflect.AnnotatedElement
    24. */
    25. RUNTIME
    26. }
    27. //ElementType如下
    28. public enum ElementType {
    29. /** Class, interface (including annotation type), or enum declaration */
    30. TYPE,
    31. /** Field declaration (includes enum constants) */
    32. FIELD,
    33. /** Method declaration */
    34. METHOD,
    35. /** Formal parameter declaration */
    36. PARAMETER,
    37. /** Constructor declaration */
    38. CONSTRUCTOR,
    39. /** Local variable declaration */
    40. LOCAL_VARIABLE,
    41. /** Annotation type declaration */
    42. ANNOTATION_TYPE,
    43. /** Package declaration */
    44. PACKAGE,
    45. /**
    46. * Type parameter declaration
    47. *
    48. * @since 1.8
    49. */
    50. TYPE_PARAMETER,
    51. /**
    52. * Use of a type
    53. *
    54. * @since 1.8
    55. */
    56. TYPE_USE
    57. }

    jdk8以前想实现可重复的注解需要这样

    1. //1. 写一个注解
    2. @Retention(RetentionPolicy.RUNTIME)
    3. @Target({TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR,
    4. LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE})
    5. public @interface MyAnnotation {
    6. String value() default "编程指南";
    7. }
    8. //2. 写一个数组类型的注解
    9. @Retention(RetentionPolicy.RUNTIME)
    10. @Target({TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR,
    11. LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE})
    12. public @interface MyAnnotations {
    13. MyAnnotation[] value() default {};
    14. }
    15. //3.使用示例
    16. //重复注解报错 //Duplicate annotation. The declaration of
    17. //'com.initit.java8.annotation.MyAnnotation'
    18. //does not have a valid java.lang.annotation.Repeatable annotation
    19. //@MyAnnotation
    20. @MyAnnotation
    21. //重复注解这样用
    22. @MyAnnotations({
    23. @MyAnnotation("注解1"),
    24. @MyAnnotation("注解2"),
    25. @MyAnnotation("注解3"),
    26. })
    27. public class AnnotationDemo {
    28. }

    jdk8提供的可重复注解

    • 增加元注解,标注为可以重复的注解@Repeatable(MyAnnotations.class),其中参数是一个数组类型的注解
    1. //1.写一个注解 并增加@Repeatable(MyAnnotations.class)
    2. @Repeatable(MyAnnotations.class)
    3. @Retention(RetentionPolicy.RUNTIME)
    4. @Target({TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR,
    5. LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE})
    6. public @interface MyAnnotation {
    7. String value() default "编程指南";
    8. }
    9. //2.建MyAnnotations.class
    10. @Retention(RetentionPolicy.RUNTIME)
    11. @Target({TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR,
    12. LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE})
    13. public @interface MyAnnotations {
    14. MyAnnotation[] value() default {};
    15. }
    16. //3. 使用
    17. @MyAnnotation("注解1"),
    18. @MyAnnotation("注解2"),
    19. @MyAnnotation("注解3"),
    20. public class AnnotationDemo {
    21. //jdk8还增加了 类型注解
    22. private @MyAnnotation String name;
    23. }