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 杨军威 阅读(187) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