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)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 富平县| 芦溪县| 溆浦县| 繁昌县| 平远县| 延津县| 奎屯市| 三江| 应城市| 辽中县| 成都市| 包头市| 兴国县| 重庆市| 乌苏市| 孟村| 河间市| 深泽县| 凤阳县| 海伦市| 新乡县| 葫芦岛市| 铜陵市| 顺平县| 大埔区| 屯留县| 万山特区| 客服| 阜康市| 乐清市| 米脂县| 绥中县| 新闻| 仁怀市| 凌源市| 余江县| 江达县| 宁晋县| 淮阳县| 乌恰县| 木兰县|