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;
}
}

  

最新文章

  1. Django配置和初探
  2. js的一些属性
  3. lambda表達式
  4. 初始jQuery
  5. ***iOS 项目的目录结构能看出你的开发经验
  6. jdbc学习(一)——SqlServer、Oracle和MySQL
  7. XCode实用好用插件集锦
  8. Linux下安装Perl和Perl的DBI模块
  9. SSD和HDD的区别
  10. 简单动态规划——三逆数的O(N^2)解法!
  11. cell的复用机制
  12. [JDK8]读写锁的改进:StampedLock
  13. mybatis 自动生成文件配置
  14. mysql数据库备份与导入
  15. 20175310 迭代和JDB
  16. [leetcode]101. Symmetric Tree对称树
  17. SpringBoot Cmd运行Jar文件指定active文件的命令如下
  18. zabbix 安装错误汇总
  19. 【学习笔记】--- 老男孩学Python,day16-17 初识面向对象,类名称空间,查询顺序,组合
  20. NEERC 15 (10/12)

热门文章

  1. 树莓派wiringPi经常使用的函数介绍
  2. BingMap地图怎样显示中文
  3. git常用知识笔记
  4. android 提示
  5. ie9浏览器中h标签的嵌套问题
  6. Quartz -第一篇-入门
  7. HDU1565 方格取数(1) —— 状压DP or 插头DP(轮廓线更新) or 二分图点带权最大独立集(最小割最大流)
  8. html5--6-19 CSS3中的文字与字体
  9. 修改fuse库成功
  10. bzoj1207 [HNOI2004]打鼹鼠——LIS