Annotation实例

来源:互联网 发布:淘宝改默认地址怎么改 编辑:程序博客网 时间:2024/06/05 15:28

 

 

http://www.javaeye.com/topic/36659

说的不错,mark一下。

 

  1. package com.rx.annotation;
  2. import java.lang.annotation.Documented; 
  3. import java.lang.annotation.ElementType; 
  4. import java.lang.annotation.Retention; 
  5. import java.lang.annotation.RetentionPolicy; 
  6. import java.lang.annotation.Target; 
  7. @Target(ElementType.TYPE) 
  8. @Retention(RetentionPolicy.RUNTIME) 
  9. @Documented 
  10. public @interface Description { 
  11.     String value(); 

 

 

  1. package com.rx.annotation;
  2. import java.lang.annotation.Documented; 
  3. import java.lang.annotation.ElementType; 
  4. import java.lang.annotation.Retention; 
  5. import java.lang.annotation.RetentionPolicy; 
  6. import java.lang.annotation.Target; 
  7. @Target(ElementType.METHOD) 
  8. @Retention(RetentionPolicy.RUNTIME) 
  9. @Documented 
  10. public @interface Name {
  11.     String originate(); 
  12.     String community(); 
  13. }

 

  1. package com.rx.annotation;
  2. @Description("javaeye,做最棒的软件开发交流社区")
  3. public class JavaEyer { 
  4.     
  5.     @Name(originate="创始人:robbin",community="javaEye"
  6.     public String getName() 
  7.     { 
  8.         return null
  9.     } 
  10.     
  11.     @Name(originate="创始人:江南白衣",community="springside"
  12.     public String getName2()
  13.     { 
  14.         return "借用两位的id一用,写这一个例子,请见谅!"
  15.     } 
  16.     public void test() {
  17.         System.out.println("测试.....");
  18.     }

 

  1. package com.rx.annotation;
  2. import java.lang.reflect.Method; 
  3. import java.util.HashSet; 
  4. import java.util.Set; 
  5. public class TestAnnotation { 
  6.     public static void main(String[] args) throws Exception { 
  7.         String CLASS_NAME = "com.rx.annotation.JavaEyer"
  8.         Class annotationClass = Class.forName(CLASS_NAME); 
  9.         boolean flag = annotationClass.isAnnotationPresent(Description.class); 
  10.         if(flag) 
  11.         { 
  12.             Description des = (Description)annotationClass.getAnnotation(Description.class); 
  13.             System.out.println("描述:" + des.value()); 
  14.             System.out.println("-----------------"); 
  15.         } 
  16.     
  17.         Method[] method = annotationClass.getMethods(); 
  18.         //把JavaEyer这一类有利用到@Name的全部方法保存到Set中去 
  19.         Set<Method> set = new HashSet<Method>(); 
  20.         for(int i=0;i<method.length;i++) 
  21.         { 
  22.             boolean otherFlag = method[i].isAnnotationPresent(Name.class); 
  23.             if(otherFlag) set.add(method[i]); 
  24.         }
  25.         
  26.         // 遍历集合
  27.         for(Method m: set
  28.         { 
  29.             Name name = m.getAnnotation(Name.class); 
  30.             System.out.println(name.originate()); 
  31.             System.out.println("创建的社区:"+name.community()); 
  32.         } 
  33.     } 

运行测试结果

  1. 描述:javaeye,做最棒的软件开发交流社区
  2. -----------------
  3. 创始人:robbin
  4. 创建的社区:javaEye
  5. 创始人:江南白衣
  6. 创建的社区:springside

其他参考文献:http://www.blogjava.net/nokiaguy/archive/2008/05/09/199456.html