上一篇我们说了Java反射之数组的反射应用

这篇我们来模拟实现那些javabean的框架(BeanUtils)的基本操作。

[一] 什么是JavaBean

JavaBean 是一种JAVA语言写成的可重用组件。为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器。JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性,通过set和get方法获取。

一般我们根据这种方法命名规则,通过反射获得某个或者设置某个属性的时候,假如这个属性名为”x",那么,就会处理以下几步:

1、将x变为大写X,判断x后是否还有字母,有则首字母大写

2、在x前加get

[二] 对JavaBean的复杂内省操作

这是一种比较笨重的方式:

Bean类:

package club.leyvan.muzile;

public class Bean {
private int x = 10;
private int y = 20;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
} }

测试类:

	public static void main(String[] args) throws Exception {
Bean bean = new Bean();
System.out.println(getProperty(bean, "x"));
setProperty(bean, "y", 8);
System.out.println(getProperty(bean,"y"));
} /**
* 根据属性名获得属性方法
* @param obj
* @param propertyName
* @return
* @throws Exception
*/
public static Object getProperty(Object obj,String propertyName) throws Exception{
Object retVal = null;
//通过内省获得描述对象
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
//通过描述对象获得该类的所有属性描述
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
//迭代所有属性,获得与规则相同的方法。
for(PropertyDescriptor pd : pds){
//如果该属性的名称与方法形参一致
if(pd.getName().equals(propertyName)){
//则调用该属性的get方法
Method method = pd.getReadMethod();
//get方法都是无参数的
retVal = method.invoke(obj);
}
} return retVal;
} /**
* 根据属性名设置属性的方法
* @param obj
* @param propertyName
* @param value
* @throws Exception
*/
public static void setProperty(Object obj,String propertyName,Object value) throws Exception{
//通过内省获得描述对象
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
//通过描述对象获得该类的所有属性描述
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
//迭代所有属性,获得与规则相同的方法。
for(PropertyDescriptor pd : pds){
//如果该属性的名称与方法形参一致
if(pd.getName().equals(propertyName)){
//则调用该属性的set方法
Method method = pd.getWriteMethod();
//set方法传入参数
method.invoke(obj,value);
}
}
}

结果:

10
8

将原本等于20的y改为了8

[三] 对JavaBean的简单内省操作

上面是一种笨拙的方式,有更为简单的方法:

	public static void main(String[] args) throws Exception {
Bean bean = new Bean();
System.out.println(getProperty(bean, "x"));
setProperty(bean, "y", 39);
System.out.println(getProperty(bean,"y"));
} public static Object getProperty(Object obj,String propertyName) throws Exception{
Object retVal = null;
//直接获得属性描述对象
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
//根据属性,获得get方法
Method method = pd.getReadMethod();
//调用方法
retVal = method.invoke(obj);
return retVal;
} public static void setProperty(Object obj,String propertyName,Object value) throws Exception{
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
Method method = pd.getWriteMethod();
method.invoke(obj, value);
}

结果:

10
39

本期Java反射就介绍到这,谢谢大家!

最新文章

  1. 【原创经验分享】WCF之消息队列
  2. 淘宝SDK扒出来的CURL调用含文件上传代码
  3. 2016年11月28日--ADO.Net 查、插、删、改 小练习
  4. 连接linux数据库Oracle时报错ORA-12541: TNS: 无监听程序
  5. Word文字处理器发展演变
  6. #include #import @class 的一些用法区别
  7. EXT layout
  8. HDOJ多校联合第五场
  9. 黑马程序猿——Java中的类载入器
  10. Python学习笔记总结(二)函数和模块
  11. android捕获程序异常退出
  12. 与众不同 windows phone (8) - Tile(磁贴)
  13. luci-bwc
  14. 1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长 (2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。 该类包含有成员变量: radius:public 修饰的double类型radius,表示圆的半径。 x:private修饰的double型变量
  15. FreeRTOS基础以及UIP之协程--C语言剑走偏锋
  16. 运行gpg --gen-key生成key时出现卡住的问题
  17. Python爬虫入门教程 47-100 mitmproxy安装与安卓模拟器的配合使用-手机APP爬虫部分
  18. Graph图总结
  19. 旋转矩阵 The Rotation Matrix
  20. Fiddler设置抓取FireFox火狐的包

热门文章

  1. SCI|EI|ISTP|万方|istic|NSTL|CASTD|CNKI|nlc|ethesys|CALIS|CETD|proquest|NDLTD|中国科学院学位论文检索系统|学位论文
  2. [LC] 362. Design Hit Counter
  3. LeetCode Day 8
  4. 为什么jdk1.8 HashMap的容量一定要是2的n次幂
  5. SHELL用法八(Grep语句)
  6. MIAME|Highwire press
  7. Linux Ubuntu 常见的压缩命令
  8. shell清除文件内容脚本
  9. java虚拟机的运行原理
  10. Spring5源码分析(1)设计思想与结构