18.3.2从Class上获取信息(注解)

package d18_3_1;
/**
 * Class类上所包含的注解
 * 
 * getAnnotation(Class annotationClass) 获取该元素上指定的类型的注解
 * getAnnotations():返回此元素上存在的所有注解
 * getDeclaredAnnotations():返回直接存在于此元素上的所有注解
 */
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;

@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassInfo4 {

	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 = ClassInfo4.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 = ClassInfo4.class.getAnnotations();

		for (Annotation cAnnotation : classAnnotation) {
			Class annotationType = cAnnotation.annotationType();
			System.out.println("User类上的注解有: " + annotationType);
		}

		System.out.println("----------华丽的分割线--------------");

		// 5、获取方法上的所有注解
		Method method;
		try {
			method = ClassInfo4.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;
	}
}

  

转载于:https://www.cnblogs.com/1020182600HENG/p/7357132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值