(1)定義Annotation類型
l 使用@interface聲明Annotation類型
public @interface InProgress {
}
l 使用Annotation類型
public class TestAnnotation {
@InProcess
public void test() {
}
}
l 如果Annotation類型和使用它的類不在相同的包中,可以import Annotation類型,以便直接使用 @InProgress
(2)添加成員
l Annotation類型可以有成員變量,以提供有用的信息
l 定義數(shù)據(jù)成員不需要定義getter和setter方法,只需要定義一個以成員名稱命名的方法,并指定返回類型為需要的數(shù)據(jù)類型
l 簡單的例子:
public @interface TODO {
String value();
}
l 使用帶成員的Annotation類型:
public class TestAnnotation {
@InProcess
@TODO("Need to finish this method later")
public void test() {
}
}
(3)設(shè)置缺省值
l 要為Annotation類型的成員設(shè)置缺省值,需要在聲明成員時使用default關(guān)鍵字:
public @interface GroupTODO {
public enum Severity {
CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION
};
Severity severity() default Severity.IMPORTANT;
String item();
String assignedTo();
String dateAssigned();
}
l 當(dāng)然,缺省值的類型必須與成員變量聲明的類型完全相同
l 下面是使用缺省值的例子:
public class TestAnnotation {
@InProcess
@GroupTODO(
item="Need to finish this method later",
assignedTo="nelson_tu",
dateAssigned="2005/02/05"
)
public void test() {
}
}
l 下面是改寫缺省值的例子:
public class TestAnnotation {
@InProcess
//@TODO("Need to finish this method later")
@GroupTODO(
severity=GroupTODO.Severity.DOCUMENTATION,
item="Need to finish this method later",
assignedTo="nelson_tu",
dateAssigned="2005/02/05"
)
public void test() {
}
}
2、元Annotation
l 元Annotation就是Annotation的Annotation,JDK5提供了4種預(yù)定義的元Annotation
(1)@Target
l @Target指定Annotation類型可以應(yīng)用的程序元素,以便在其它程序元素中誤用Annotation類型
l 程序元素的類型由java.lang.annotation.ElementType枚舉類定義:
package java.lang.annotation;
public enum ElementType {
TYPE, // Class, interface, or enum (but not annotation)
FIELD, // Field (including enumerated values)
METHOD, // Method (does not include constructors)
PARAMETER, // Method parameter
CONSTRUCTOR, // Constructor
LOCAL_VARIABLE, // Local variable or catch clause
ANNOTATION_TYPE, // Annotation Types (meta-annotations)
PACKAGE // Java package
}
l 下面是使用@Target的例子:
@Target({ElementType.TYPE,
ElementType.METHOD,
ElementType.CONSTRUCTOR,
ElementType.ANNOTATION_TYPE})
public @interface TODO {
String value();
}
(2)@Retention
l @Retention和 Java 編譯器處理Annotation類型的方式有關(guān)
l 這些方式由java.lang.annotation.RetentionPolicy
枚舉類定義:
package java.lang.annotation;
public enum RetentionPolicy {
SOURCE, // Annotation is discarded by the compiler
CLASS, // Annotation is stored in the class file, but ignored by the VM
RUNTIME // Annotation is stored in the class file and read by the VM
}
l 使用@Retention的例子參看后面的@Documented
(3)@Documented
l @Documented指明需要在Javadoc中包含Annotation(缺省是不包含的)
l 下面是一個使用@Documented的例子:
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface InProcess {
}
l 使用@Documented的一個技巧就是指定保持性策略為RetentionPolicy.RUNTIME:這樣,Annotation就會保留在編譯后的類文件中并且由虛擬機加載,然后Javadoc就可以抽取出Annotation,添加到類的HTML文檔中
(4)@Inherited
l @Inherited最復(fù)雜、使用最少、也最容易造成混淆的一個
l 假設(shè)使用@InProgress 標(biāo)記一個正在開發(fā)的類,只要正確應(yīng)用@Documented,Annotation信息就會出現(xiàn)在Javadoc中;現(xiàn)在要編寫一個新類,擴展那個正在開發(fā)的類,那么使用子類,或者查看它的文檔,根本沒法表明還有什么地方?jīng)]有完成;而本來是希望@InProgress 的Annotation信息會被帶到子類中,這就需要使用@Inherited了
l 下面是這樣的例子:
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface InProcess {
}