您的位置:首页 > 编程语言 > Java开发

Java Annotation(注解)

2016-08-25 15:28 183 查看
从JDK5开始,Java增加了Annotation(注解),Annotation是代码里的特殊标记,这些标记可以在编译、类加载、运行时被读取,并执行相应的处理。通过使用Annotation,开发人员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充的信息。代码分析工具、开发工具和部署工具可以通过这些补充信息进行验证、处理或者进行部署。

Annotation提供了一种为程序元素(包、类、构造器、方法、成员变量、参数、局域变量)设置元数据的方法。Annotation不能运行,它只有成员变量,没有方法。Annotation跟public、final等修饰符的地位一样,都是程序元素的一部分,Annotation不能作为一个程序元素使用。

1 定义Annotation

定义新的Annotation类型使用@interface关键字(在原有interface关键字前增加@符号)。定义一个新的Annotation类型与定义一个接口很像,例如:

public @interface Test{

}

定义完该Annotation后,就可以在程序中使用该Annotation。使用Annotation,非常类似于public、final这样的修饰符,通常,会把Annotation另放一行,并且放在所有修饰符之前。例如:

@Test

public class MyClass{

….

}

1.1 成员变量

Annotation只有成员变量,没有方法。Annotation的成员变量在Annotation定义中以“无形参的方法”形式来声明,其方法名定义了该成员变量的名字,其返回值定义了该成员变量的类型。例如:

public @interface MyTag{

string name();

int age();

}

示例中定义了2个成员变量,这2个成员变量以方法的形式来定义。

一旦在Annotation里定义了成员变量后,使用该Annotation时就应该为该Annotation的成员变量指定值。例如:

public class Test{

@MyTag(name=”红薯”,age=30)

public void info(){

……

}

}

也可以在定义Annotation的成员变量时,为其指定默认值,指定成员变量默认值使用default关键字。示例:

public @interface MyTag{

string name() default “我兰”;

int age() default 18;

}

如果Annotation的成员变量已经指定了默认值,使用该Annotation时可以不为这些成员变量指定值,而是直接使用默认值。例如:

public class Test{

@MyTag

public void info(){

……

}

}

根据Annotation是否包含成员变量,可以把Annotation分为如下两类:

标记Annotation:没有成员变量的Annotation被称为标记。这种Annotation仅用自身的存在与否来为我们提供信息,例如@override等。

元数据Annotation:包含成员变量的Annotation。因为它们可以接受更多的元数据,因此被称为元数据Annotation。

1.2 元注解

在定义Annotation时,也可以使用JDK提供的元注解来修饰Annotation定义。JDK提供了如下4个元注解(注解的注解,不是上述的”元数据Annotation“):

@Retention

@Target

@Documented

@Inherited

1.2.1 @Retention

@Retention用于指定Annotation可以保留多长时间。

@Retention包含一个名为“value”的成员变量,该value成员变量是RetentionPolicy枚举类型。使用@Retention时,必须为其value指定值。value成员变量的值只能是如下3个:

RetentionPolicy.SOURCE:Annotation只保留在源代码中,编译器编译时,直接丢弃这种Annotation。

RetentionPolicy.CLASS:编译器把Annotation记录在class文件中。当运行Java程序时,JVM中不再保留该Annotation。

RetentionPolicy.RUNTIME:编译器把Annotation记录在class文件中。当运行Java程序时,JVM会保留该Annotation,程序可以通过反射获取该Annotation的信息。

示例:

package com.demo1;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

//name=value形式

//@Retention(value=RetentionPolicy.RUNTIME)

//直接指定

@Retention(RetentionPolicy.RUNTIME)

public @interface MyTag{

String name() default “我兰”;

}

如果Annotation里有一个名为“value“的成员变量,使用该Annotation时,可以直接使用XXX(val)形式为value成员变量赋值,无须使用name=val形式。

1.2.2 @Target

@Target指定Annotation用于修饰哪些程序元素。@Target也包含一个名为”value“的成员变量,该value成员变量类型为ElementType[ ],ElementType为枚举类型,值有如下几个:

ElementType.TYPE:能修饰类、接口或枚举类型

ElementType.FIELD:能修饰成员变量

