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

Java反射学习总结五(Annotation(注解)-基础篇)

2017-02-14 14:18 561 查看
Annotation(注解)简介:

注解大家印象最深刻的可能就是JUnit做单元测试,和各种框架里的使用了。本文主要简单介绍一下注解的使用方法,下篇文章再深入的研究。

annotation并不直接影响代码语义,但是它能够被看作类似程序的工具或者类库,它会反过来对正在运行的程序语义有所影响。

annotation可以从源文件,class文件或者以在运行时反射的多种方式被读取

Java注解系统自带有主要以下几个注解:

Override注解表示子类要重写(override)父类的对应方法

Deprecated注解表示方法是不建议被使用的

Suppress Warnings注解表示抑制警告

如何自定义注解:

只需要使用@interface来定义一个注解,例如:

[java] view plain copy print?





//使用@interface来声明一个注解(实际上是自动继承了java.lang.annotation.Annotation接口)

public @interface AnnotationTest {

String value1() default "hello"; //为注解设置String类型的属性Value1,并使用defalut关键字设置默认值

EnumTest value2(); //设置枚举类型的value2

String[] value3(); //设置数组类型的value3

}

如何来使用注解呢,如下:

[java] view plain copy print?





@AnnotationTest(value2 = EnumTest.age, value3={""})

public class AnnotationUsage {

@AnnotationTest(value1 = "Test", value2 = EnumTest.name, value3={""})

String test;

@AnnotationTest(value1 = "Test", value2 = EnumTest.name, value3={""})

public void method(){

System.out.println("usage of Annotation");

}

}

如上,注解可以标注在属性,方法,类上。

需要使用name=value这种赋值方式为属性赋值,因为value1设置了默认属性,所以可以忽略,如果没有设置默认值则所有的属性都要一一赋值。

还有一种特殊情况,如果注解里只定义了一个属性,名字是value,那么可以直接赋值,不需要使用name=value这种赋值方式,如下:

[java] view plain copy print?





public @interface AnnotationTest {

String value();

}

@AnnotationTest("test")

public void method(){

System.out.println("usage of Annotation");

}

修饰注解的“注解”

注解也可以添加注解的“注解”去修饰,常用的有以下两个,一个是Retention,一个Target

Retention:

使用Retention必须要提供一个为java.lang.annotation.RetentionPolicy类型的的枚举

RetentionPolicy枚举有以下3个类型:

SOURCE:编译程序时处理完Annotation信息后就完成任务

CLASS:编译程序将Annotation存储于class文件中,不可以由虚拟机读入

RUNTIME:编译程序将Annotation存储于class文件中,可以由虚拟机读入

用这三种Retention的Prolicy可以决定注解是从源文件,class文件或者以在运行时反射被读取

关于Retention的例子在最后

Target:

使用java.lang.annotation.Target可以定义注解被使用的位置

同样,在使用时要指定一个java.lang.annotation.ElementType的枚举值类型为他的“属性”

ElementType枚举的类型如下:

ANNOTATION_TYPE:适用于annotation

CONSTRUCTOR:适用于构造方法

FIELD:适用于field

LOCAL_VARIABLE:适用于局部变量

METHOD:适用于方法

PACKAGE:适用于package

PARAMETER:适用于method上的parameter

TYPE:适用于class,interface,enum

如下:定义一个注解MyTarget,设置Target类型为Method来修饰这个注解,这样这个注解只能标注在method的方法上

[java] view plain copy print?





@Target(ElementType.METHOD)

public @interface MyTarget {

String hello() default "hello";

}

[java] view plain copy print?





@MyTarget //这里则会报错,因为他标注在类上面了

public class MyTargetTest {

@MyTarget //标注在方法上不会报错

public void doSomething(){

System.out.println("hello world");

}

}

使用反射调用注解

在以下的类中Class Constructor Field Method Package等类都实现了AnnotatedElement接口
在接口中有以下重要的方法:

getAnnotations(Class annotationType)获取一个指定的annotation类型

getAnnotations() 获取所有的Annotation

getDeclaredAnnotations() 获取声明过的所有Annotation

isAnnotationPresent(Class<? extends Annotation> annotationClass)这个annotation是否出现

通过这些方法,配合反射我们就可以在程序运行时拿到注解的内容了,例子如下:

[java] view plain copy print?





@Retention(RetentionPolicy.RUNTIME) //定义一个注解,使用Retention标注为RUNTIME

public @interface MyAnnotation {

String hello() default "hello";

String world();

}

该注解被标示为runtime类型,表示该注解最后可以保存在class文件中,并为java虚拟机在运行时读取到

[java] view plain copy print?





@Retention(RetentionPolicy.CLASS) //定义一个注解,Retention标注为RUNTIME

public @interface MyAnnotation2 {

String hello() default "hello"; //设置默认值为hello

}

自定义的另一个注解Retention标示为class

[java] view plain copy print?





public class MyTest {

@SuppressWarnings("unchecked") //java自带的注解Retention的policy为SOURCE

@Deprecated //java自带的注解Retention的policy为RUNTIME

@MyAnnotation(Name="Dean", Age="25") //自定义的注解Retention的policy为RUNTIME

@MyAnnotation2 //自定义的注解Retention的policy为CLASS

public void TestMethod() {

System.out.println("this is a method");

}

}

定义一个TestMethod方法,给他标示了4个注解,其中2个java自带的,2个我们自定义的。注解的的Retention属性各不相同。

下面定义一个测试类来验证结果:

[java] view plain copy print?





public static void main(String[] args) throws Exception {

MyTest myTest = new MyTest();

//通过反射得到TestMethod方法

Class<MyTest> c = MyTest.class;

Method method = c.getMethod("TestMethod", new Class[]{});

//AnnotatedElement接口中的方法isAnnotationPresent(),判断传入的注解类型是否存在

if (method.isAnnotationPresent(MyAnnotation.class)) {

method.invoke(myTest, new Object[]{});

//AnnotatedElement接口中的方法getAnnotation(),获取传入注解类型的注解

MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);

//拿到注解中的属性

String name = myAnnotation.Name();

String age = myAnnotation.Age();

System.out.println("name:"+name +" age:"+age);

}

System.out.println("-----------------------------------");

//AnnotatedElement接口中的方法getAnnotations(),获取所有注解

Annotation[] annotations = method.getAnnotations();

//循环注解数组打印出注解类型的名字

for (Annotation annotation : annotations) {

System.out.println(annotation.annotationType().getName());

}

}

打印结果为:

this is a method

name:Dean age:25

-----------------------------------

java.lang.Deprecated

gxy.text.annotation.MyAnnotation

分割线上:介绍了如何使用AnnotatedElement接口中的方法和反射去调用注解

分割线下:证明了只有定义了Retention的Policy为Runtime的注解才可以被反射读取出来

下一篇文章分析一下在JUnit中反射与注解的使用和原理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: