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();
          }

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

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

          ????????}

          }

          使用java中反射API讀取class中的元數(shù)據(jù)的信息
          ??????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());
          ????????}

          ????}

          }


          ???學(xué)習(xí)中的一個(gè)完整的例子。
          posted on 2006-12-12 10:51 windfree 閱讀(1964) 評(píng)論(3)  編輯  收藏 所屬分類: java

          評(píng)論

          # re: java元數(shù)據(jù)的學(xué)習(xí) 2007-05-04 15:07 BeanSoft  回復(fù)  更多評(píng)論   

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

          # re: java元數(shù)據(jù)的學(xué)習(xí) 2007-05-22 16:40 久城  回復(fù)  更多評(píng)論   

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

          # re: java元數(shù)據(jù)的學(xué)習(xí) 2012-07-12 10:10 得到  回復(fù)  更多評(píng)論   

          本草藥王

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 庆城县| 会宁县| 辽阳市| 墨玉县| 奈曼旗| 屯留县| 富锦市| 张北县| 二连浩特市| 新干县| 信阳市| 上杭县| 邮箱| 惠州市| 桂东县| 东乡县| 高雄市| 马山县| 乌恰县| 海安县| 根河市| 新野县| 礼泉县| 利辛县| 营山县| 维西| 明溪县| 高唐县| 县级市| 阿城市| 松江区| 新巴尔虎右旗| 九龙城区| 新宾| 托里县| 咸阳市| 襄城县| 舒城县| 石河子市| 华亭县| 汶上县|