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

Annotation(注解)简介:

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

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

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


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

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

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

Suppress Warnings注解表示抑制警告


如何自定义注解:

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

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

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

  1. @AnnotationTest(value2 = EnumTest.age, value3={""})  
  2. public class AnnotationUsage {  
  3.   
  4.     @AnnotationTest(value1 = "Test", value2 = EnumTest.name, value3={""})  
  5.     String test;  
  6.       
  7.     @AnnotationTest(value1 = "Test", value2 = EnumTest.name, value3={""})  
  8.     public void method(){  
  9.         System.out.println("usage of Annotation");  
  10.     }  
  11. }  

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

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



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

  1. public @interface AnnotationTest {  
  2.     String value();  
  3. }  
  4.   
  5. @AnnotationTest("test")  
  6. public void method(){  
  7.     System.out.println("usage of Annotation");  
  8. }  

修饰注解的“注解”

注解也可以添加注解的“注解”去修饰,常用的有以下两个,一个是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的方法上

  1. @Target(ElementType.METHOD)  
  2. public @interface MyTarget {  
  3.      String hello() default "hello";  
  4. }  
  1. @MyTarget  //这里则会报错,因为他标注在类上面了  
  2. public class MyTargetTest {  
  3.     @MyTarget   //标注在方法上不会报错  
  4.     public void doSomething(){  
  5.         System.out.println("hello world");  
  6.     }  
  7. }  

使用反射调用注解

在以下的类中Class Constructor Field Method Package等类都实现了AnnotatedElement接口
在接口中有以下重要的方法:
getAnnotations(Class annotationType)获取一个指定的annotation类型
getAnnotations() 获取所有的Annotation
getDeclaredAnnotations() 获取声明过的所有Annotation
isAnnotationPresent(Class<? extends Annotation> annotationClass)这个annotation是否出现

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

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

  1. @Retention(RetentionPolicy.CLASS)   //定义一个注解,Retention标注为RUNTIME  
  2. public @interface MyAnnotation2 {  
  3.     String hello() default "hello"; //设置默认值为hello  
  4. }  

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

  1. public class MyTest {  
  2.     @SuppressWarnings("unchecked")  //java自带的注解Retention的policy为SOURCE  
  3.     @Deprecated     //java自带的注解Retention的policy为RUNTIME  
  4.     @MyAnnotation(Name="Dean", Age="25")    //自定义的注解Retention的policy为RUNTIME  
  5.     @MyAnnotation2  //自定义的注解Retention的policy为CLASS  
  6.     public void TestMethod() {  
  7.         System.out.println("this is a method");  
  8.     }  
  9. }  

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

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

  1. public static void main(String[] args) throws Exception {  
  2.           
  3.         MyTest myTest = new MyTest();  
  4.         //通过反射得到TestMethod方法  
  5.         Class<MyTest> c = MyTest.class;  
  6.         Method method = c.getMethod("TestMethod", new Class[]{});  
  7.           
  8.         //AnnotatedElement接口中的方法isAnnotationPresent(),判断传入的注解类型是否存在  
  9.         if (method.isAnnotationPresent(MyAnnotation.class)) {  
  10.             method.invoke(myTest, new Object[]{});  
  11.             //AnnotatedElement接口中的方法getAnnotation(),获取传入注解类型的注解  
  12.             MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);  
  13.             //拿到注解中的属性  
  14.             String name = myAnnotation.Name();  
  15.             String age = myAnnotation.Age();  
  16.             System.out.println("name:"+name +"   age:"+age);  
  17.         }  
  18.           
  19.         System.out.println("-----------------------------------");  
  20.           
  21.         //AnnotatedElement接口中的方法getAnnotations(),获取所有注解  
  22.         Annotation[] annotations = method.getAnnotations();  
  23.         //循环注解数组打印出注解类型的名字  
  24.         for (Annotation annotation : annotations) {  
  25.             System.out.println(annotation.annotationType().getName());  
  26.         }  
  27.     }  

打印结果为: this is a method
name:Dean   age:25
-----------------------------------
java.lang.Deprecated
gxy.text.annotation.MyAnnotation


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

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


下一篇文章分析一下在JUnit中反射与注解的使用和原理

原文地址:https://www.cnblogs.com/Free-Thinker/p/6397417.html