Java註解

Sample

public @interface Test {
     String value();//可以省略屬性名稱
     int timeout() default 0;
}

ElementType

@Target(ElementType.METHOD)
public @interface Test{
}


public enum ElementType {
    TYPE,                  // 用於類別、介面、列舉等
    FIELD,                 // 用於資料成員
    METHOD,                // 用於方法
    PARAMETER,             // 用於方法上的參數
    CONSTRUCTOR,           // 用於建構式
    LOCAL_VARIABLE,        // 用於區域變數
    ANNOTATION_TYPE,       // 用於標註型態
    PACKAGE,               // 適用套件
    TYPE_PARAMETER,        // 用於泛型宣告,JDK8新增
    TYPE_USE               // 用於各種型態,JDK8新增
}

RetentionPolicy

@Retention(RetentionPolicy.RUNTIME)
public @interface Test{
}

public enum RetentionPolicy {
    SOURCE,//Annotations are to be discarded by the compiler.

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     */
    RUNTIME
}

Documented

@Documented
public @interface Test{
}

產生的JavaDoc,包括註解

Inherited

@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。 
@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它的方法继承annotation。

Repeatable

同個元素可以指定同annotation多次