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

来源:互联网 发布:我淘宝主营不超过80 编辑:程序博客网 时间:2024/06/06 00:50

Annotation(注解)简介:

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

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

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


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

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

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

Suppress Warnings注解表示抑制警告


如何自定义注解:

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

//使用@interface来声明一个注解(实际上是自动继承了java.lang.annotation.Annotation接口)public @interface AnnotationTest {String value1() default "hello";  //为注解设置String类型的属性Value1,并使用defalut关键字设置默认值EnumTest value2();//设置枚举类型的value2String[] value3();//设置数组类型的value3}
如何来使用注解呢,如下:

@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这种赋值方式,如下:

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的方法上

@Target(ElementType.METHOD)public @interface MyTarget { String hello() default "hello";}

@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是否出现

通过这些方法,配合反射我们就可以在程序运行时拿到注解的内容了,例子如下:
@Retention(RetentionPolicy.RUNTIME)//定义一个注解,使用Retention标注为RUNTIMEpublic @interface MyAnnotation {String hello() default "hello";String world();}

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

@Retention(RetentionPolicy.CLASS)//定义一个注解,Retention标注为RUNTIMEpublic @interface MyAnnotation2 {String hello() default "hello"; //设置默认值为hello}
自定义的另一个注解Retention标示为class

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为CLASSpublic void TestMethod() {System.out.println("this is a method");}}
定义一个TestMethod方法,给他标示了4个注解,其中2个java自带的,2个我们自定义的。注解的的Retention属性各不相同。

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

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中反射与注解的使用和原理

6 2
原创粉丝点击