[03] 处理注解:反射
1、AnnotatedElement接口
- boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
- 这里的传参实际上就是传入 ”某个注解.class“
/** * Represents an annotated element of the program currently running in this* VM. This interface allows annotations to be read reflectively. All* annotations returned by methods in this interface are immutable and* serializable. It is permissible for the caller to modify the* arrays returned by accessors for array-valued enum members; it will* have no affect on the arrays returned to other callers.** <p>If an annotation returned by a method in this interface contains* (directly or indirectly) a {@link Class}-valued member referring to* a class that is not accessible in this VM, attempting to read the class* by calling the relevant Class-returning method on the returned annotation* will result in a {@link TypeNotPresentException}.** <p>Similarly, attempting to read an enum-valued member will result in* a {@link EnumConstantNotPresentException} if the enum constant in the* annotation is no longer present in the enum type.* * <p>Finally, Attempting to read a member whose definition has evolved* incompatibly will result in a {@link* java.lang.annotation.AnnotationTypeMismatchException} or an* {@link java.lang.annotation.IncompleteAnnotationException}.** @since 1.5* @author Josh Bloch*/
/**
* Represents an annotated element of the program currently running in this
* VM. This interface allows annotations to be read reflectively. All
* annotations returned by methods in this interface are immutable and
* serializable. It is permissible for the caller to modify the
* arrays returned by accessors for array-valued enum members; it will
* have no affect on the arrays returned to other callers.
*
* <p>If an annotation returned by a method in this interface contains
* (directly or indirectly) a {@link Class}-valued member referring to
* a class that is not accessible in this VM, attempting to read the class
* by calling the relevant Class-returning method on the returned annotation
* will result in a {@link TypeNotPresentException}.
*
* <p>Similarly, attempting to read an enum-valued member will result in
* a {@link EnumConstantNotPresentException} if the enum constant in the
* annotation is no longer present in the enum type.
*
* <p>Finally, Attempting to read a member whose definition has evolved
* incompatibly will result in a {@link
* java.lang.annotation.AnnotationTypeMismatchException} or an
* {@link java.lang.annotation.IncompleteAnnotationException}.
*
* @since 1.5
* @author Josh Bloch
*/
- Class 类
- Constructor 构造器
- Field 类的成员变量
- Method 类的方法
- Package 类的包
- //判断该程序元素上是否包含指定类型的注解,存在返回true,否则返回false
- boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);
- //返回该程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回null
- <T extends Annotation> T getAnnotation(Class<T> annotationClass);
- //返回该程序元素上存在的所有注解
- Annotation[] getAnnotations();
- //返回直接存在于该元素上的所有注解(与该接口中其他方法不同,该方法将忽略继承的注解)
- //如果没有注解直接存在于此元素上,则返回长度为零的一个数组
- //该方法调用者可以随意修改返回的数组,不会对其他调用者返回的数组产生影响
- Annotation[] getDeclaredAnnotations();
2、AnnotatedElement使用示例
2.1 自定义注解
/*** 水果名称注解*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {String value() default "";
}/*** 水果颜色注解*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {/*** 颜色枚举*/public enum Color {BULE, RED, GREEN}/*** 颜色属性*/public Color fruitColor() default Color.GREEN;
}/*** 水果供应商注解*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {/*** 供应商编号*/public int id() default -1;/*** 供应商名称*/public String name() default "";/*** 供应商地址*/public String address() default "";}
/**
* 水果名称注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
String value() default "";
}
/**
* 水果颜色注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
/**
* 颜色枚举
*/
public enum Color {BULE, RED, GREEN}
/**
* 颜色属性
*/
public Color fruitColor() default Color.GREEN;
}
/**
* 水果供应商注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {
/**
* 供应商编号
*/
public int id() default -1;
/**
* 供应商名称
*/
public String name() default "";
/**
* 供应商地址
*/
public String address() default "";
}
2.2 使用自定义注解的类
/*** 苹果类*/
public class Apple {/*** 苹果名称*/@FruitName("红富士")private String appleName;/*** 苹果颜色*/@FruitColor(fruitColor = FruitColor.Color.RED)private String appleColor;/*** 苹果供应商*/@FruitProvider(id = 1, name = "陕西红富士集团", address = "陕西省西安市延安路89号红富士大厦")private String appleProvider;public String getAppleName() {return appleName;}public void setAppleName(String appleName) {this.appleName = appleName;}public String getAppleColor() {return appleColor;}public void setAppleColor(String appleColor) {this.appleColor = appleColor;}public String getAppleProvider() {return appleProvider;}public void setAppleProvider(String appleProvider) {this.appleProvider = appleProvider;}
}
/**
* 苹果类
*/
public class Apple {
/**
* 苹果名称
*/
@FruitName("红富士")
private String appleName;
/**
* 苹果颜色
*/
@FruitColor(fruitColor = FruitColor.Color.RED)
private String appleColor;
/**
* 苹果供应商
*/
@FruitProvider(id = 1, name = "陕西红富士集团", address = "陕西省西安市延安路89号红富士大厦")
private String appleProvider;
public String getAppleName() {
return appleName;
}
public void setAppleName(String appleName) {
this.appleName = appleName;
}
public String getAppleColor() {
return appleColor;
}
public void setAppleColor(String appleColor) {
this.appleColor = appleColor;
}
public String getAppleProvider() {
return appleProvider;
}
public void setAppleProvider(String appleProvider) {
this.appleProvider = appleProvider;
}
}
2.3 注解处理
public class FruitInfoUtil {/*** 获取水果信息* @param clazz 类对象*/public static void getFruitInfo(Class<?> clazz) {String strFruitName;String strFruitColor;String strFruitProvider;//获取该类对象包含的所有属性对象Field[] fields = clazz.getDeclaredFields();//遍历属性对象for (Field field : fields) {//如果属性包含的注解是FruitNameif (field.isAnnotationPresent(FruitName.class)) {FruitName fruitName = field.getAnnotation(FruitName.class);strFruitName = "水果名称:" + fruitName.value();System.out.println(strFruitName);}//如果属性包含的注解是FruitColorelse if (field.isAnnotationPresent(FruitColor.class)) {FruitColor fruitColor = field.getAnnotation(FruitColor.class);strFruitColor = "水果颜色:" + fruitColor.fruitColor().toString();System.out.println(strFruitColor);}//如果属性包含的注解是FruitProviderelse if (field.isAnnotationPresent(FruitProvider.class)) {FruitProvider fruitProvider = field.getAnnotation(FruitProvider.class);strFruitProvider = "供应商编号:" + fruitProvider.id()+ ";供应商名称:" + fruitProvider.name()+ ";供应商地址:" + fruitProvider.address();System.out.println(strFruitProvider);}}}}
public class FruitInfoUtil {
/**
* 获取水果信息
* @param clazz 类对象
*/
public static void getFruitInfo(Class<?> clazz) {
String strFruitName;
String strFruitColor;
String strFruitProvider;
//获取该类对象包含的所有属性对象
Field[] fields = clazz.getDeclaredFields();
//遍历属性对象
for (Field field : fields) {
//如果属性包含的注解是FruitName
if (field.isAnnotationPresent(FruitName.class)) {
FruitName fruitName = field.getAnnotation(FruitName.class);
strFruitName = "水果名称:" + fruitName.value();
System.out.println(strFruitName);
}
//如果属性包含的注解是FruitColor
else if (field.isAnnotationPresent(FruitColor.class)) {
FruitColor fruitColor = field.getAnnotation(FruitColor.class);
strFruitColor = "水果颜色:" + fruitColor.fruitColor().toString();
System.out.println(strFruitColor);
}
//如果属性包含的注解是FruitProvider
else if (field.isAnnotationPresent(FruitProvider.class)) {
FruitProvider fruitProvider = field.getAnnotation(FruitProvider.class);
strFruitProvider = "供应商编号:" + fruitProvider.id()
+ ";供应商名称:" + fruitProvider.name()
+ ";供应商地址:" + fruitProvider.address();
System.out.println(strFruitProvider);
}
}
}
}
2.4 测试和结果输出
public class TestAnnotation {public static void main(String[] args) {FruitInfoUtil.getFruitInfo(Apple.class);}
}//结果输出
水果名称:红富士
水果颜色:RED
供应商编号:1;供应商名称:陕西红富士集团;供应商地址:陕西省西安市延安路89号红富士大厦
public class TestAnnotation {
public static void main(String[] args) {
FruitInfoUtil.getFruitInfo(Apple.class);
}
}
//结果输出
水果名称:红富士
水果颜色:RED
供应商编号:1;供应商名称:陕西红富士集团;供应商地址:陕西省西安市延安路89号红富士大厦
3、参考链接
- 深入理解Java:注解(Annotation)--注解处理器
转载于:https://www.cnblogs.com/deng-cc/p/7462588.html
[03] 处理注解:反射相关推荐
- java学习~线程、注解反射、Swagger
这几天一直在学习java基础,所以跟着老师一起学习做的总结. 项目地址:StudyJava项目地址 一:线程学习 这是代码的布局以及对各个类的描述和总结.可以借鉴. 线程代码地址:thread No1 ...
- Java 枚举注解反射
4-9 枚举&注解&反射 枚举 引入的目的:用于定义有限数量的一组同类常量,限定了使用者在使用时常量的取值范围. Java中枚举的演变: java1.5前使用类+final修饰变量+私 ...
- 使用注解+反射实现List与树形结构的相互转换
应用场景: 提示:此种写法只适用于数据量小的转换,数据量大时请移步另一个方案 [一次循环实现List转树形结构(不使用递归)](https://blog.csdn.net/weixin_3730496 ...
- 利用注解 + 反射消除重复代码(Java项目)
作者 | Leilei Chen 来源 | https://llchen60.com/ 1. 案例场景 假设银行提供了一些 API 接口,对参数的序列化有点特殊,不使用 JSON,而是需要我们把参数依 ...
- Java_注解 反射 字节码 类加载机制
一.注解 1. 二.反射 1.动态语言:python.js 程序运行时,可以改变程序结构或变量类型. java的动态性:利用反射机制.字节码操作获得类似动态语言的特性. 2.反射机制 ref ...
- 利用注解 + 反射消除重复代码,妙!
1.1 案例场景 假设银行提供了一些 API 接口,对参数的序列化有点特殊,不使用 JSON,而是需要我们把参数依次拼在一起构成一个大字符串: 1)按照银行提供的API文档顺序,将所有的参数构成定长的 ...
- java 重复代码优化_利用注解 + 反射消除重复代码(Java项目)
1. 案例分析 1.1 案例场景 假设银行提供了一些 API 接口,对参数的序列化有点特殊,不使用 JSON,而是需要我们把参数依次拼在一起构成一个大字符串 按照银行提供的API文档顺序,将所有的参数 ...
- 枚举注解反射整合详解
一丶枚举 1 ** 2 * 一.枚举类的使用 3 * 1.枚举类的理解:类的对象只有有限个,确定的.我们称此类为枚举类 4 * 2.当需要定义一组常量时,强烈建议使用枚举类 5 * 3.如果枚举类中只 ...
- JAVA基础,注解反射机制
文章目录 注解 (非常重要) 什么是注解 基本注解 @Override @Deprecated @SuppressWarnings @SafeVarargs @FunctionalInterface ...
最新文章
- 干掉 ZooKeeper?阿里为什么不用 ZK 做服务发现?
- 南安出台行动方案推进智慧城市建设
- 4分钟极速甩脂训练!抛开借口!繁忙族必备!
- 【图像分类】从数据集和经典网络开始
- Python操作excel文件根据某一字段将相同的类拆分
- php 组合模式,php设计模式(十三)透明组合模式
- Sublime Text 3 Mac常用快捷键与注意事项
- 重庆大二学生花了6年写出了操作系统
- Java 线程之间通信
- bzoj1005 [HNOI2008]明明的烦恼 prufer+组合数学
- 计算机无法上网修复工具,lsp怎么修复解决上不了网问题汇总
- 解决input输入中文时,拼音在输入框内会触发input事件的问题
- mysql 存储过程 对象_【MYSQL笔记3】MYSQL过程式数据库对象之存储过程的调用、删除和修改...
- Kotlin基础篇(二)-作用域函数
- 地址栏中输入IP地址提示指定的网络名不再可用的解决方法
- 通过有线网卡共享无线网络
- Docker网络及资源管理
- 该设备正在使用中,请关闭可能使用该设备的所有程序或窗口,然后重试
- 林肯公园(LINKIN PARK)上海演唱会观众座位表(转帖)
- 如何用海思HI3516DV300/3518EV200推流H.264