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的元數據能夠快捷賦值

相關文章
相關標籤/搜索