Java注解(Annotation)详解
转:http://blog.csdn.net/zen99t/article/details/49508447
(二)自定义注解
先上一段代码有个大概的印象,再慢慢讲解(其实代码注释已经讲解地很清楚了):
- package diyDescription;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target({ElementType.METHOD,ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Inherited
- @Documented
- public @interface Description { // 使用 @interface 关键字定义注解
- // 成员以无参数无异常方式声明
- String desc();
- /* String desc(int a);
- * String desc() throws Exception;
- * 都是错误的声明方式
- */
- String author();
- // String author() default ”“; 合法的声明
- // 可以用 default 为成员指定一个默认值
- int age() default 18;
- /*
- * 如果声明:Map map(); 则会报错:
- * Invalid type Map for the annotation attribute Description.map;
- * only primitive type, String, Class, annotation, enumeration
- * are permitted or 1-dimensional arrays thereof
- *
- * 只有原始类型和 String, Class, annotation, enumeration 才可以
- */
- }
package diyDescription; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Description {// 使用 @interface 关键字定义注解 // 成员以无参数无异常方式声明 String desc(); /* String desc(int a); * String desc()throws Exception; * 都是错误的声明方式 */ String author(); // String author()default ""; 合法的声明 // 可以用 default 为成员指定一个默认值 int age() default 18; /* * 如果声明:Map map(); 则会报错: * Invalid type Map for the annotation attribute Description.map; * only primitive type, String, Class, annotation, enumeration * are permitted or 1-dimensional arrays thereof * * 只有原始类型和 String, Class, annotation, enumeration 才可以 */}
首先看上面代码的下面部分:
1.使用 @interface 关键字定义注解,注意关键字的位置
2.成员以无参数无异常的方式声明,注意区别一般类成员变量的声明
3.可以使用 default 为成员指定一个默认值,如上所示
4.成员类型是受限的,合法的类型包括原始类型以及 String、Class、Annotation、Enumeration (JAVA 的基本数据类型有 8 种:byte(字节)、short(短整型)、int(整数型)、long(长整型)、float(单精度浮点数类型)、double(双精度浮点数类型)、char(字符类型)、boolean( 布尔类型)
5.注解类可以没有成员,没有成员的注解称为标识注解,例如 JDK 注解中的 @Override、@Deprecation
6.如果注解只有一个成员,并且把成员取名为 value(),则在使用时可以忽略成员名和赋值号“=” , 例如 JDK 注解的 @SuppviseWarnings ;如果成员名不为 value,则使用时需指明成员名和赋值号”=”,例子代码如下:
- package jtzeng;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Target;
- @Target({ElementType.TYPE})
- public @interface SingleValue1 {
- String desc();
- }
- package jtzeng;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Target;
- @Target({ElementType.METHOD})
- public @interface SingleValue2 {
- String value();
- }
- package jtzeng;
- @SingleValue1( desc = “这是 TYPE 注解” ) // 使用时需指明成员名和赋值号”=”
- public class Test {
- @SingleValue2(“这是 METHOD 注解”) // 使用时可以忽略成员名和赋值号“=”
- public void print() {
- System.out.println();
- }
- }
package jtzeng; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.TYPE}) public @interface SingleValue1 {String desc(); } package jtzeng; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.METHOD}) public @interface SingleValue2 {String value(); } package jtzeng; @SingleValue1(desc = "这是 TYPE 注解") // 使用时需指明成员名和赋值号 "=" public class Test {@SingleValue2("这是 METHOD 注解") // 使用时可以忽略成员名和赋值号“=” public void print(){ System.out.println(); } }
2. 元注解
何为元注解?就是注解的注解,就是给你自己定义的注解添加注解,你自己定义了一个注解,但你想要你的注解有什么样的功能,此时就需要用元注解对你的注解进行说明了。
元注解有 4 个,如下代码的上面部分:
2.1. @Target
即注解的作用域,用于说明注解的使用范围(即注解可以用在什么地方,比如类的注解,方法注解,成员变量注解等等)
取值:
ElemenetType.CONSTRUCTOR—————————- 构造器声明
ElemenetType.FIELD ————————————–域声明(包括 enum 实例)
ElemenetType.LOCAL_VARIABLE————————- 局部变量声明
ElemenetType.METHOD ———————————- 方法声明
ElemenetType.PACKAGE ——————————— 包声明
ElemenetType.PARAMETER ——————————参数声明
ElemenetType.TYPE————————————— 类,接口(包括注解类型)或 enum 声明
ElemenetType.FIELD ————————————–域声明(包括 enum 实例)
ElemenetType.LOCAL_VARIABLE————————- 局部变量声明
ElemenetType.METHOD ———————————- 方法声明
ElemenetType.PACKAGE ——————————— 包声明
ElemenetType.PARAMETER ——————————参数声明
ElemenetType.TYPE————————————— 类,接口(包括注解类型)或 enum 声明
使用实例:
首先定义一个 Description 注解,
- package jtzeng;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Target;
- @Target({ElementType.TYPE,ElementType.FIELD})
- public @interface Description {
- String desc();
- String author();
- int age() default 21;
- }
package jtzeng; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.TYPE,ElementType.FIELD}) public @interface Description {String desc(); String author(); int age() default 21; }
然后再定义一个 Test 类进行测试,为了能让大家看出错误,此处用图片给出。
可以发现,因为上面定义注解时候,@Target 只包含了ElemenetType.TYPE和ElemenetType.FIELD,所以在类和声明中注解是可以的,而在方法上注解会报错。
2.2. @Retention
描述的注解在什么范围内有效。
取值有:
RetentionPolicy.SOURCE————————–只在源码显示,编译时会丢失
RetentionPolicy.CLASS—————————–编译时会记录到 class 中,运行时忽略
RetentionPolicy.RUNTIME————————- 运行时存在,可以通过反射读取
RetentionPolicy.CLASS—————————–编译时会记录到 class 中,运行时忽略
RetentionPolicy.RUNTIME————————- 运行时存在,可以通过反射读取
使用实例:
下面给出的是简单的定义,至于会有什么不同的效果,往后的解析注解部分会讲解。
- package jtzeng;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- @Retention(RetentionPolicy.RUNTIME) // 运行时存在,可以通过反射读取
- //@Retention(RetentionPolicy.SOURCE) // 只在源码显示,编译时会丢失
- //@Retention(RetentionPolicy.CLASS) // 编译时会记录到 class 中,运行时忽略
- public @interface Description {
- String desc();
- String author() default “JTZeng”;
- int age() default 21;
- }
package jtzeng; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) // 运行时存在,可以通过反射读取 //@Retention(RetentionPolicy.SOURCE) // 只在源码显示,编译时会丢失 //@Retention(RetentionPolicy.CLASS) // 编译时会记录到 class 中,运行时忽略 public @interface Description {String desc(); String author()default "JTZeng"; int age() default 21; }
2.3. @Inherited
1. 是一个标记注解,没有成员,表示允许子类继承该注解,也就是说如果一个使用了 @Inherited 修饰的注解被用于一个 class 时,则这个注解将被该 class 的子类继承拥有
2. 使用了 @Inherited 修饰的注解只能被子类所继承,并不可以从它所实现的接口继承
3. 子类继承父类的注解时,并不能从它所重载的方法继承注解
使用实例:
- package jtzeng;
- import java.lang.annotation.Inherited;
- @Inherited
- public @interface Description {
- String desc();
- String author() default “JTZeng”;
- int age() default 21;
- }
package jtzeng; import java.lang.annotation.Inherited; @Inherited public @interface Description {String desc(); String author()default "JTZeng"; int age() default 21; }
2.4. @Documented
@Documented 是一个标记注解,没有成员。用于描述其它类型的 annotation 应该被作为被标注的程序成员的公共 API,因此可以被例如 javadoc 此类的工具文档化。(有点抽象,看例子吧)
使用实例:
- /*
- * 测试 @Documented 的功能
- */
- package jtzeng;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target({ElementType.METHOD,ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Inherited
- @Documented
- public @interface Description {
- String desc();
- String author() default “JTZeng”;
- int age() default 21;
- }
- /*
- * 定义一个 Test 测试类,类和方法都有注解
- */
- package jtzeng;
- @Description(desc=“这是 TYPE 注解”,author=“JTZeng”,age=21)
- public class Test {
- private String field = “自定义注解”;
- @Description(desc=“这是 METHOD 注解”,author=“JTZeng”,age=21)
- public void print() {
- System.out.println(field);
- }
- }
/* * 测试 @Documented 的功能 */ package jtzeng; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Description {String desc(); String author()default "JTZeng"; int age() default 21; } /* * 定义一个 Test 测试类,类和方法都有注解 */ package jtzeng; @Description(desc="这是 TYPE 注解",author="JTZeng",age=21) public class Test {private String field = "自定义注解"; @Description(desc="这是 METHOD 注解",author="JTZeng",age=21) public void print(){ System.out.println(field); } }
然后,在 Eclipse 中,右键项目名称,选择 Export,选择 Java——>javadoc,下一步,完成。看结果,左边是添加了 @Documented 的效果,右边是没有添加的效果。
自定义注解结束 ~~