Annotation

 

    java api中的介绍html

Annotation Types Summary
Annotation Type Description
Documented
Indicates that annotations with a type are to be documented by javadoc and similar tools by default.
Inherited
Indicates that an annotation type is automatically inherited.
Native
Indicates that a field defining a constant value may be referenced from native code.
Repeatable
The annotation type  java.lang.annotation.Repeatable is used to indicate that the annotation type whose declaration it (meta-)annotates is  repeatable.
Retention
Indicates how long annotations with the annotated type are to be retained.
Target
Indicates the contexts in which an annotation type is applicable.

    

@Documentedjava

  是否将注解信息添加到javadoc中,默认添加api

@Inherited数组

   子类能够继承父类中的注解oracle

@Nativeapp

   常量值类型的字段可能会被从Native Code(二进制文件?)调用。函数

@Repeatablespa

  表示注解能够重复被使用code

@Retentionhtm

  表示注释存在的方式,也就是生命周期

  一、源码级别(SOURCE)

    Annotation只存在于源码之中,编译以后不会保留。

  二、字节码文件级别(CLASS)

    Annotation的默认级别,执行时不会加载到JVM虚拟机,存在于源码、字节码文件中。

  三、运行级别(RUNTIME)

    会加载到JVM虚拟机中,能够经过反射机制读取注解的信息。

  在Enum类RetentionPolicy中。

@Target

  注释在哪使用

  ElementType是枚举类型

public enum ElementType {
    /**标明该注解能够用于类、接口(包括注解类型)或enum声明*/
    TYPE,

    /** 标明该注解能够用于字段(域)声明,包括enum实例 */
    FIELD,

    /** 标明该注解能够用于方法声明 */
    METHOD,

    /** 标明该注解能够用于参数声明 */
    PARAMETER,

    /** 标明注解能够用于构造函数声明 */
    CONSTRUCTOR,

    /** 标明注解能够用于局部变量声明 */
    LOCAL_VARIABLE,

    /** 标明注解能够用于注解声明(应用于另外一个注解上)*/
    ANNOTATION_TYPE,

    /** 标明注解能够用于包声明 */
    PACKAGE,

    /**
     * 标明注解能够用于类型参数声明(1.8新加入)
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * 类型使用声明(1.8新加入)
     * @since 1.8
     */
    TYPE_USE
}

 

注解支持的元数据类型

  • 全部基本数据类型
  • String
  • Class
  • enum
  • Annotation
  • 上述类型的数组  

编译器对Annotation默认值的限制

  • 元素不能有不肯定的值
  • 默认值不能为null,能够为空字符串。

注解不支持继承,由于注解在编译以后默认是继承Annotation的

注解中的名为value的元数据能够快捷赋值

相关文章
相关标签/搜索