美文网首页
JAVA Annotation Types Summary

JAVA Annotation Types Summary

作者: googoler | 来源:发表于2020-05-18 11:13 被阅读0次

    java.lang 元注解

    1. Documented
      If the annotation @Documented is present on the declaration of an annotation type A, then any @A annotation on an element is considered part of the element's public contract.
      @Document:说明该注解将被包含在javadoc中
      1. Documented 注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中.
    2. Inherited
      Indicates that an annotation type is automatically inherited.
      @Inherited
      1. 说明子类可以继承父类中的该注解
    3. Native
      Indicates that a field defining a constant value may be referenced from native code.
      @Native (jdk1.8提供)
      1. 指定字段是一个常量,其值引用native code。
    4. Repeatable
      The annotation type java.lang.annotation.Repeatable is used to indicate that the annotation type whose declaration it (meta-)annotates is repeatable.
      @Repeatable (jdk1.8提供)
      1. 注解上可以使用重复注解,即可以在一个地方可以重复使用同一个注解,像spring中的包扫描注解就使用了这个。
    5. Retention
      Indicates how long annotations with the annotated type are to be retained.
      @Retention:(保留)注解说明,这种类型的注解会被保留到那个阶段.
      有三个值:
      1. RetentionPolicy.SOURCE:这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含;
      2. RetentionPolicy.CLASS:这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得;
      3. RetentionPolicy.RUNTIME:这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。
    6. Target
      Indicates the contexts in which an annotation type is applicable.
      @Target:注解的作用目标
      1. @Target(ElementType.TYPE)——接口、类、枚举、注解
      2. @Target(ElementType.FIELD)——字段、枚举的常量
      3. @Target(ElementType.METHOD)——方法
      4. @Target(ElementType.PARAMETER)——方法参数
      5. @Target(ElementType.CONSTRUCTOR) ——构造函数
      6. @Target(ElementType.LOCAL_VARIABLE)——局部变量
      7. @Target(ElementType.ANNOTATION_TYPE)——注解
      8. @Target(ElementType.PACKAGE)——包

    注意事项(一些约束)

    • @Repeatable 所声明的注解,其元注解@Target的使用范围要比@Repeatable的值声明的注解中的@Target的范围要大或相同,否则编译器错误,显示@Repeatable值所声明的注解的元注解@Target不是@Repeatable声明的注解的@Target的子集
    • @Repeatable注解声明的注解的元注解@Retention的周期要比@Repeatable的值指向的注解的@Retention得周期要小或相同。
    • 周期长度为 SOURCE(源码) < CLASS (字节码) < RUNTIME(运行)

    java.lang 标准注解

    1. Deprecated
      A program element annotated @Deprecated is one that programmers are discouraged from using.
    2. FunctionalInterface
      An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.
    3. Override
      Indicates that a method declaration is intended to override a method declaration in a supertype.
    4. SafeVarargs
      A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter.
    5. SuppressWarnings
      Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).

    相关文章

      网友评论

          本文标题:JAVA Annotation Types Summary

          本文链接:https://www.haomeiwen.com/subject/hjtuohtx.html