java元數(shù)據(jù)的學(xué)習(xí)
Annotation簡(jiǎn)介:??????????Annotation提供一種機(jī)制,將程序中元素(如類、方法、屬性等)和元數(shù)據(jù)聯(lián)系起來(lái)。這樣編譯器可以將元數(shù)據(jù)保存的class文件中。代碼分析工具就可以使用這些元數(shù)據(jù)執(zhí)行的額外任務(wù)。注釋采用“at”標(biāo)記形式 ( @ ),后面是注釋名稱。
???????自定義?annotationDebug.java
/**
?*?
?*/
package?annotation.test;
import?java.lang.annotation.Documented;
import?java.lang.annotation.Inherited;
import?java.lang.annotation.Retention;
import?java.lang.annotation.RetentionPolicy;
import?java.lang.annotation.ElementType;
import?java.lang.annotation.Target;
/**
?*?@author?windfree
?*
?*/
????/*
?????*?annotation類型是一種接口,能夠通過(guò)java反射API的方式提供對(duì)其信息的訪問(wèn)
?????*/
????/*
?????*?Documented?產(chǎn)生Java?Doc文件,這次可以看到文件中包括了@Debug的信息
?????*/
????/*
?????*?限定?annotation?使用對(duì)象?-?Target?
?????*?在定義annotation時(shí),使用java.lang.annotation.Target可以定義annotation使用的時(shí)機(jī)。
?????*/
????/*
?????*?@Documented?Java?Doc文件
?????*/
????/*
?????*?
?????*?public?enum?ElementType?{
?????*???TYPE,?//?適用?class,?interface,?enum
?????*???FIELD,?//?適用?field
?????*???METHOD,?//?適用?method
?????*???PARAMETER,?//?適用?method?上之?parameter
?????*???CONSTRUCTOR,?//?適用?constructor
?????*???LOCAL_VARIABLE,?//?適用區(qū)域變量
?????*???ANNOTATION_TYPE,?//?適用?annotation?型態(tài)
?????*???PACKAGE?//?適用?package
?????*???}?
?????*/
????/*
?????*?Retention?在你自定義的形態(tài)中,指定編譯器如何處理自定義的annotation
?????*?告知編譯器如何處理?annotaion?-?Retention?
?????*/
????/*
?????*?RetentionPolicy
?????*?????????SOURCE????????編譯器處理完Annotation信息后就沒(méi)事了
?????*?????????CLASS????????編譯器將Annotation儲(chǔ)存于class檔中,預(yù)設(shè)
?????*?????????RUNTIME????????編譯器將Annotation儲(chǔ)存于class檔中,可由VM讀入
?????*?????????????????????使用java設(shè)計(jì)的一個(gè)程序代碼工具,從VM中讀出Annotation信息,已在分析程序中使用。
?????*?????????????????????搭配Reflection機(jī)制,就可以實(shí)現(xiàn)。
?????*?????????
?????*?????????J2SE?5.0中新增了java.lang.reflect.AnnotatedElement這個(gè)接口,當(dāng)中定義有四個(gè)方法:
?????*?????????????????public?Annotation?getAnnotation(Class?annotationType);
?????*?????????????????public?Annotation[]?getAnnotations();
?????*?????????????????public?Annotation[]?getDeclaredAnnotations();
?????*?????????????????public?boolean?isAnnotationPresent(Class?annotationType);
?????*?????????Class、Constructor、Field、Method、Package等類別,都實(shí)作了?AnnotatedElement這個(gè)接口,
?????*?????????所以您可以從這些類別的實(shí)例上,分別取得標(biāo)示于其上的Annotation與其信息,
?????*?????????如果?RetentionPolicy為RUNTIME的話。
?????*/
????/*
?????*?@Inherited?子類是否繼承父類的?annotation?
?????*/
@Documented
@Target({ElementType.CONSTRUCTOR,?ElementType.METHOD})//指定目標(biāo)
@Retention(RetentionPolicy.RUNTIME)//設(shè)置保持性
@Inherited
public?@interface?Debug?{
????//定義了value()方法,編譯器在編譯時(shí)會(huì)自動(dòng)幫您產(chǎn)生一個(gè)value的變量成員,接著在使用Debug?Annotation時(shí)要指定值
????//注釋類型的數(shù)據(jù)成員被設(shè)置成使用有限信息進(jìn)行工作
????//定義數(shù)據(jù)成員不需要分別定義訪問(wèn)和修改的方法,只需定義一個(gè)方法,以數(shù)據(jù)成員的名稱命名它,數(shù)據(jù)類型應(yīng)該是該方法返回值的類型.
????String?value()default?"windfree";//默認(rèn)值的類型必需與成員變量的申明類型一置
????String?name();
}
?*?
?*/
package?annotation.test;
import?java.lang.annotation.Documented;
import?java.lang.annotation.Inherited;
import?java.lang.annotation.Retention;
import?java.lang.annotation.RetentionPolicy;
import?java.lang.annotation.ElementType;
import?java.lang.annotation.Target;
/**
?*?@author?windfree
?*
?*/
????/*
?????*?annotation類型是一種接口,能夠通過(guò)java反射API的方式提供對(duì)其信息的訪問(wèn)
?????*/
????/*
?????*?Documented?產(chǎn)生Java?Doc文件,這次可以看到文件中包括了@Debug的信息
?????*/
????/*
?????*?限定?annotation?使用對(duì)象?-?Target?
?????*?在定義annotation時(shí),使用java.lang.annotation.Target可以定義annotation使用的時(shí)機(jī)。
?????*/
????/*
?????*?@Documented?Java?Doc文件
?????*/
????/*
?????*?
?????*?public?enum?ElementType?{
?????*???TYPE,?//?適用?class,?interface,?enum
?????*???FIELD,?//?適用?field
?????*???METHOD,?//?適用?method
?????*???PARAMETER,?//?適用?method?上之?parameter
?????*???CONSTRUCTOR,?//?適用?constructor
?????*???LOCAL_VARIABLE,?//?適用區(qū)域變量
?????*???ANNOTATION_TYPE,?//?適用?annotation?型態(tài)
?????*???PACKAGE?//?適用?package
?????*???}?
?????*/
????/*
?????*?Retention?在你自定義的形態(tài)中,指定編譯器如何處理自定義的annotation
?????*?告知編譯器如何處理?annotaion?-?Retention?
?????*/
????/*
?????*?RetentionPolicy
?????*?????????SOURCE????????編譯器處理完Annotation信息后就沒(méi)事了
?????*?????????CLASS????????編譯器將Annotation儲(chǔ)存于class檔中,預(yù)設(shè)
?????*?????????RUNTIME????????編譯器將Annotation儲(chǔ)存于class檔中,可由VM讀入
?????*?????????????????????使用java設(shè)計(jì)的一個(gè)程序代碼工具,從VM中讀出Annotation信息,已在分析程序中使用。
?????*?????????????????????搭配Reflection機(jī)制,就可以實(shí)現(xiàn)。
?????*?????????
?????*?????????J2SE?5.0中新增了java.lang.reflect.AnnotatedElement這個(gè)接口,當(dāng)中定義有四個(gè)方法:
?????*?????????????????public?Annotation?getAnnotation(Class?annotationType);
?????*?????????????????public?Annotation[]?getAnnotations();
?????*?????????????????public?Annotation[]?getDeclaredAnnotations();
?????*?????????????????public?boolean?isAnnotationPresent(Class?annotationType);
?????*?????????Class、Constructor、Field、Method、Package等類別,都實(shí)作了?AnnotatedElement這個(gè)接口,
?????*?????????所以您可以從這些類別的實(shí)例上,分別取得標(biāo)示于其上的Annotation與其信息,
?????*?????????如果?RetentionPolicy為RUNTIME的話。
?????*/
????/*
?????*?@Inherited?子類是否繼承父類的?annotation?
?????*/
@Documented
@Target({ElementType.CONSTRUCTOR,?ElementType.METHOD})//指定目標(biāo)
@Retention(RetentionPolicy.RUNTIME)//設(shè)置保持性
@Inherited
public?@interface?Debug?{
????//定義了value()方法,編譯器在編譯時(shí)會(huì)自動(dòng)幫您產(chǎn)生一個(gè)value的變量成員,接著在使用Debug?Annotation時(shí)要指定值
????//注釋類型的數(shù)據(jù)成員被設(shè)置成使用有限信息進(jìn)行工作
????//定義數(shù)據(jù)成員不需要分別定義訪問(wèn)和修改的方法,只需定義一個(gè)方法,以數(shù)據(jù)成員的名稱命名它,數(shù)據(jù)類型應(yīng)該是該方法返回值的類型.
????String?value()default?"windfree";//默認(rèn)值的類型必需與成員變量的申明類型一置
????String?name();
}
在程序中使用自定義的annotation
???TestDebug.java
???
??????
package?annotation.test;
public?class?TestDebug?{
????@Debug(
???????????????name?=?"zgliu"
????????????)
?????public?void?doSomething()?{
????????
????????}
}
public?class?TestDebug?{
????@Debug(
???????????????name?=?"zgliu"
????????????)
?????public?void?doSomething()?{
????????
????????}
}
使用java中反射API讀取class中的元數(shù)據(jù)的信息
??????DebugTool.java:
??????
?????????





































???學(xué)習(xí)中的一個(gè)完整的例子。
# re: java元數(shù)據(jù)的學(xué)習(xí) 2007-05-04 15:07 BeanSoft 回復(fù) 更多評(píng)論
學(xué)習(xí)了! 支持一下, 尤其是如何解析 Annotation 部分的代碼. 受益了!