Java反射 Introspector
2024-08-24 09:30:29
一、解释
Introspector 内省,自我检查。
位于java中的java.beans包中,其原文说明文为:
The Introspector class provides a standard way for tools to learn about the properties, events, and methods supported by a target Java Bean.
中文大意为
Introspector提供了一种标准的方式作为工具来获取类的属性,时间,方法。
通常用在反射中,查看类的内部信息。
以下为收集到的一个,空间换时间的反射类。
// 类属性缓存,空间换时间
private static final ConcurrentMap, PropertyDescriptor[]> classPropCache =
new ConcurrentHashMap, PropertyDescriptor[]>(64);
/**
* 获取Bean的属性
* @param bean
* @return
*/
private static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
Class beanClass = bean.getClass();
PropertyDescriptor[] cachePds = classPropCache.get(beanClass);
if (null != cachePds) {
return cachePds;
}
try {
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
cachePds = beanInfo.getPropertyDescriptors();
classPropCache.put(beanClass, cachePds);
return cachePds;
} catch (IntrospectionException e) {
throw new RuntimeException(e);
}
}
/**
* 获取Bean的属性
* @param bean bean
* @param propertyName 属性名
* @return 属性值
*/
public static Object getProperty(Object bean, String propertyName) {
PropertyDescriptor[] beanPds = getPropertyDescriptors(bean);
for (PropertyDescriptor propertyDescriptor : beanPds) {
if (propertyDescriptor.getName().equals(propertyName)){
Method readMethod = propertyDescriptor.getReadMethod();
if (null == readMethod) {
continue;
}
if (!readMethod.isAccessible()) {
readMethod.setAccessible(true);
}
try {
return readMethod.invoke(bean);
} catch (Throwable ex) {
throw new RuntimeException("Could not read property '" + propertyName + "' from bean", ex);
}
}
}
return null;
}
/**
* 设置Bean属性
* @param bean bean
* @param propertyName 属性名
* @param value 属性值
*/
public static void setProperty(Object bean, String propertyName, Object value) {
PropertyDescriptor[] beanPds = getPropertyDescriptors(bean);
for (PropertyDescriptor propertyDescriptor : beanPds) {
if (propertyDescriptor.getName().equals(propertyName)){
Method writeMethod = propertyDescriptor.getWriteMethod();
if (null == writeMethod) {
continue;
}
if (!writeMethod.isAccessible()) {
writeMethod.setAccessible(true);
}
try {
writeMethod.invoke(bean, value);
} catch (Throwable ex) {
throw new RuntimeException("Could not set property '" + propertyName + "' to bean", ex);
}
}
}
}
最新文章
- linux fdisk命令使用
- CSS实战中经常出现的问题。
- static 修饰内部类
- Windows 内存架构
- JS判断用户是否在线的方法
- [GC]一个简单的Garbage Collector的实现
- activemq spring 配置
- DHTMLX地图开发参考示例摘录
- js在web绘制在页上的圆
- HTML5网页制作好好玩啊
- 微信H5支付 遇到坑的一些解决方法
- cdh 安装系列2--cdh manager product 安装
- Android硬件入门-照相机
- docker tag 详解
- ajax响应报文可以被浏览器缓存的必要条件
- hasura graphql 角色访问控制
- C# 解压gzip文件(.tgz)
- 【Java】使用CSVUtils生成文件并供下载
- 北京Uber优步司机奖励政策(3月23日)
- ACE线程管理机制-并发控制(2)
热门文章
- 【Python】微博自动抢红包
- 如何生动形象、切中要点地讲解 OSI 七层模型和两主机传输过程
- Web通信
- java中定义enum示例
- css按钮口诀 - CSS BUG顺口溜
- JS 对应CSS 样式
- [转]MySQL-5.7 Update语句详解
- 解密SVM系列(二):SVM的理论基础
- Exception in thread ";main"; java.lang.UnsatisfiedLinkError: org.apache.hadoop.io .nativeio.NativeIO$Windows.createDirectoryWithMode0(Ljava/lang/String;I)V
- 微信怎样做SEO