注釋將補充信息嵌入源文件中。注釋不會更改程序的語義。
通過基于接口的機制創(chuàng)建注釋。
以下代碼聲明了一個名為 MyAnno
的注釋:
// A simple annotation type. @interface MyAnno { String str(); int val(); }
@
在關(guān)鍵字接口之前。所有注釋僅具有方法聲明。注釋不能包含extends子句。
所有注釋類型自動擴展注釋接口。注釋接口是所有注釋的超級接口。注釋接口在java.lang.annotation包中聲明。
任何類型的聲明都可以具有與其相關(guān)聯(lián)的注釋。例如,可以注釋類,方法,字段,參數(shù)和枚舉常量。注釋也可以注釋。在所有情況下,注釋在聲明的其余部分之前。
應(yīng)用注釋時,您可以向其成員提供值。例如,下面是一個應(yīng)用于類的 MyAnno
示例:
// Annotate a method. @MyAnno(str = "Annotation Example", val = 100) public class Main{}
此注釋與類Main鏈接。
保留策略確定在什么時候丟棄注釋。
Java定義了三個這樣的策略:SOURCE,CLASS和RUNTIME。
通過使用Java的內(nèi)置注釋之一為注釋指定保留策略:
@Retention
其一般形式如下所示:
@Retention(retention-policy)
retention-policy必須是 SOURCE
, CLASS
和 RUNTIME
之一。
默認策略為 CLASS
。
以下代碼將保留策略指定為RUNTIME。
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str(); int val(); }
如何通過使用反射在運行時獲取注釋。
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; // An annotation type declaration. @Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str(); int val(); } public class Main { @MyAnno(str = "Annotation Example", val = 100) public static void myMeth() { Main ob = new Main(); try { Class c = ob.getClass(); Method m = c.getMethod("myMeth"); MyAnno anno = m.getAnnotation(MyAnno.class); System.out.println(anno.str() + " " + anno.val()); } catch (NoSuchMethodException exc) { System.out.println("Method Not Found."); } } public static void main(String args[]) { myMeth(); } }
上面的代碼生成以下結(jié)果。
您可以獲取具有與某個關(guān)聯(lián)的RUNTIME保留的所有注釋項目,通過調(diào)用該項目的getAnnotations()。
它有這種一般形式:
Annotation[ ] getAnnotations( )
以下代碼顯示了如何從類中獲取注釋。
import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str(); int val(); } @Retention(RetentionPolicy.RUNTIME) @interface What { String description(); } @What(description = "An annotation") @MyAnno(str = "Meta2", val = 99) public class Main { @What(description = "test method") @MyAnno(str = "Testing", val = 100) public static void myMeth() throws Exception { Main ob = new Main(); Annotation annos[] = ob.getClass().getAnnotations(); System.out.println("All annotations for Meta2:"); for (Annotation a : annos) { System.out.println(a); } Method m = ob.getClass().getMethod("myMeth"); annos = m.getAnnotations(); for (Annotation a : annos) { System.out.println(a); } } public static void main(String args[]) throws Exception { myMeth(); } }
上面的代碼生成以下結(jié)果。
您可以給注釋成員默認值。如果在應(yīng)用注釋時未指定值,那么將使用這些默認值。
通過向成員聲明添加默認子句來指定默認值。
它有這種一般形式:
type member( ) default value;
這里是 @MyAnno
重寫為包括默認值:
@Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str() default "Testing"; int val() default 9000; }
如果需要,可以給出任一值或兩者。因此,以下是可以使用@MyAnno的四種方式:
@MyAnno() // both str and val default @MyAnno(str = "string") // val defaults @MyAnno(val = 100) // str defaults @MyAnno(str = "Testing", val = 100) // no defaults
以下程序演示了在注釋中使用默認值。
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str() default "Testing"; int val() default 1; } public class Main { @MyAnno() public static void myMeth() throws Exception{ Main ob = new Main(); Class c = ob.getClass(); Method m = c.getMethod("myMeth"); MyAnno anno = m.getAnnotation(MyAnno.class); System.out.println(anno.str() + " " + anno.val()); } public static void main(String args[]) throws Exception{ myMeth(); } }
上面的代碼生成以下結(jié)果。
更多建議: