java元數據的學習

          Annotation簡介:
          ??????????Annotation提供一種機制,將程序中元素(如類、方法、屬性等)和元數據聯系起來。這樣編譯器可以將元數據保存的class文件中。代碼分析工具就可以使用這些元數據執行的額外任務。注釋采用“at”標記形式 ( @ ),后面是注釋名稱。
          ???????自定義?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類型是一種接口,能夠通過java反射API的方式提供對其信息的訪問
          ?????
          */
          ????
          /*
          ?????*?Documented?產生Java?Doc文件,這次可以看到文件中包括了@Debug的信息
          ?????
          */

          ????
          /*
          ?????*?限定?annotation?使用對象?-?Target?
          ?????*?在定義annotation時,使用java.lang.annotation.Target可以定義annotation使用的時機。
          ?????
          */
          ????
          /*
          ?????*?@Documented?Java?Doc文件
          ?????
          */

          ????
          /*
          ?????*?
          ?????*?public?enum?ElementType?{
          ?????*???TYPE,?//?適用?class,?interface,?enum
          ?????*???FIELD,?//?適用?field
          ?????*???METHOD,?//?適用?method
          ?????*???PARAMETER,?//?適用?method?上之?parameter
          ?????*???CONSTRUCTOR,?//?適用?constructor
          ?????*???LOCAL_VARIABLE,?//?適用區域變量
          ?????*???ANNOTATION_TYPE,?//?適用?annotation?型態
          ?????*???PACKAGE?//?適用?package
          ?????*???}?
          ?????
          */

          ????
          /*
          ?????*?Retention?在你自定義的形態中,指定編譯器如何處理自定義的annotation
          ?????*?告知編譯器如何處理?annotaion?-?Retention?
          ?????
          */

          ????
          /*
          ?????*?RetentionPolicy
          ?????*?????????SOURCE????????編譯器處理完Annotation信息后就沒事了
          ?????*?????????CLASS????????編譯器將Annotation儲存于class檔中,預設
          ?????*?????????RUNTIME????????編譯器將Annotation儲存于class檔中,可由VM讀入
          ?????*?????????????????????使用java設計的一個程序代碼工具,從VM中讀出Annotation信息,已在分析程序中使用。
          ?????*?????????????????????搭配Reflection機制,就可以實現。
          ?????*?????????
          ?????*?????????J2SE?5.0中新增了java.lang.reflect.AnnotatedElement這個接口,當中定義有四個方法:
          ?????*?????????????????public?Annotation?getAnnotation(Class?annotationType);
          ?????*?????????????????public?Annotation[]?getAnnotations();
          ?????*?????????????????public?Annotation[]?getDeclaredAnnotations();
          ?????*?????????????????public?boolean?isAnnotationPresent(Class?annotationType);
          ?????*?????????Class、Constructor、Field、Method、Package等類別,都實作了?AnnotatedElement這個接口,
          ?????*?????????所以您可以從這些類別的實例上,分別取得標示于其上的Annotation與其信息,
          ?????*?????????如果?RetentionPolicy為RUNTIME的話。

          ?????
          */

          ????
          /*
          ?????*?@Inherited?子類是否繼承父類的?annotation?
          ?????
          */
          @Documented
          @Target({ElementType.CONSTRUCTOR,?ElementType.METHOD})
          //指定目標
          @Retention(RetentionPolicy.RUNTIME)//設置保持性
          @Inherited
          public?@interface?Debug?{
          ????
          //定義了value()方法,編譯器在編譯時會自動幫您產生一個value的變量成員,接著在使用Debug?Annotation時要指定值
          ????
          //注釋類型的數據成員被設置成使用有限信息進行工作
          ????
          //定義數據成員不需要分別定義訪問和修改的方法,只需定義一個方法,以數據成員的名稱命名它,數據類型應該是該方法返回值的類型.
          ????String?value()default?"windfree";//默認值的類型必需與成員變量的申明類型一置
          ????String?name();
          }

          在程序中使用自定義的annotation
          ???TestDebug.java
          ???
          ??????
          package?annotation.test;

          public?class?TestDebug?{
          ????@Debug(
          ???????????????name?
          =?"zgliu"
          ????????????)
          ?????public?void?doSomething()?{
          ????????

          ????????}

          }

          使用java中反射API讀取class中的元數據的信息
          ??????DebugTool.java:
          ??????
          ?????????
          package?annotation.test;

          import?java.lang.annotation.Annotation;
          import?java.lang.reflect.Method;

          public?class?DebugTool?{
          ????
          public?static?void?main(String?[]args)throws?NoSuchMethodException?{
          ????????Class
          <TestDebug>?c=TestDebug.class;
          ????????Method?method?
          =?c.getMethod("doSomething");
          ????????
          if(method.isAnnotationPresent(Debug.class))?{
          ????????????System.out.println(
          "@Debug?is?found.");
          ????????????Debug?debug?
          =?method.getAnnotation(Debug.class);
          ????????????System.out.println(
          "\tvalue?=?"?+?debug.value());
          ????????????System.out.println(
          "\tname?=?"?+?debug.name());
          ????????}

          ????????
          else?{
          ????????????System.out.println(
          "@Debug?is?not?found.");
          ????????}


          ????????Annotation[]?annotations?
          =?method.getAnnotations();
          ????????
          for(Annotation?annotation?:?annotations)?{
          ????????????System.out.println(
          ????????????????????annotation.annotationType().getName());
          ????????}

          ????}

          }


          ???學習中的一個完整的例子。
          posted on 2006-12-12 10:51 windfree 閱讀(1965) 評論(3)  編輯  收藏 所屬分類: java

          評論

          # re: java元數據的學習 2007-05-04 15:07 BeanSoft  回復  更多評論   

          學習了! 支持一下, 尤其是如何解析 Annotation 部分的代碼. 受益了!

          # re: java元數據的學習 2007-05-22 16:40 久城  回復  更多評論   

          看完了,試了下。終于有了些思路。謝謝BZ。
          只是有些疑惑,我們什么時候能用到annotation呢?

          # re: java元數據的學習 2012-07-12 10:10 得到  回復  更多評論   

          本草藥王

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 岳阳市| 津南区| 汾西县| 应用必备| 汝南县| 秦皇岛市| 宿州市| 靖宇县| 陆川县| 临漳县| 黑河市| 东安县| 韶关市| 永年县| 泸州市| 乌海市| 合水县| 泾阳县| 即墨市| 离岛区| 青神县| 西青区| 福清市| 平安县| 大洼县| 平乐县| 拜城县| 海门市| 梁山县| 瓮安县| 嘉义县| 柯坪县| 巴彦淖尔市| 宝山区| 剑阁县| 新化县| 桃源县| 南平市| 鹤岗市| 东光县| 临夏市|