Annotation的注解定义通过关键字@interface表示。
定义注解格式:
public @interface 注解名 {定义体}
注解参数的可支持数据类型:
1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
2.String类型
3.Class类型
4.enum类型
5.Annotation类型
6.以上所有类型的数组
下面通过例子来分析注解:
//注解类定义
package com.mcg.demo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationDemo
{
String fun() default "hello Annotation";
}
//注解类使用demo
package com.mcg.demo;
@AnnotationDemo
public class AnnotationMain {
public static void main(String[] args) {
AnnotationDemo annotationDemo=AnnotationMain.class.getAnnotation(AnnotationDemo.class);
String name=annotationDemo.fun();
System.out.println(name);
}
}
//输出:
hello Annotation
观察上面使用例子以及输出结果,可以确定AnnotationDemo在编译过程中,编译器对其进行了语法糖处理。通过javap -verbose查看编译后的AnnotationDemo.class文件:
public interface com.mcg.demo.AnnotationDemo extends java.lang.annotation.Annotation
SourceFile: "AnnotationDemo.java"
RuntimeVisibleAnnotations:
0: #14(#15=[e#16.#17])
1: #18(#15=e#19.#20)
minor version: 0
major version: 49
flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT, ACC_ANNOTATION
Constant pool:
#1 = Class #2 // com/mcg/demo/AnnotationDemo
#2 = Utf8 com/mcg/demo/AnnotationDemo
#3 = Class #4 // java/lang/Object
#4 = Utf8 java/lang/Object
#5 = Class #6 // java/lang/annotation/Annotation
#6 = Utf8 java/lang/annotation/Annotation
#7 = Utf8 fun
#8 = Utf8 ()Ljava/lang/String;
#9 = Utf8 AnnotationDefault
#10 = Utf8 hello Annotation
#11 = Utf8 SourceFile
#12 = Utf8 AnnotationDemo.java
#13 = Utf8 RuntimeVisibleAnnotations
#14 = Utf8 Ljava/lang/annotation/Target;
#15 = Utf8 value
#16 = Utf8 Ljava/lang/annotation/ElementType;
#17 = Utf8 TYPE
#18 = Utf8 Ljava/lang/annotation/Retention;
#19 = Utf8 Ljava/lang/annotation/RetentionPolicy;
#20 = Utf8 RUNTIME
{
public abstract java.lang.String fun();
flags: ACC_PUBLIC, ACC_ABSTRACT
AnnotationDefault:
default_value: s#10}
发现编译器将AnnotationDemo变成了接口,同时继承了java.lang.annotation.Annotation接口,Annotation接口是所有注解的共同基类接口。由于AnnotationDemo是接口,要调用fun()方法,需要创建出实现了AnnotationDemo的实例。观察使用例子语句:
1.AnnotationDemo annotationDemo=AnnotationMain.class.getAnnotation(AnnotationDemo.class);
2.String name=annotationDemo.fun();
通过语句2确定语句1返回了一个实现了AnnotationDemo的实例。debug代码可以观察具体的调用堆栈,这里不具体讲解,过程中涉及到了一个重要的栈帧,是AnnotationParser的annotationForMap方法(如下),该方法的作用就是通过动态代理的机制返回一个实现了AnnotationDemo的代理类。所以语句1中annotationDemo就是这个代理类。
//AnnotationParser
public static Annotation annotationForMap(Class<? extends Annotation> arg,
Map<String, Object> arg0) {
return (Annotation) Proxy.newProxyInstance(arg.getClassLoader(),
new Class[]{arg}, new AnnotationInvocationHandler(arg, arg0));
}
AnnotationInvocationHandler类实现了InvocationHandler接口。
上面AnnotationDemo中设置了一些注解,这些注解是所谓的元注解,元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
1.@Target,2.@Retention,3.@Documented,4.@Inherited
@Target:
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Retention:
@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)
@Documented:
@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
@Inherited:
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。