copyProperties方法分析

     BeanUtils和PropertyUtils类是许多开源框架中频繁使用的两个工具,它们都能实现将一个类中的属性拷贝到另一个类中,这个功能甚至是spring实现依赖注入的基础。研究一下apache的comon包中如何实现这个两个工具,可以发现它们都是使用java.lang.reflect和java.beans这两个包下的几个类来实现的。

    这里我们通过编写一个将一个类的所有属性拷贝到另一个类的相应属性的方法来分析是如何实现拷贝功能的.先把方法放上来:


/** 实现将源类属性拷贝到目标类中
   * @param source
   * @param target
   */

public static void copyProperties(Object source, Object target) {
   try {
        //获取目标类的属性信息
        BeanInfo targetbean = Introspector.getBeanInfo(target.getClass());
        PropertyDescriptor[] propertyDescriptors = targetbean.getPropertyDescriptors();
        //对每个目标类的属性查找set方法,并进行处理
        for (int i = 0; i < propertyDescriptors.length; i++) {
             PropertyDescriptor pro = propertyDescriptors[i];
             Method wm = pro.getWriteMethod();
             if (wm != null) {//当目标类的属性具有set方法时,查找源类中是否有相同属性的get方法
                 BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass());
                 PropertyDescriptor[] sourcepds = sourceBean.getPropertyDescriptors();
                 for (int j = 0; j < sourcepds.length; j++) {
                      if (sourcepds[j].getName().equals(pro.getName())) { //匹配
                           Method rm = sourcepds[j].getReadMethod();
                           //如果方法不可访问(get方法是私有的或不可达),则抛出SecurityException
                           if (!Modifier.isPublic(rm.getDeclaringClass().getModifiers())) {
                                rm.setAccessible(true);
                           }
                          //获取对应属性get所得到的值
                          Object value = rm.invoke(source,new Object[0]);
                          if (!Modifier.isPublic(wm.getDeclaringClass().getModifiers())) {
                               wm.setAccessible(true);
                          }
                          //调用目标类对应属性的set方法对该属性进行填充
                          wm.invoke((Object) target, new Object[] { value });
                          break;
                      }
                 }
              }
          }
   } catch (IntrospectionException e) {
       e.printStackTrace();
   } catch (IllegalArgumentException e) {
       e.printStackTrace();
   } catch (IllegalAccessException e) {
       e.printStackTrace();
  } catch (InvocationTargetException e) {
      e.printStackTrace();
  }
}

两个工具的其他方法实现虽然有点差别,但原理都跟上面的例子差不多,有兴趣的话可以写个测试类试试是否可以使用.

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值