JAVA進階-註解
註解元數據分為4部分分別為Target,Documented,Inherited,Retention:
字段,類,返回值等等;
聲明:
/** * @author Lean @date:2014-10-13 */ @Target(ElementType.METHOD) public @interface WorkInProgress {}
應用:
/** * @author Lean @date:2014-10-13 */ public class AnnotationSample { //當在字段中使用時:The annotation @WorkInProgress is disallowed for this location [email protected]
Retention>設置註解可見性;使用到RetentionPolicy枚舉
RetentionPolicy.SOURCE>>編譯器可見,但對.class文件和執行時不可見;
RetentionPolicy.CLASS>>默認工具可見,對.class文件可見,但執行不可見;
RetentionPolicy.RUNTIME>>執行時可見;不會被.class文件所知,在執行時告訴JVM的值;
下面樣例為執行時內省檢查,當一個元註解須要多個限定值的時,必須使用{}和逗號隔開,
[email protected]