Java注解入门
第一部分: 了解一下 java1.5 起默认的三个 annotation 类型
一个是 @Override: 只能用在方法之上的,用来告诉别人这一个方法是改写父类的。
一个是 @Deprecated: 建议别人不要使用旧的 API 的时候用的, 编译的时候会产生警告信息, 可以设定在程序里的所有的元素上.
一个是 @SuppressWarnings: 这一个类型可以用来暂时把一些警告信息消息关闭.
第二部分: 讲一下 annotation 的概念先,再来讲一下怎样设计自己的 annotation
首先在 jdk 自带的 java.lang.annotation 包里, 打开如下几个源文件:
1、源文件 Target.java
1 2 3 4 5 6 | @Documented @Retention (RetentionPolicy.RUNTIME) @Target (ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); } |
其中 @interface 是一个关键字,在设计 annotations 的时候必须把一个类型定义为 @interface,而不能用 class 或 interface 关键字 (会不会觉得 sun 有点吝啬, 偏偏搞得与 interface 这么像).
2、源文件 Retention.java
1 2 3 4 5 6 | @Documented @Retention (RetentionPolicy.RUNTIME) @Target (ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); } |
看到这里,大家可能都模糊了, 都不知道在说什么,别急,往下看一下。
在上面的文件都用到了 RetentionPolicy,ElementType 这两个字段, 你可能就会猜到这是两个 java 文件. 的确,这两个文件的源代码如下:
3、源文件 RetentionPolicy.java
1 2 3 4 5 | public enum RetentionPolicy { SOURCE, CLASS, RUNTIME } |
这是一个 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 2 3 4 | public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE } |
@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)
上面一下 1 和 2 的源文件,它们都使用了 @Documented,@Documented 的目的就是让这一个 Annotation 类型的信息能够显示在 javaAPI 说明文档上; 没有添加的话,使用 javadoc 生成 API 文档的时候就会找不到这一个类型生成的信息.
另外一点,如果需要把 Annotation 的数据继承给子类,那么就会用到 @Inherited 这一个 Annotation 类型.
第三部分:Annotation 例子
1、Description.java
package com.ljq.test;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Description {
String value();
}
第一, 只能用 public 或默认 (default) 这两个访问权修饰. 例如,String value(); 这里把方法设为 defaul 默认类型.
第二, 参数成员只能用基本类型 byte,short,char,int,long,float,double,boolean 八种基本数据类型和 String,Enum,Class,annotations 等数据类型, 以及这一些类型的数组. 例如,String value(); 这里的参数成员就为 String.
第三, 如果只有一个参数成员, 最好把参数名称设为 "value", 后加小括号. 例: 上面的例子就只有一个参数成员.
2、Name.java
package com.ljq.test;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;//注意这里的 @Target 与 @Description 里的不同, 参数成员也不同
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Name {
String originate();
String community();
}
3、JavaEyer.java
package com.ljq.test;@Description("javaeye, 做最棒的软件开发交流社区")
public class JavaEyer {
@Name(originate = "创始人:robbin", community = "javaEye")
public String getName() {
return null;
}@Name(originate </span>= "创始人:江南白衣", community = "springside"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName2() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "借用两位的id一用,写这一个例子,请见谅!"<span style="color: rgba(0, 0, 0, 1)">; }
}
4、最后,写一个可以运行提取 JavaEyer 信息的类 TestAnnotation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.ljq.test; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; public class TestAnnotation { public static void main(String[] args) throws Exception { String CLASS_NAME = "com.ljq.test.JavaEyer" ; Class test = Class.forName(CLASS_NAME); Method[] method = test.getMethods(); boolean flag = test.isAnnotationPresent(Description. class ); if (flag) { Description des = (Description) test .getAnnotation(Description. class ); System.out.println( "描述:" + des.value()); System.out.println( "-----------------" ); } // 把JavaEyer这一类有利用到@Name的全部方法保存到Set中去 Set<Method> set = new HashSet<Method>(); for ( int i = 0 ; i < method.length; i++) { boolean otherFlag = method[i].isAnnotationPresent(Name. class ); if (otherFlag) set.add(method[i]); } for (Method m : set) { Name name = m.getAnnotation(Name. class ); System.out.println(name.originate()); System.out.println( "创建的社区:" + name.community()); } } } |
5、运行结果:
描述:javaeye, 做最棒的软件开发交流社区
-----------------
创始人:robbin
创建的社区:javaEye
创始人: 江南白衣
创建的社区:springside
本文章摘抄自网络。