Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29943795
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-01 11:00:12

  1. import java.lang.annotation.Annotation;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. import java.lang.reflect.Constructor;
  7. import java.lang.reflect.Field;
  8. import java.lang.reflect.Method;

  9. @Target(ElementType.CONSTRUCTOR)
  10. // 用于构造方法
  11. @Retention(RetentionPolicy.RUNTIME)
  12. // 在运行时加载到Annotation到JVM中(这个一定不能丢掉,否则在getAnnotation的时候取不到!!!)
  13. @interface Constructor_Annotation {
  14.     String value() default "默认构造方法"; // 定义一个具有默认值的String型成员
  15. }

  16. @Target( { ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
  17. // 用于字段,方法,参数
  18. @Retention(RetentionPolicy.RUNTIME)
  19. // 在运行时加载到Annotation到JVM中
  20. @interface Field_Method_Parameter_Annotation {
  21.     Class type() default void.class; // 定义一个具有默认值的Class型成员

  22.     String describ(); // 定义一个没有默认值的String成员
  23. }

  24. public class AnnotationTest {

  25.     @Field_Method_Parameter_Annotation(describ = "字段编号", type = int.class)
  26.     // 注释字段
  27.     int id;
  28.     @Field_Method_Parameter_Annotation(describ = "字段姓名", type = String.class)
  29.     // 注释字段
  30.     String name;

  31.     @Constructor_Annotation()
  32.     // 采用默认构造方法
  33.     public AnnotationTest() {

  34.     }

  35.     @Constructor_Annotation("立即初始化构造方法.")
  36.     // 注释构造方法
  37.     public AnnotationTest(
  38.     // 注释构造方法参数
  39.             @Field_Method_Parameter_Annotation(describ = "编号", type = int.class) int id, @Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) {
  40.         this.id = id;
  41.         this.name = name;
  42.     }

  43.     @Field_Method_Parameter_Annotation(describ = "获得编号", type = int.class)
  44.     public int getId() {
  45.         return id;
  46.     }

  47.     @Field_Method_Parameter_Annotation(describ = "设置编号")
  48.     // 成员type,采用默认注释方法
  49.     public void setId(
  50.     // 注释参数
  51.             @Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id) {
  52.         this.id = id;
  53.     }

  54.     @Field_Method_Parameter_Annotation(describ = "获得姓名", type = String.class)
  55.     public String getName() {
  56.         return name;
  57.     }

  58.     @Field_Method_Parameter_Annotation(describ = "设置姓名")
  59.     public void setName(@Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) {
  60.         this.name = name;
  61.     }

  62.     /**
  63.      * @param args
  64.      */
  65.     public static void main(String[] args) {
  66.         // TODO Auto-generated method stub
  67.         // 构造方法:
  68.         Constructor[] declaredConstructor = AnnotationTest.class.getDeclaredConstructors(); // 获得所有的构造方法
  69.         for (int i = 0; i < declaredConstructor.length; i++) {
  70.             Constructor constructor = declaredConstructor[i]; // 遍历构造方法
  71.             if (constructor.isAnnotationPresent(Constructor_Annotation.class)) // 查看是否指定类型的注释
  72.             {
  73.                 Constructor_Annotation ca = (Constructor_Annotation) constructor.getAnnotation(Constructor_Annotation.class);
  74.                 System.out.println("ca.value()=: " + ca.value());
  75.             }

  76.             Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();// 获得参数注释
  77.             for (int j = 0; j < parameterAnnotations.length; j++) {
  78.                 int length = parameterAnnotations[j].length;
  79.                 if (length == 0) // 如果为0,则表示没有为该参数添加注释
  80.                 {
  81.                     System.out.println("没有为该参数添加注释");
  82.                 } else {
  83.                     for (int k = 0; k < length; k++) {
  84.                         // 获得参数注释
  85.                         Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation) parameterAnnotations[j][k];
  86.                         System.out.print(" " + pa.describ()); // 参数描述
  87.                         System.out.println(" " + pa.type()); // 参数类型
  88.                     }
  89.                 }
  90.             }
  91.             System.out.println("****************");
  92.         }

  93.         // 字段:
  94.         System.out.println("********字段的Annotation*************");
  95.         Field[] declaredFields = AnnotationTest.class.getDeclaredFields(); // 获得所有的字段
  96.         for (int i = 0; i < declaredFields.length; i++) {
  97.             Field field = declaredFields[i];
  98.             // 查看是否具有指定类型的注释:
  99.             if (field.isAnnotationPresent(Field_Method_Parameter_Annotation.class)) {
  100.                 Field_Method_Parameter_Annotation fa = (Field_Method_Parameter_Annotation) field.getAnnotation(Field_Method_Parameter_Annotation.class);
  101.                 System.out.print(" " + fa.describ()); // 获得字段描述
  102.                 System.out.println(" " + fa.type()); // 获得字段类型
  103.             }
  104.         }

  105.         // 方法
  106.         System.out.println("********方法的Annotation*************");
  107.         Method[] methods = AnnotationTest.class.getDeclaredMethods(); // 获得所有的方法
  108.         for (int i = 0; i < methods.length; i++) {
  109.             Method method = methods[i];
  110.             // 查看是否指定注释:
  111.             if (method.isAnnotationPresent(Field_Method_Parameter_Annotation.class))

  112.             {
  113.                 Field_Method_Parameter_Annotation ma = (Field_Method_Parameter_Annotation) method.getAnnotation(Field_Method_Parameter_Annotation.class);
  114.                 System.out.print(" " + ma.describ()); // 获得方法描述
  115.                 System.out.println(" " + ma.type()); // 获得方法类型
  116.             }

  117.             Annotation[][] parameterAnnotations = method.getParameterAnnotations(); // 获得所有参数
  118.             for (int j = 0; j < parameterAnnotations.length; j++) {
  119.                 int length = parameterAnnotations[j].length;
  120.                 if (length == 0) {
  121.                     System.out.println("没有添加Annotation参数");
  122.                 } else {
  123.                     for (int k = 0; k < length; k++) {
  124.                         // 获得指定的注释:
  125.                         Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation) parameterAnnotations[j][k];
  126.                         System.out.print(" " + pa.describ()); // 获得参数描述
  127.                         System.out.println(" " + pa.type()); // 获得参数类型
  128.                     }
  129.                 }
  130.             }
  131.             System.out.println("********************");

  132.         }
  133.     }

  134. }
阅读(1451) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~