ElementType.METHOD:能修饰方法

ElementType.PARAMETER:能修饰参数

ElementType.CONSTRUCTOR:能修饰构造器

ElementType.LOCAL_VARIABLE:能修饰局部变量

ElementType.ANNOTATION_TYPE:能修饰注解

ElementType.PACKAGE:能修饰包

示例1(单个ElementType):

package com.demo1;

import java.lang.annotation.ElementType;

import java.lang.annotation.Target;

@Target(ElementType.FIELD)

public @interface AnnTest {

String name() default “sunchp”;

}

示例2(多个ElementType):

package com.demo1;

import java.lang.annotation.ElementType;

import java.lang.annotation.Target;

@Target({ ElementType.FIELD, ElementType.METHOD })

public @interface AnnTest {

String name() default “sunchp”;

}

1.2.3 @Documented

如果定义注解A时,使用了@Documented修饰定义,则在用javadoc命令生成API文档后,所有使用注解A修饰的程序元素,将会包含注解A的说明。

示例:

@Documented

public @interface Testable {

}

public class Test {

@Testable

public void info() {

}

}

1.2.4 @Inherited

@Inherited指定Annotation具有继承性。

示例:

package com.demo2;

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.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Inherited

public @interface MyTag{

}

package com.demo2;

@MyTag

public class Base {

}

package com.demo2;

//SubClass只是继承了Base类

//并未直接使用@MyTag注解修饰

public class SubClass extends Base {

public static void main(String[] args) {

System.out.println(SubClass.class.isAnnotationPresent(MyTag.class));

}

}

示例中Base使用@MyTag修饰,SubClass继承Base,而且没有直接使用@MyTag修饰,但是因为MyTag定义时,使用了@Inherited修饰,具有了继承性,所以运行结果为true。

如果MyTag注解没有被@Inherited修饰,则运行结果为:false。

1.3 基本Annotation

JDK默认提供了如下几个基本Annotation:

@Override

限定重写父类方法。对于子类中被@Override 修饰的方法,如果存在对应的被重写的父类方法,则正确;如果不存在,则报错。@Override 只能作用于方法,不能作用于其他程序元素。

@Deprecated

用于表示某个程序元素(类、方法等)已过时。如果使用被@Deprecated修饰的类或方法等,编译器会发出警告。

@
a7d7
SuppressWarning

抑制编译器警告。指示被@SuppressWarning修饰的程序元素(以及该程序元素中的所有子元素,例如类以及该类中的方法…..)取消显示指定的编译器警告。例如,常见的@SuppressWarning(value=”unchecked”)

@SafeVarargs

@SafeVarargs是JDK 7 专门为抑制“堆污染”警告提供的。

2 提取Annotation信息(反射)

当开发者使用了Annotation修饰了类、方法、Field等成员之后,这些Annotation不会自己生效,必须由开发者提供相应的代码来提取并处理Annotation信息。这些处理提取和处理Annotation的代码统称为APT(Annotation Processing Tool)。

JDK主要提供了两个类,来完成Annotation的提取:

java.lang.annotation.Annotation接口:这个接口是所有Annotation类型的父接口(后面会分析Annotation的本质,Annotation本质是接口,而java.lang.annotation.Annotation接口是这些接口的父接口)。

java.lang.reflect.AnnotatedElement接口:该接口代表程序中可以被注解的程序元素。

2.1 java.lang.annotation.Annotation

java.lang.annotation.Annotation接口源码:

package java.lang.annotation;

public interface Annotation {

boolean equals(Object obj);

int hashCode();

String toString();

Class<? extends Annotation> annotationType();


}

java.lang.annotation.Annotation接口的主要方法是annotationType( ),用于返回该注解的java.lang.Class。

2.2 java.lang.reflect.AnnotatedElement

java.lang.reflect.AnnotatedElement接口源码:

package java.lang.reflect;

import java.lang.annotation.Annotation;

public interface AnnotatedElement {

boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);

<T extends Annotation> T getAnnotation(Class<T> annotationClass);

Annotation[] getAnnotations();

Annotation[] getDeclaredAnnotations();


}

主要方法有:

isAnnotationPresent(Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息