首先可以使用下面四個方法來訪問Annotation的信息
- ** <A extends Annotation> A getAnnotation(Class<A> annotationType) **
返回該程序元素上存在的形入、指定類型的注解全跨,如果該類型注解不存在,則返回null - ** Annotation[] getAnnotations() **
返回該程序元素上存在的所有注解亿遂。 - ** boolean isAnnotationPresent(Class<? extends Annotation> annotationType) **
判斷該程序元素上是否包含指定類型的注解浓若,存在則返回true,否則返回false. -
Annotation[] getDeclaredAnnotations()
返回直接存在于此元素上的所有注釋蛇数。與此接口中的其他方法不同挪钓,該方法將忽略繼承的注釋。(如果沒有注釋直接存在于此元素上耳舅,則返回長度為零的一個數組碌上。)該方法的調用者可以隨意修改返回的數組;這不會對其他調用者返回的數組產生任何影響浦徊。
Java5.0定義了4個標準的meta-annotation類型對 annotation類型作說明
** 1. @Target **: 說明了Annotation所修飾的對象范圍取值(ElementType)有:
1.CONSTRUCTOR:用于描述構造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部變量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述參數
7.TYPE:用于描述類馏予、接口(包括注解類型) 或enum聲明
2. @Retention : 定義了該Annotation被保留的時間長短:某些Annotation僅出現(xiàn)在源代碼中后雷,而被編譯器丟棄蜀肘;而另一些卻被編譯在class文件中;編譯在class文件中的Annotation可能會被虛擬機忽略囤萤,而另一些在class被裝載時將被讀却砍觥(請注意并不影響class的執(zhí)行蚯妇,因為Annotation與class在使用上是被分離的)取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在運行時有效(即運行時保留)
3. @Documented :用于描述其它類型的annotation應該被作為被標注的程序成員的公共API,因此可以被例如javadoc此類的工具文檔化暂筝。Documented是一個標記注解箩言,沒有成員。
4. @Inherited :是一個標記注解焕襟,@Inherited闡述了某個被標注的類型是被繼承的陨收。如果一個使用了@Inherited修飾的annotation類型被用于一個class,則這個annotation將被用于該class的子類鸵赖。注意:@Inherited annotation類型是被標注過的class的子類所繼承务漩。類并不從它所實現(xiàn)的接口繼承annotation,方法并不從它所重載的方法繼承annotation它褪。當@Inherited annotation類型標注的annotation的Retention是RetentionPolicy.RUNTIME饵骨,則反射API增強了這種繼承性。如果我們使用java.lang.reflect去查詢一個@Inherited annotation類型的annotation時茫打,反射代碼檢查將展開工作:檢查class和其父類居触,直到發(fā)現(xiàn)指定的annotation類型被發(fā)現(xiàn)妖混,或者到達類繼承結構的頂層。
注解可以支持的數據類型
1.所有基本數據類型(int,float,boolean,byte,double,char,long,short)
2.String類型
3.Class類型
4.enum類型
5.Annotation類型
6.以上所有類型的數組
Annotation類型里面的參數轮洋,方法設定
只能用public或默認(default)這兩個訪問權修飾.例如,String value();這里把方法設為defaul默認類型
參數成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基本數據類型和 String,Enum,Class,annotations等數據類型,以及這一些類型的數組.例如,String value();這里的參數成員就為String;
如果只有一個參數成員,最好把參數名稱設為"value",后加小括號.
在每一個注解中每一個方法其實就是一個變量
如下是一個注解使用例子
//定義了一個學生專業(yè)的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentMajor {
public enum Major {計算機, 軟件, 經管}//專業(yè)的枚舉
Major studentMajor() default Major.經管;//學生的專業(yè)
}
//學生名字注解類
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentName {
String value() default "";
}
//學生信息注解類
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentData {
public int age() default -1;
public String address() default "";
public int height() default -1;
}
//學生類
public class Student {
@StudentName("luoweidong")
private String studentName;
@StudentMajor(studentMajor = StudentMajor.Major.計算機)
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);
}
}