java学习笔记11--Annotation

 java学习笔记11--Annotation

Annotation:在JDK1.5之后增加的一个新特性,这种特性被称为元数据特性,在JDK1.5之后称为注释,即:使用注释的方式加入一些程序的信息。

java.lang.annotation Annotation接口是所有的Annotation都必须实现的接口。

系统内建的Annotation

在JDK1.5之后,系统中已经建立了如下的三个内建的Annotation类型,用户直接使用即可。

@Override:覆写的Annotation

@Deprecated:不赞成使用的Annotation

@SuppressWarnings:压制安全警告的Annotation

自定义Annotation

Annotation定义格式:

【public】 @interface  Annotation名称{

             数据类型   变量名称();

}

  1. public @interface Meaning {  
  2.     String value();  
  3. }  


之后就直接可以在程序中使用@Meaning的格式

  1. @Meaning(value="itmyhome")  
  2. class Demo{  
  3.       
  4. }  


可以在Annotation中设置一个参数,用来接收变量的内容,如上面的value,使用Annotation的时候也必须给参数赋值如:value="itmyhome"

既然可以设置一个参数,则同时也就可以设置多个参数。

  1. public @interface Meaning {  
  2.     String key();  
  3.     String value();  
  4. }  


此Annotation在使用的时候 需要设置两个参数,一个key一个value

  1. @Meaning(key="hi",value="itmyhome")  
  2. class Demo{  
  3.       
  4. }  


也可以设置一个数组进去

  1. public @interface Meaning {  
  2.     String[] value();  
  3. }  


接收的内容要传递数组

  1. @Meaning(value={"hello","world"})  
  2. class Demo{  
  3.       
  4. }  

以上所定义的全部的Annotation中有一个特点,所有的参数内容需要在使用注释的时候设置上去,那么也可以为一个参数设置默认的内容,在声明的时候使用default即可。

  1. public @interface Meaning {  
  2.     String value() default "";  //默认为空   
  3. }  

在使用的时候就可以不设置值

  1. @Meaning  
  2. class Demo{  
  3.       
  4. }  


在操作中,对于一个Annotation而言有时候会固定期取值范围,只能取固定的几个值,这个时候实际上就需要依靠枚举。

  1. public enum FormItemType {  //定义枚举类型  
  2.     hidden,text,select,date  
  3. }  


定义Annotation

  1. public @interface Meaning {  
  2.     FormItemType value() ;  //设置为枚举类型  
  3. }  


Annotation的取值只能是枚举类型中的值

  1. @Meaning(value=FormItemType.date)  
  2. class Demo{  
  3.       
  4. }  


Retention和RetentionPolicy

在Annotation中,可以使用Retention定义个Annotation的保存范围,此Annotation的定义如下:

  1. @Retention(RetentionPolicy.RUNTIME)  
  2. @Target(ElementType.FIELD)  
  3. public @interface Meaning {  
  4.     FormItemType value() ;  //设置为枚举类型  
  5. }  


在以上的Retetion定义中存在了一个RetentionPolicy的变量,此变量用于指定Annotation的保存范围,RetentionPolicy包含三种范围

在三个范围中,最重要的就是RUNTIME范围,因为在执行的时候起作用。

内建Annotation的RetentionPolicy

三个内建的Annotation的定义:

Override定义采用的是@Retention(RetentionPolicy.SOURCE) 只能在源文件中出现

Deprecated定义采用的是@Retention(RetentionPolicy.RUNTIME),可以在执行时出现

SuppressWarnings定义采用的是@Retention(RetentionPolicy.SOURCE),只能在源文件中出现

一个Annotation如果要是想让其变得有意义,则必须结合反射机制取得Annotaion中设置的全部内容。

在Class类中存在以下几种与Annotation操作有关的方法

  1. package com.itmyhome;  
  2.   
  3. import java.lang.annotation.Annotation;  
  4. import java.lang.reflect.Method;  
  5.   
  6. class Demo{  
  7.     @SuppressWarnings("unchecked")  
  8.     @Deprecated  
  9.     @Override  
  10.     public String toString(){  
  11.         return "hello";  
  12.     }  
  13. }  
  14.   
  15. public class T {  
  16.     public static void main(String[] args) throws Exception{  
  17.         Class<?> c = Class.forName("com.itmyhome.Demo");  
  18.         Method mt = c.getMethod("toString");   //找到toString方法  
  19.         Annotation an[] = mt.getAnnotations(); //取得全部的Annotation  
  20.         for(Annotation a:an){  
  21.             System.out.println(a);  
  22.         }  
  23.           
  24.     }  
  25. }  


此时已经取得了一个Annota。以上的操作实际上是通过三个系统内建的Annotation完成的,也可以自定义一个Annotation

  1. @Retention(RetentionPolicy.RUNTIME)  
  2. @Target(ElementType.METHOD)  
  3. public @interface Meaning {  
  4.     FormItemType value() ;  //设置为枚举类型  
  5. }  
  1. package com.itmyhome;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. class Demo{  
  6.     @Meaning(value=FormItemType.select)  //自定义Annotation  
  7.     @SuppressWarnings("unchecked")  
  8.     @Deprecated  
  9.     @Override  
  10.     public String toString(){  
  11.         return "hello";  
  12.     }  
  13. }  
  14.   
  15. public class T {  
  16.     public static void main(String[] args) throws Exception{  
  17.         Class<?> c = Class.forName("com.itmyhome.Demo");  
  18.         Method mt = c.getMethod("toString");   //找到toString方法  
  19.           
  20.         //指定的注释是否存在于此元素上  
  21.         if(mt.isAnnotationPresent(Meaning.class)){  
  22.             Meaning m = mt.getAnnotation(Meaning.class);  //得到指定的Annotation  
  23.             System.out.println(m.value());                //取得Annotation的值  
  24.         }  
  25.           
  26.     }  
  27. }  


 

@Target

指示注释类型所适用的程序元素的种类。如果注释类型声明中不存在 Target 元注释,则声明的类型可以用在任一程序元素上。如果存在这样的元注释,则编译器强制实施指定的使用限制,如:@Target(ElementType.ANNOTATION_TYPE)

ElementType的保存范围

原文地址:https://www.cnblogs.com/timssd/p/4790456.html