首页 > 代码库 > Java 使用反射获取类、方法、属性上的注释

Java 使用反射获取类、方法、属性上的注释

有的时候我们想使用反射获取某个类的注释、方法上的注释、属性上的注释。

下面是一个简单的例子。里面包括了上面提到的三个点。

package com.mine.practice.reflectfield;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
  * 
  * @author 2014-11-10 下午01:54:48 
  * @version V1.0  
 */
@XmlRootElement(name="user")
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

	private String pwd;

	@XmlElement(name = "ID")
	private int id;
	
	@XmlAttribute
	@XmlElement
	private String name;
	
	/***
	 *  1、获取属性上的指定类型的注释
	 *  2、获取属性上的指定类型的注释的指定方法
	 *  3、获取属性上的所有注释
	 *  4、获取类上的所有注释
	 *  5、获取方法上的所有注释
	  * @author 2014-11-10 下午02:18:24
	  * @param args 
	 */
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		
		Field[] fields =  User.class.getDeclaredFields();
		
		for(Field f : fields){
			String filedName = f.getName();
			System.out.println("属性名称:【"+filedName+"】");

			//1、获取属性上的指定类型的注释
			Annotation annotation = f.getAnnotation(XmlElement.class);
			
			//有该类型的注释存在
			if (annotation!=null) {
				//强制转化为相应的注释	
				XmlElement xmlElement = (XmlElement)annotation;
				//3、获取属性上的指定类型的注释的指定方法
				//具体是不是默认值可以去查看源代码
				if (xmlElement.name().equals("##default")) {
					System.out.println("属性【"+filedName+"】注释使用的name是默认值: "+xmlElement.name());
				}else {
					System.out.println("属性【"+filedName+"】注释使用的name是自定义的值: "+xmlElement.name());
				}
			}

			//2、获取属性上的所有注释
			Annotation[] allAnnotations = f.getAnnotations();
			
			for(Annotation an : allAnnotations){
				
				Class annotationType = an.annotationType();
				
				System.out.println("属性【"+filedName+"】的注释类型有: " + annotationType);
			}
			System.out.println("----------华丽的分割线--------------");
		}
		
		//4、获取类上的所有注释
		Annotation[] classAnnotation = User.class.getAnnotations();
		
		for(Annotation cAnnotation : classAnnotation){
			Class annotationType =  cAnnotation.annotationType();
			System.out.println("User类上的注释有: " +annotationType);
		}
		
		System.out.println("----------华丽的分割线--------------");
		
		// 5、获取方法上的所有注释
		Method method;
		try {
			method = User.class.getMethod("setPwd",String.class);
			
			Annotation[] methodAnnotations = method.getAnnotations();

			for(Annotation me : methodAnnotations){
				Class annotationType =  me.annotationType();
				System.out.println("setPwd方法上的注释有: " + annotationType);
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}

	@XmlElement
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getPwd() {
		return pwd;
	}
}


运行结果如下所示

属性名称:【pwd】
----------华丽的分割线--------------
属性名称:【id】
属性【id】注释使用的name是自定义的值: ID
属性【id】的注释类型有: interface javax.xml.bind.annotation.XmlElement
----------华丽的分割线--------------
属性名称:【name】
属性【name】注释使用的name是默认值: ##default
属性【name】的注释类型有: interface javax.xml.bind.annotation.XmlAttribute
属性【name】的注释类型有: interface javax.xml.bind.annotation.XmlElement
----------华丽的分割线--------------
User类上的注释有: interface javax.xml.bind.annotation.XmlAccessorType
User类上的注释有: interface javax.xml.bind.annotation.XmlRootElement
----------华丽的分割线--------------
setPwd方法上的注释有: interface javax.xml.bind.annotation.XmlElement



Java 使用反射获取类、方法、属性上的注释