轉載自:http://www.iteye.com/topic/400085
註解(Annotation) 為我們在代碼中天界資訊提供了一種形式化的方法,是我們可以在稍後
某個時刻方便地使用這些資料(通過 解析註解 來使用這些資料)。
註解的文法比較簡單,除了@符號的使用以外,它基本上與java的固有文法一致,java內建了三種
註解,定義在java.lang包中。
@Override 表示當前方法是覆蓋父類的方法。
@Deprecated 表示當前元素是不贊成使用的。
@SuppressWarnings 表示關閉一些不當的編譯器警告資訊。
下面是一個定義註解的執行個體
Java代碼
- package Test_annotation;
-
- import java.lang.annotation.Documented;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.Target;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.RetentionPolicy;
-
- /*
- * 元註解@Target,@Retention,@Documented,@Inherited
- *
- * @Target 表示該註解用於什麼地方,可能的 ElemenetType 參數包括:
- * ElemenetType.CONSTRUCTOR 構造器聲明
- * ElemenetType.FIELD 域聲明(包括 enum 執行個體)
- * ElemenetType.LOCAL_VARIABLE 局部變數聲明
- * ElemenetType.METHOD 方法聲明
- * ElemenetType.PACKAGE 包聲明
- * ElemenetType.PARAMETER 參數聲明
- * ElemenetType.TYPE 類,介面(包括註解類型)或enum聲明
- *
- * @Retention 表示在什麼層級儲存該註解資訊。可選的 RetentionPolicy 參數包括:
- * RetentionPolicy.SOURCE 註解將被編譯器丟棄
- * RetentionPolicy.CLASS 註解在class檔案中可用,但會被VM丟棄
- * RetentionPolicy.RUNTIME VM將在運行期也保留注釋,因此可以通過反射機制讀取註解的資訊。
- *
- * @Documented 將此註解包含在 javadoc 中
- *
- * @Inherited 允許子類繼承父類中的註解
- *
- */
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- /*
- * 定義註解 Test
- * 註解中含有兩個元素 id 和 description
- * description 元素 有預設值 "no description"
- */
- public @interface Test {
- public int id();
- public String description() default "no description";
- }
package Test_annotation;import java.lang.annotation.Documented;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.RetentionPolicy;/* * 元註解@Target,@Retention,@Documented,@Inherited * * @Target 表示該註解用於什麼地方,可能的 ElemenetType 參數包括: * ElemenetType.CONSTRUCTOR 構造器聲明 * ElemenetType.FIELD 域聲明(包括 enum 執行個體) * ElemenetType.LOCAL_VARIABLE 局部變數聲明 * ElemenetType.METHOD 方法聲明 * ElemenetType.PACKAGE 包聲明 * ElemenetType.PARAMETER 參數聲明 * ElemenetType.TYPE 類,介面(包括註解類型)或enum聲明 * * @Retention 表示在什麼層級儲存該註解資訊。可選的 RetentionPolicy 參數包括: * RetentionPolicy.SOURCE 註解將被編譯器丟棄 * RetentionPolicy.CLASS 註解在class檔案中可用,但會被VM丟棄 * RetentionPolicy.RUNTIME VM將在運行期也保留注釋,因此可以通過反射機制讀取註解的資訊。 * * @Documented 將此註解包含在 javadoc 中 * * @Inherited 允許子類繼承父類中的註解 * */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited/* * 定義註解 Test * 註解中含有兩個元素 id 和 description * description 元素 有預設值 "no description" */public @interface Test {public int id();public String description() default "no description";}
下面是一個使用註解 和 解析註解的執行個體
Java代碼
- package Test_annotation;
-
- import java.lang.reflect.Method;
-
- public class Test_1 {
- /*
- * 被註解的三個方法
- */
- @Test(id = 1, description = "hello method_1")
- public void method_1() {
- }
-
- @Test(id = 2)
- public void method_2() {
- }
-
- @Test(id = 3, description = "last method")
- public void method_3() {
- }
-
- /*
- * 解析註解,將Test_1類 所有被註解方法 的資訊列印出來
- */
- public static void main(String[] args) {
- Method[] methods = Test_1.class.getDeclaredMethods();
- for (Method method : methods) {
- /*
- * 判斷方法中是否有指定註解類型的註解
- */
- boolean hasAnnotation = method.isAnnotationPresent(Test.class);
- if (hasAnnotation) {
- /*
- * 根據註解類型返回方法的指定類型註解
- */
- Test annotation = method.getAnnotation(Test.class);
- System.out.println("Test( method = " + method.getName()
- + " , id = " + annotation.id() + " , description = "
- + annotation.description() + " )");
- }
- }
- }
-
- }
package Test_annotation;import java.lang.reflect.Method;public class Test_1 {/* * 被註解的三個方法 */@Test(id = 1, description = "hello method_1")public void method_1() {}@Test(id = 2)public void method_2() {}@Test(id = 3, description = "last method")public void method_3() {}/* * 解析註解,將Test_1類 所有被註解方法 的資訊列印出來 */public static void main(String[] args) {Method[] methods = Test_1.class.getDeclaredMethods();for (Method method : methods) {/* * 判斷方法中是否有指定註解類型的註解 */boolean hasAnnotation = method.isAnnotationPresent(Test.class);if (hasAnnotation) {/* * 根據註解類型返回方法的指定類型註解 */Test annotation = method.getAnnotation(Test.class);System.out.println("Test( method = " + method.getName()+ " , id = " + annotation.id() + " , description = "+ annotation.description() + " )");}}}}
輸出結果如下: Test( method = method_1 , id = 1 , description = hello method_1 ) Test( method = method_2 , id = 2 , description = no description ) Test( method = method_3 , id = 3 , description = last method )