内省详解(Introspector/BeanInfo/MethodDescriptor/PropertyDescriptor)
内省(Introspector)概念
内省Introspector 是Java提供的操作 JavaBean 的 API,用来访问某个属性的 getter/setter 方法。对于一个标准的 JavaBean 来说,它包括属性、get 方法和 set 方法,这是一个约定俗成的规范。为此 sun 提供了 Introspector 工具包,来使开发者更好或者更灵活的操作 JavaBean。
例如:User类中有个name属性,那我们可以通过getName/setName来获取/设置name的值,内省就是通过Java提供的API访问属性的getName()/setName()方法。
内省与反射的区别
在计算机科学中,内省是指计算机程序在运行时(Run time)检查对象(Object)类型的一种能力,通常也可以称作运行时类型检查。 不应该将内省和反射混淆。相对于内省,反射更进一步,是指计算机程序在运行时(Run time)可以访问、检测和修改它本身状态或行为的一种能力。
内省常用类说明
Introspector
类提供了的 getBeanInfo()方法获取BeanInfo对象,可以拿到一个 JavaBean 的所有信息BeanInfo
通过getPropertyDescriptors() 方法和 getMethodDescriptors()方法可以获取到PropertyDescriptors、MethodDescriptors对象MethodDescriptor
类可以获得方法的元信息,比如方法名,参数个数,参数字段类型等- getMethod()获取方法的Method对象
- getParameters() 获取方法的所有参数Parameter列表
- getParameterTypes()获取方法的参数ParameterType列表
PropertyDescriptor
类的主要方法- getPropertyType(),获得属性的Class对象
- getReadMethod()/getWriteMethod(),获得用于读取/写入属性值的方法
- setReadMethod(Method readMethod)/setWriteMethod(Method writeMethod),设置用于读取/写入属性值的方法
代码演示
MethodDescriptor
详解// 使用Introspector获取BeanInfo对象
BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
// 使用BeanInfo对象获取到MethodDescriptor列表
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
for (MethodDescriptor methodDescriptor : methodDescriptors) {
// 获取方法Method对象
Method method = methodDescriptor.getMethod();
// 获取方法名称
String methodName = method.getName(); // 参数类型列表
List<String> parameterTypeNames = new ArrayList<>();
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes!=null && parameterTypes.length>0){
Arrays.stream(parameterTypes).forEach(x-> {
String name = x.getName();
parameterTypeNames.add(name);
});
} // 获取方法参数Parameter数组
Parameter[] parameters = method.getParameters();
// 参数名称列表
List<String> parameterNames = new ArrayList<>();
if (parameters!=null && parameters.length>0){
parameterNames = Arrays.stream(parameters).map(Parameter::getName).collect(Collectors.toList());
} System.out.println("方法名称:"+methodName +" 参数类型列表:"+parameterTypeNames+" 参数名称列表:"+parameterNames);
PropertyDescriptors
详解// 使用Introspector获取BeanInfo对象
BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
// 使用BeanInfo对象获取到PropertyDescriptor列表
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor :propertyDescriptors) {
// 获取属性名称
String name = propertyDescriptor.getName();
if ("class".equals(name)){
continue;
}
// 获取setter方法
Method writeMethod = propertyDescriptor.getWriteMethod();
// 获取getter方法
Method readMethod = propertyDescriptor.getReadMethod();
System.out.println("属性名称:"+name+
" 赋值方法:"+writeMethod.getName()+
" 获取方法:"+readMethod.getName());
} // 利用反射创建对象
User user = User.class.newInstance();
// 创建PropertyDescriptor对象
PropertyDescriptor propertyDescriptor = new PropertyDescriptor("name",User.class); // 获取写入方法并执行
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(user,"Rangers"); // 获取读取方法并执行
Method readMethod = propertyDescriptor.getReadMethod();
Object readPropertyValue = readMethod.invoke(user);
System.out.println("ReadMethod获取到到属性值:"+readPropertyValue);
System.out.println("getter获取到到属性值:"+user.getName()); // 设置写入方法并执行
Method writeMd = User.class.getDeclaredMethod("setOoo",String.class);
propertyDescriptor.setWriteMethod(writeMd);
Method writeMethodAgain = propertyDescriptor.getWriteMethod();
writeMethodAgain.invoke(user, "ooo");
System.out.println("设置写入方法重新赋值:"+user.getName());
内省详解(Introspector/BeanInfo/MethodDescriptor/PropertyDescriptor)的更多相关文章
- Java内省详解
内省和反射有什么区别: 反射式在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态. 内省机制是通过反射来实现的,BeanIn ...
- Java基础-反射(reflect)技术详解
Java基础-反射(reflect)技术详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.类加载器 1>.JVM 类加载机制 如下图所示,JVM类加载机制分为五个部分 ...
- Spring之IOC原理及代码详解
一.什么是IOC 引用 Spring 官方原文:This chapter covers the Spring Framework implementation of the Inversion of ...
- 常见 jar包详解
常见 jar包详解 jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期 ...
- MyBatis Generator 详解
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
- MyBatis Generator 详解 【转来纯为备忘】
版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com 目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...
- [转]application.properties详解 --springBoot配置文件
本文转载:http://blog.csdn.net/lpfsuperman/article/details/78287265###; # spring boot application.propert ...
- application.properties详解 --springBoot配置文件【转载】
# spring boot application.properties配置的各个属性详解 # 该示例文件作为标准提供.(官方文档 翻译过来的) # 还是花了些功夫翻译,各位如果转发,请留下本文地址, ...
- 类名.class 与 类名.this 详解
类名.class 与 类名.this 详解 今天在看 PropertyPlaceholderConfigurer 源码时,突然看到一个 PropertyPlaceholderConfigurer.th ...
随机推荐
- Python 是什么语言
Python 是 解释型语言,强类型定义语言,动态类型定义语言 编译型语言 & 解释型语言 编译型语言:代码在执行前,需要编译(成机器语言文件,如 .exe 文件):以后再运行时,直接使用编译 ...
- Linux-源码安装及FPM打包
目录 源码安装 制作RPM包(使用FPM工具) 安装rpm后要执行的脚本(优化版) 源码安装 这里举例Nginx的源码安装,需要前往Nginx官网找到稳定版本源码安装包下载. ## 源码安装nginx ...
- Kubernets二进制安装(8)之部署四层反向代理
四层反向代理集群规划 主机名 角色 IP地址 mfyxw10.mfyxw.com 4层负载均衡(主) 192.168.80.10 mfyxw20.mfyxw.com 4层负载均衡(从) 192.168 ...
- kubernetes实战-配置中心(一)configmap资源
在我们的环境中测试使用configmap资源,需要先对我们的环境进行一些准备,首先将dubbo服务调整为0个pod ,然后把zookeeper进行拆分: 拆分zk环境,模拟测试环境跟生产环境: 停止z ...
- codeforces 1076E Vasya and a Tree 【dfs+树状数组】
题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...
- sql-libs(6) 双引号的报错注入
payload:http://192.168.48.130/sqli-laaess-6/?id=1" and updatexml(1,concat(0x7e,(SELECT schema_n ...
- illustrating javascript prototype & prototype chain
illustrating javascript prototype & prototype chain 图解 js 原型和原型链 proto & prototype func; // ...
- IndexedDB All In One
IndexedDB All In One https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API https://develope ...
- taro & Block
taro & Block https://nervjs.github.io/taro/docs/children.html#注意事项-1 import Taro, { Component, E ...
- Nodejs file path to url path
import * as path from 'path'; import * as url from 'url'; const savePath = path.join('public', 'imag ...