1. * 元註解有:@Target,@Retention,@Documented,@Inherited
2. *
3. * @Target 表示該註解用於什麼地方,可能的 ElemenetType 參數包括:
4. * ElemenetType.CONSTRUCTOR 構造器聲明
5. * ElemenetType.FIELD 域聲明(包括 enum 實例)
6. * ElemenetType.LOCAL_VARIABLE 局部變量聲明
7. * ElemenetType.METHOD 方法聲明
8. * ElemenetType.PACKAGE 包聲明
9. * ElemenetType.PARAMETER 參數聲明
10. * ElemenetType.TYPE 類,接口(包括註解類型)或enum聲明
11. *
12. * @Retention 表示在什麼級別保存該註解信息。可選的 RetentionPolicy 參數包括:
13. * RetentionPolicy.SOURCE 註解將被編譯器丟棄
14. * RetentionPolicy.CLASS 註解在class文件中可用,但會被VM丟棄
15. * RetentionPolicy.RUNTIME VM將在運行期也保留註釋,因此可以通過反射機制讀取註解的信息。
16. *
17. * @Documented 將此註解包含在 javadoc 中
18. *
19. * @Inherited 允許子類繼承父類中的註解
|
package lighter.javaeye.com;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)//這個標註應用於類
@Retention(RetentionPolicy.RUNTIME)//標註會一直保留到運行時
@Documented//將此註解包含在javadoc中
public @interface Description {
String value();
}
|
package lighter.javaeye.com;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//注意這裏的@Target與@Description裏的不同,參數成員也不同
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Name {
String originate();
String community();
}
|
package lighter.javaeye.com;
@Description(value="javaeye,做最棒的軟件開發交流社區")
public class JavaEyer {
@Name(originate="創始人:robbin",community="javaEye")
public String getName()
{
return null;
}
@Name(originate="創始人:江南白衣",community="springside")
public String getName2()
{
return "借用兩位的id一用,寫這一個例子,請見諒!";
}
}
說明:其中標註“@Description(value="javaeye,做最棒的軟件開發交流社區")”,可以寫成“@Description("javaeye,做最棒的軟件開發交流社區") ”,結果也是一樣的。因爲Description標註定義的時候其參數(或者說屬性)爲value。而value比較特殊,它在被指定參數的時候可以不用顯示的寫出來。當然如果定義的時候參數名不是value而是其它的比如des,那麼使用註解的時候,必須顯示寫出參數名,然後再賦值:@Description(Des=”xxx”)
|
package lighter.javaeye.com;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
public class TestAnnotation {
/**
* author lighter
* 說明:具體關天Annotation的API的用法請參見javaDoc文檔
*/
public static void main(String[] args) throws Exception {
String CLASS_NAME = "lighter.javaeye.com.JavaEyer";
Class test = Class.forName(CLASS_NAME);
Method[] method = test.getMethods();
boolean flag = test.isAnnotationPresent(Description.class);
if(flag)
{
Description des = (Description)test.getAnnotation(Description.class);//注意:是類.class.getAnnotation()
System.out.println("描述:"+des.value());
System.out.println("-----------------");
}
//把JavaEyer這一類有利用到@Name的全部方法保存到Set中去
Set<Method> set = new HashSet<Method>();
for(int i=0;i<method.length;i++)
{
boolean otherFlag = method[i].isAnnotationPresent(Name.class); //注意是method[i],不是類.class
if(otherFlag) set.add(method[i]);
}
for(Method m: set)
{
Name name = m.getAnnotation(Name.class);//注意是method.getAnnotation,不是類.class.getAnnotation
System.out.println(name.originate());
System.out.println("創建的社區:"+name.community());
}
}
}
|