java學習

          java學習

           

          自定義java annotation

          @Retention(RetentionPolicy.RUNTIME)

          public @interface MyAnnotation {

           public String key();
           public String value();
          }



          @MyAnnotation(key = "k", value = "v")
          public class UserAnnotation {
           @MyAnnotation(key = "km", value = "vm")
           public void sayHello() {
            System.out.println("111");
           }

           public static void main(String[] args) throws Exception {
            Class<?> cla = Class
              .forName("com.kaishengit.annotation.UserAnnotation");
            Method[] methods = cla.getMethods();
            boolean flag = cla.isAnnotationPresent(MyAnnotation.class);
            System.out.println(flag);
            if (flag) {
             MyAnnotation mya = (MyAnnotation) cla
               .getAnnotation(MyAnnotation.class);
             System.out.println(mya.key() + "====" + mya.value());
            }
            Set<Method> set = new HashSet<Method>();
            for (int i = 0; i < methods.length; i++) {
             boolean otherflag = methods[i]
               .isAnnotationPresent(MyAnnotation.class);
             if (otherflag) {
              set.add(methods[i]);
              System.out.println(methods[i].getName());
             }

            }
            for (Method method : set) {
             MyAnnotation name = method.getAnnotation(MyAnnotation.class);
             System.out.println(name.key());
             System.out.println("value===:" + name.value());
            }

           }
          }

          posted on 2013-09-11 17:22 楊軍威 閱讀(198) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 衡东县| 大安市| 星子县| 剑河县| 长宁县| 灵丘县| 鄢陵县| 遵义市| 腾冲县| 秀山| 健康| 黑龙江省| 万年县| 鸡东县| 丽江市| 桓台县| 岳阳市| 玉环县| 定兴县| 南岸区| 海盐县| 武乡县| 宁强县| 青浦区| 奇台县| 桂阳县| 通化县| 于田县| 团风县| 五河县| 府谷县| 阿尔山市| 新兴县| 阳曲县| 分宜县| 宜章县| 龙门县| 麟游县| 天全县| 湄潭县| 西华县|