java 注解(转)

时间:2023-03-09 13:11:38
java 注解(转)

第一部分:了解一下java1.5起默认的三个annotation类型: 
   一个是@Override:只能用在方法之上的,用来告诉别人这一个方法是改写父类的。 
   一个是@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,可以设定在程序里的所有的元素上. 
   一个是@SuppressWarnings:这一个类型可以来暂时把一些警告信息消息关闭. 
   如果不清楚上面三个类型的具体用法,各位可以baidu或google一下的,很简单的。

第二部分:讲一下annotation的概念先,再来讲一下怎样设计自己的annotation. 
   首先在jdk自带的java.lang.annotation包里,打开如下几个源文件: 
   
   1、源文件Target.java

  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.ANNOTATION_TYPE)
  4. public @interface Target {
  5. ElementType[] value();
  6. }
  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.ANNOTATION_TYPE)
  4. public @interface Target {
  5. ElementType[] value();
  6. }

其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字(会不会觉得sun有点吝啬,偏偏搞得与interface这么像). 
   
   2、源文件Retention.java

  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.ANNOTATION_TYPE)
  4. public @interface Retention {
  5. RetentionPolicy value();
  6. }
  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.ANNOTATION_TYPE)
  4. public @interface Retention {
  5. RetentionPolicy value();
  6. }

看到这里,大家可能都模糊了,都不知道在说什么,别急,往下看一下. 
   在上面的文件都用到了RetentionPolicy,ElementType这两个字段,你可能就会猜到这是两个java文件.的确,这两个文件的源代码如下: 
   
   3、源文件RetentionPolicy.java

  1. public enum RetentionPolicy {
  2. SOURCE,
  3. CLASS,
  4. RUNTIME
  5. }
  1. public enum RetentionPolicy {
  2. SOURCE,
  3. CLASS,
  4. RUNTIME
  5. }

这是一个enum类型,共有三个值,分别是SOURCE,CLASS 和 RUNTIME. 
   SOURCE代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。 
   ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS. 
   第三个,是RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的. 
  举一个例子,如@Override里面的Retention设为SOURCE,编译成功了就不要这一些检查的信息;相反,@Deprecated里面的Retention设为RUNTIME,表示除了在编译时会警告我们使用了哪个被Deprecated的方法,在执行的时候也可以查出该方法是否被Deprecated.

4、源文件ElementType.java

  1. public enum ElementType {
  2. TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR,
  3. LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE
  4. }
  1. public enum ElementType {
  2. TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR,
  3. LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE
  4. }

@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.说明一下:TYPE(类型), FIELD(属性), METHOD(方法), PARAMETER(参数), CONSTRUCTOR(构造函数),LOCAL_VARIABLE(局部变量), ANNOTATION_TYPE,PACKAGE(包),其中的TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上. 
   另外,从1的源代码可以看出,@Target自己也用了自己来声明自己,只能用在ANNOTATION_TYPE之上. 
   如果一个Annotation类型没有指明@Target使用在哪些元素上,那么它可以使用在任何元素之上,这里的元素指的是上面的八种类型. 
   举几个正确的例子: 
   @Target(ElementType.METHOD) 
   @Target(value=ElementType.METHOD) 
   @Target(ElementType.METHOD,ElementType.CONSTRUCTOR)   
   具体参考一下javadoc文档 
   
   上面一下1和2的源文件,它们都使用了@Documented,@Documented的目的就是让这一个Annotation类型的信息能够显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个类型生成的信息. 
   另外一点,如果需要把Annotation的数据继承给子类,那么就会用到@Inherited这一个Annotation类型. 
   
   第三部分:下面讲的设计一个最简单的Annotation例子,这一例子共用四个文件; 
   1、Description.java

  1. package lighter.javaeye.com;
  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();
  12. }
  1. package lighter.javaeye.com;
  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();
  12. }

说明:所有的Annotation会自动继承java.lang.annotation这一个接口,所以不能再去继承别的类或是接口. 
   最重要的一点,Annotation类型里面的参数该怎么设定: 
   第一,只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型. 
   第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String. 
   第三,如果只有一个参数成员,最好把参数名称设为"value",后加小括号.例:上面的例子就只有一个参数成员.

2、Name.java

  1. package lighter.javaeye.com;
  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与@Description里的不同,参数成员也不同
  8. @Target(ElementType.METHOD)
  9. @Retention(RetentionPolicy.RUNTIME)
  10. @Documented
  11. public @interface Name {
  12. String originate();
  13. String community();
  14. }
  1. package lighter.javaeye.com;
  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与@Description里的不同,参数成员也不同
  8. @Target(ElementType.METHOD)
  9. @Retention(RetentionPolicy.RUNTIME)
  10. @Documented
  11. public @interface Name {
  12. String originate();
  13. String community();
  14. }

3、JavaEyer.java

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

4、最后,写一个可以运行提取JavaEyer信息的类TestAnnotation

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

5、运行结果: 
     描述:javaeye,做最棒的软件开发交流社区 
     ----------------- 
    创始人:robbin 
    创建的社区:javaEye 
    创始人:江南白衣 
     创建的社区:springside