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

深入理解Java的Annotation系列-第三部分 解析注解信息

2017-08-22 17:25 746 查看
九、使用Java的反射机制解析注解信息

    java.lang.reflect 包下主要包含一些实现反射功能的工具类,实际上,java.lang.reflect包所有提供的反射API扩充了读取运行时Annotation信息的能力。当一个Annotation类型被定义为运行时的Annotation后,该注解才能是运行时可见,当class文件被装载时被保存在class文件中的Annotation才会被虚拟机读取。
AnnotatedElement 接口是所有程序元素(Class、Method和Constructor)的父接口,所以程序通过反射获取了某个类的AnnotatedElement对象之后,程序就可以调用该对象的如下四个个方法来访问Annotation信息:

  方法1:<T extends Annotation>T getAnnotation(Class<T> annotationClass):返回改程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回null。
  方法2:Annotation[]getAnnotations():返回该程序元素上存在的所有注解。
  方法3:boolean isAnnotationPresent(Class<?extends Annotation> annotationClass):判断该程序元素上是否包含指定类型的注解,存在则返回true,否则返回false.
  方法4:Annotation[]getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。(如果没有注释直接存在于此元素上,则返回长度为零的一个数组。)该方法的调用者可以随意修改返回的数组;这不会对其他调用者返回的数组产生任何影响。

    还是以上的那个例子,在Pet类中主方法添加如下代码:

    public
static void
main(String[]
args) {
       Pet pet1 = new Pet();
       System.out.println(pet1.showAll());
       System.out.println(Pet.class.getAnnotations().length);
       for (Annotationa : Pet.class.getAnnotations()) {
           if (ainstanceof
PetName) {
              PetName
petname = (PetName)
a;
              System.out.println(petname.value());
           }
           if (ainstanceof
PetType) {
              PetType pettype = (PetType)a;
              System.out.println(pettype.pet_type());
           }
       }
       System.out.println(Pet.class.isAnnotationPresent(PetName.class));
       if (Pet.class.isAnnotationPresent(PetName.class))
{
           PetName
petname = (PetName) Pet.class.getAnnotation(PetName.class);
           System.out.println(petname.value());
       }
       if (Pet.class.isAnnotationPresent(PetType.class))
{
           PetType
pettype = (PetType) Pet.class.getAnnotation(PetType.class);
           System.out.println(pettype.pet_type());
       }
    }

执行的结果如下:

null null
2
mimi
PIG
true
mimi
PIG

再举一个例子:

package cn.com.bochy.authority.ann
4000
otation;
//一、定义一个类注解
/**
* 注解类
* @author
chenzhengfeng
*/ 
import java.lang.annotation.ElementType; 

import java.lang.annotation.Retention; 

import java.lang.annotation.RetentionPolicy; 

import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME

@Target(ElementType.TYPE

public
@interface
ClassAnnotation { 
  String uri(); 
  String desc(); 


package cn.com.bochy.authority.annotation;
//二、定义构造方法注解
/**
* 构造方法注解
* @author
chenzhengfeng
*
*/
import java.lang.annotation.ElementType; 

import java.lang.annotation.Retention; 

import java.lang.annotation.RetentionPolicy; 

import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME)    

@Target(ElementType.CONSTRUCTOR)  

public
@interface
ConstructorAnnotation { 

  String uri(); 
  String desc(); 


 

package cn.com.bochy.authority.annotation;
//三、定义一个字段注解
/**
* 字段注解定义
* @author
chenzhengfeng
*
*/ 
import java.lang.annotation.ElementType; 

import java.lang.annotation.Retention; 

import java.lang.annotation.RetentionPolicy; 

import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME)    

@Target(ElementType.FIELD)  

public
@interface
FieldAnnotation { 
  String uri(); 
  String desc(); 


 

package cn.com.bochy.authority.annotation;
//四、定义方法注解
/**
* @author
chenzhengfeng
*
*/ 
import java.lang.annotation.ElementType; 

import java.lang.annotation.Retention; 

import java.lang.annotation.RetentionPolicy; 

import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME)    

@Target(ElementType.METHOD

public
@interface
MethodAnnotation { 

  String uri(); 
  String desc(); 


 

package cn.com.bochy.authority.annotation.test;
//五、编写测试类
import java.lang.reflect.Constructor; 

import java.lang.reflect.Field; 
import java.lang.reflect.Method;
import cn.com.bochy.authority.annotation.ClassAnnotation;
import cn.com.bochy.authority.annotation.ConstructorAnnotation;
import cn.com.bochy.authority.annotation.FieldAnnotation;
import cn.com.bochy.authority.annotation.MethodAnnotation; 

@ClassAnnotation(desc="The classname", uri="cn.com.bochy.authority.annotation.MyTest") 

public
class
MyTest { 
  @FieldAnnotation(desc="The classfield", uri="cn.com.bochy.authority.annotation.MyTest#id") 

  private
int
id; 
  @ConstructorAnnotation(desc="The classconstructor", uri="cn.com.bochy.authority.annotation.MyTest#MyTest") 

  public MyTest(){} 

  public
int
getId() { 
      return
id; 
  } 
  @MethodAnnotation(desc="The classmethod", uri="cn.com.bochy.authority.annotation.MyTest#setId") 

  public
void
setId(int
id) { 
      this.id =
id; 
  }   
  public
static void
main(String[]
args) throws Exception {        
      Class<MyTest> clazz = MyTest.class;      

      //得到类注解 

      ClassAnnotation
ClassAnnotation = clazz.getAnnotation(ClassAnnotation.class);      

      System.out.println(ClassAnnotation.desc()+" "+ClassAnnotation.uri());         

      //得到构造方法注解 

      Constructor<MyTest> cons =
clazz.getConstructor(new Class[]{});     

      ConstructorAnnotation
ConstructorAnnotation = cons.getAnnotation(ConstructorAnnotation.class);       

      System.out.println(ConstructorAnnotation.desc()+" "+ConstructorAnnotation.uri());      

      //获取方法注解 

      Method method =
clazz.getMethod("setId", new Class[]{int.class});   

      MethodAnnotation
MethodAnnotation = method.getAnnotation(MethodAnnotation.class);    

      System.out.println(MethodAnnotation.desc()+" "+MethodAnnotation.uri()); 

      //获取字段注解 

      Field field =
clazz.getDeclaredField("id");   
      FieldAnnotation
FieldAnnotation = field.getAnnotation(FieldAnnotation.class);   

      System.out.println(FieldAnnotation.desc()+" "+FieldAnnotation.uri() ); 

  }   


运行结果如下:

The class namecn.com.bochy.authority.annotation.MyTest
The class constructorcn.com.bochy.authority.annotation.MyTest#MyTest
The class methodcn.com.bochy.authority.annotation.MyTest#setId
The class fieldcn.com.bochy.authority.annotation.MyTest#id
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息