首先可以使用下面四個(gè)方法來訪問Annotation的信息
- ** <A extends Annotation> A getAnnotation(Class<A> annotationType) **
返回該程序元素上存在的、指定類型的注解,如果該類型注解不存在,則返回null - ** Annotation[] getAnnotations() **
返回該程序元素上存在的所有注解。 - ** boolean isAnnotationPresent(Class<? extends Annotation> annotationType) **
判斷該程序元素上是否包含指定類型的注解,存在則返回true,否則返回false. -
Annotation[] getDeclaredAnnotations()
返回直接存在于此元素上的所有注釋。與此接口中的其他方法不同,該方法將忽略繼承的注釋。(如果沒有注釋直接存在于此元素上,則返回長度為零的一個(gè)數(shù)組。)該方法的調(diào)用者可以隨意修改返回的數(shù)組;這不會(huì)對其他調(diào)用者返回的數(shù)組產(chǎn)生任何影響。
Java5.0定義了4個(gè)標(biāo)準(zhǔn)的meta-annotation類型對 annotation類型作說明
** 1. @Target **: 說明了Annotation所修飾的對象范圍取值(ElementType)有:
1.CONSTRUCTOR:用于描述構(gòu)造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部變量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述參數(shù)
7.TYPE:用于描述類、接口(包括注解類型) 或enum聲明
2. @Retention : 定義了該Annotation被保留的時(shí)間長短:某些Annotation僅出現(xiàn)在源代碼中,而被編譯器丟棄;而另一些卻被編譯在class文件中;編譯在class文件中的Annotation可能會(huì)被虛擬機(jī)忽略,而另一些在class被裝載時(shí)將被讀取(請注意并不影響class的執(zhí)行,因?yàn)锳nnotation與class在使用上是被分離的)取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在運(yùn)行時(shí)有效(即運(yùn)行時(shí)保留)
3. @Documented :用于描述其它類型的annotation應(yīng)該被作為被標(biāo)注的程序成員的公共API,因此可以被例如javadoc此類的工具文檔化。Documented是一個(gè)標(biāo)記注解,沒有成員。
4. @Inherited :是一個(gè)標(biāo)記注解,@Inherited闡述了某個(gè)被標(biāo)注的類型是被繼承的。如果一個(gè)使用了@Inherited修飾的annotation類型被用于一個(gè)class,則這個(gè)annotation將被用于該class的子類。注意:@Inherited annotation類型是被標(biāo)注過的class的子類所繼承。類并不從它所實(shí)現(xiàn)的接口繼承annotation,方法并不從它所重載的方法繼承annotation。當(dāng)@Inherited annotation類型標(biāo)注的annotation的Retention是RetentionPolicy.RUNTIME,則反射API增強(qiáng)了這種繼承性。如果我們使用java.lang.reflect去查詢一個(gè)@Inherited annotation類型的annotation時(shí),反射代碼檢查將展開工作:檢查class和其父類,直到發(fā)現(xiàn)指定的annotation類型被發(fā)現(xiàn),或者到達(dá)類繼承結(jié)構(gòu)的頂層。
注解可以支持的數(shù)據(jù)類型
1.所有基本數(shù)據(jù)類型(int,float,boolean,byte,double,char,long,short)
2.String類型
3.Class類型
4.enum類型
5.Annotation類型
6.以上所有類型的數(shù)組
Annotation類型里面的參數(shù),方法設(shè)定
只能用public或默認(rèn)(default)這兩個(gè)訪問權(quán)修飾.例如,String value();這里把方法設(shè)為defaul默認(rèn)類型
參數(shù)成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基本數(shù)據(jù)類型和 String,Enum,Class,annotations等數(shù)據(jù)類型,以及這一些類型的數(shù)組.例如,String value();這里的參數(shù)成員就為String;
如果只有一個(gè)參數(shù)成員,最好把參數(shù)名稱設(shè)為"value",后加小括號.
在每一個(gè)注解中每一個(gè)方法其實(shí)就是一個(gè)變量
如下是一個(gè)注解使用例子
//定義了一個(gè)學(xué)生專業(yè)的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentMajor {
public enum Major {計(jì)算機(jī), 軟件, 經(jīng)管}//專業(yè)的枚舉
Major studentMajor() default Major.經(jīng)管;//學(xué)生的專業(yè)
}
//學(xué)生名字注解類
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentName {
String value() default "";
}
//學(xué)生信息注解類
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentData {
public int age() default -1;
public String address() default "";
public int height() default -1;
}
//學(xué)生類
public class Student {
@StudentName("luoweidong")
private String studentName;
@StudentMajor(studentMajor = StudentMajor.Major.計(jì)算機(jī))
private String studentMajor;
@StudentData(age = 18, address = "江西", height = 170)
private String studentData;
}
//注解處理類
public class StudentInfoUtils {
public static void showData(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(StudentName.class)) {
StudentName studentName = field.getAnnotation(StudentName.class);
Log.d("lwd", studentName.value());
}
if (field.isAnnotationPresent(StudentMajor.class)) {
StudentMajor studentMajor = field.getAnnotation(StudentMajor.class);
Log.d("lwd", studentMajor.studentMajor() + "");
}
if (field.isAnnotationPresent(StudentData.class)) {
StudentData studentData = field.getAnnotation(StudentData.class);
Log.d("lwd", studentData.age() + studentData.address() + studentData.height());
}
}
}
}
//測試代碼
public class Test {
public static void main(String[] args) {
StudentInfoUtils.showData(Student.class);
}
}