Chinaunix首页 | 论坛 | 博客
  • 博客访问: 450924
  • 博文数量: 80
  • 博客积分: 2301
  • 博客等级: 大尉
  • 技术积分: 884
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-16 20:07
个人简介

I\'m interested in mathematics and Daoism. Welcome to talk about these subjects with me.

文章分类

全部博文(80)

文章存档

2017年(2)

2016年(16)

2015年(4)

2014年(6)

2013年(22)

2012年(2)

2011年(1)

2010年(4)

2009年(20)

2008年(2)

2007年(1)

我的朋友

分类: Java

2016-06-09 10:38:03

    关于java语言的Annotation,讲解的文章很多了。不再废话,直接上代码说明怎么使用Annotation.

    MyAnnotation的java文件如下:

点击(此处)折叠或打开

  1. package Annotation;

  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Inherited;
  5. import java.lang.annotation.Retention;
  6. import java.lang.annotation.RetentionPolicy;
  7. import java.lang.annotation.Target;

  8. //@Retention(RetentionPolicy.SOURCE)
  9. @Retention(RetentionPolicy.RUNTIME)
  10. public @interface MyAnnotation
  11. {
  12.     String hello() default "namaste";

  13.     String world();//没有指定默认值,使用这个Annotation的时候,必须给world指定值
  14.     
  15.     int[] array() default { 2, 4, 5, 6 };
  16.     
  17.     Class style() default String.class;
  18. }
    以上文件定义了一个Annotation.

    MyTest类的java文件如下:

点击(此处)折叠或打开

  1. package Annotation;

  2. import java.lang.String;

  3. @MyAnnotation(hello = "beijing", world = "shanghai", array={2, 3, 5, 7 }, style = int.class)
  4. public class MyTest
  5. {
  6.     @MyAnnotation(world = "shanghai", array={2, 4, 6, 8})
  7.     @SuppressWarnings("unchecked")
  8.     public void output()
  9.     {
  10.         System.out.println("output something!");
  11.     }
  12. }

这个类以及其中的方法使用了MyAnnotation.

程序入口的类,代码如下:

点击(此处)折叠或打开

  1. package Annotation;

  2. import java.lang.reflect.Method;
  3. import java.lang.annotation.Annotation;
  4. import java.lang.reflect.AnnotatedElement;

  5. public class ExampleMain {
  6.     //打印MyAnnotation对象
  7.     static void printAnnotation(MyAnnotation annotation, String desc)
  8.     {
  9.         if (annotation == null)
  10.             return ;
  11.         if (desc != null)
  12.             System.out.println(desc);

  13.         System.out.println("hello is: " + annotation.hello());
  14.         System.out.println("world is: " + annotation.world());
  15.         if (annotation.array() != null)
  16.         {
  17.             System.out.print("array is: ");
  18.             for(int a : annotation.array())
  19.             {
  20.                 System.out.print(a);
  21.                 System.out.print("\t");
  22.             }
  23.             System.out.println("");
  24.         }
  25.         System.out.println("style is: " + annotation.style());

  26.         System.out.println("");
  27.     }

  28.     static void test1()
  29.     {
  30.         //如果MyTest类名上有注解@MyAnnotation修饰,则为true
  31.         if (!MyTest.class.isAnnotationPresent(MyAnnotation.class))
  32.         {
  33.             System.out.println("class MyTest DOES NOT have any annotation");
  34.             return ;
  35.         }

  36.         MyAnnotation annotation = MyTest.class.getAnnotation(MyAnnotation.class);
  37.         printAnnotation(annotation, "MyTest类上的Annotation如下:");
  38.     }

  39.     static void test2()
  40.     {
  41.         Class<MyTest> c = MyTest.class;
  42.         Method method = null;
  43.         try {
  44.             method = c.getMethod("output", new Class[] {});
  45.         }
  46.         catch (NoSuchMethodException ne){
  47.             System.out.println("MyTest 类没有output这个方法");
  48.             return ;
  49.         }

  50.         if (method.isAnnotationPresent(MyAnnotation.class))
  51.         {
  52.             //获取方法上注解@MyAnnotation的信息
  53.             MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
  54.             printAnnotation(annotation, "MyTest类的output方法的Annotation如下:");
  55.         }

  56.         //得到output方法上的所有注解,当然是被RetentionPolicy.RUNTIME修饰的

  57.         System.out.println("output方法上的所有注解: ");
  58.         Annotation[] annotations = method.getAnnotations();
  59.         for (Annotation annotation : annotations)
  60.         {
  61.             System.out.println("annotation type name : " + annotation.annotationType().getName());
  62.         }
  63.     }

  64.     public static void main(String[] args) throws Exception
  65.     {
  66.         //例子1: 获得一个类的Annotation
  67.         //test1();

  68.         //例子2:获得一个方法的Annotation
  69.         test2();

  70.     }
  71. }

在main函数可以调用test1(),也可以调用test2().分别展示了怎样通过反射技术来获得MyTest类的Annotation。
阅读(823) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~