cglib是一款比较底层的操作java字节码的框架。

下面通过拷贝bean对象来测试BeanCopier的特性:

  1. public class OrderEntity {
  2. private int id;
  3. private String name;
  4. // Getters and setters are omitted
  5. }
  1. public class OrderDto {
  2. private int id;
  3. private String name;
  4. // Getters and setters are omitted
  5. }
  1. public class PropWithDiffType {
  2. private Integer id;
  3. private String name;
  4. // Getters and setters are omitted
  5. }
  1. public class LackOfSetter {
  2. private int id;
  3. private String name;
  4. public LackOfSetter() {
  5. }
  6. public LackOfSetter(int id, String name) {
  7. this.id = id;
  8. this.name = name;
  9. }
  10. // Getters and setters are omitted
  11. // public void setName(String name) {
  12. //  this.name = name;
  13. // }
  14. }

1. 属性名称、类型都相同:

  1. @Test
  2. public void normalCopyTest() {
  3. OrderEntity entity = new OrderEntity();
  4. entity.setId(1);
  5. entity.setName("orderName");
  6. final BeanCopier copier = BeanCopier.create(OrderEntity.class, OrderDto.class, false);
  7. OrderDto dto = new OrderDto();
  8. copier.copy(entity, dto, null);
  9. Assert.assertEquals(1, dto.getId());
  10. Assert.assertEquals("orderName", dto.getName());
  11. }

结论:拷贝OK。

2. 属性名称相同、类型不同:

  1. @Test
  2. public void sameNameDifferentTypeCopyTest() {
  3. OrderEntity entity = new OrderEntity();
  4. entity.setId(1);
  5. entity.setName("orderName");
  6. final BeanCopier copier = BeanCopier.create(OrderEntity.class, PropWithDiffType.class, false);
  7. PropWithDiffType dto = new PropWithDiffType();
  8. copier.copy(entity, dto, null);
  9. Assert.assertEquals(null, dto.getId()); // OrderEntity的id为int类型,而PropWithDiffType的id为Integer类型,不拷贝
  10. Assert.assertEquals("orderName", dto.getName());
  11. }

结论:名称相同而类型不同的属性不会被拷贝。

注意:即使源类型是原始类型(int, short和char等),目标类型是其包装类型(Integer, Short和Character等),或反之:都不会被拷贝。

3. 源类和目标类有相同的属性(两者的getter都存在),但目标类的setter不存在

  1. @Test
  2. public void targetLackOfSetterCopyTest() {
  3. OrderEntity entity = new OrderEntity();
  4. entity.setId(1);
  5. entity.setName("orderName");
  6. final BeanCopier copier = BeanCopier.create(OrderEntity.class, LackOfSetter.class, false);  // 抛NullPointerException
  7. LackOfSetter dto = new LackOfSetter();
  8. copier.copy(entity, dto, null);
  9. }

结论:创建BeanCopier的时候抛异常。

导致异常的原因是BeanCopier类的第128~133行

  1. for (int i = 0; i < setters.length; i++) { // 遍历目标类的属性描述集
  2. PropertyDescriptor setter = setters[i];
  3. PropertyDescriptor getter = (PropertyDescriptor)names.get(setter.getName()); // 从源类获取和目标类属性名称相同的属性描述
  4. if (getter != null) {
  5. MethodInfo read = ReflectUtils.getMethodInfo(getter.getReadMethod()); // 获取源类属性的getter方法
  6. MethodInfo write = ReflectUtils.getMethodInfo(setter.getWriteMethod()); // 获取目标类属性的setter方法。LackOfSetter类name属性的setter方法没有,所以报错

4. 源类或目标类的setter比getter少

  1. @Test
  2. public void sourceLackOfSetterCopyTest() {
  3. LackOfSetter source = new LackOfSetter(1, "throne");
  4. final BeanCopier copier = BeanCopier.create(LackOfSetter.class, OrderDto.class, false);
  5. OrderDto dto = new OrderDto();
  6. copier.copy(source, dto, null);
  7. Assert.assertEquals(1, dto.getId());
  8. Assert.assertEquals("throne", dto.getName());
  9. }

结论:拷贝OK。此时的setter多余,但不会报错。

总结:

1. BeanCopier只拷贝名称和类型都相同的属性。

2. 当目标类的setter数目比getter少时,创建BeanCopier会失败而导致拷贝不成功。

BeanCopier的更多相关文章

  1. 使用 BeanCopier 复制对象

    Cglib是一款比较底层的操作java字节码的框架. BeanCopier是一个工具类,可以用于Bean对象内容的复制. 复制Bean对象内容的方法有很多,比如自己手动get set ,或者使用Pro ...

  2. 对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀

    功能简介 对象拷贝的应用现状简介: 业务系统中经常需要两个对象进行属性的拷贝,不能否认逐个的对象拷贝是最快速最安全的做法,但是当数据对象的属性字段数量超过程序员的容忍的程度,代码因此变得臃肿不堪,使用 ...

  3. 基于Emit实现的C#版本的BeanCopier

    在java的技术栈当中,著名的Cglib库里面有一个BeanCopier,这个类的功能就是可以完成两个对象的属性复制工作(哪怕属于两个不同的类). 今天本人通过.net内置的System.Reflec ...

  4. 使用CGlib实现Bean拷贝(BeanCopier)

    在做业务的时候,我们有时为了隔离变化,会将DAO查询出来的Entity,和对外提供的DTO隔离开来.大概90%的时候,它们的结构都是类似的,但是我们很不喜欢写很多冗长的b.setF1(a.getF1( ...

  5. BeanCopier使用说明

    BeanCopier从名字可以看出了,是一个快捷的bean类复制工具类. 一 如何使用,我就直接丢代码了 public class BeanCopierTest { static SimpleDate ...

  6. Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)

    转自:http://www.cnblogs.com/kaka/archive/2013/03/06/2945514.html 比较的是四种复制的方式,分别为Apache的BeanUtils和Prope ...

  7. BeanCopier类

    网上学习了一番BeanCopier类. cglib是一款比较底层的操作java字节码的框架. 下面通过拷贝bean对象来测试BeanCopier的特性: public class OrderEntit ...

  8. BeanCopier对象复制学习

    BeanCopier是Cglib包中的一个类,用于对象的复制. 注意:目标对象必须先实例化  而且对象必须要有setter方法 初始化例子:   BeanCopier copier = BeanCop ...

  9. BeanCopier的使用

    BeanCopier进行的是bean之间的copy,从一个类到另一个类,进行属性值的拷贝. 成功copy的条件: 1.属性的类型和名称都相同 2.目标类的setter缺少或缺失会导致拷贝失败,名称相同 ...

随机推荐

  1. CentOS7.4 部署 Django + Python3 + Apache + Mod_wsgi

    安装环境 Remote: CentOS 7.4 x64 (django.example.com) Python: Python3.6.5 Apache: Apache 2.4.6 Mod_wsgi: ...

  2. avascript小技巧

    avascript小技巧 事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture() ...

  3. Convert Application Model Differences

    The eXpressApp Framework is based on the modules concept. As a rule, every module implements a certa ...

  4. 解决shell命令"** is not in the sudoers file..."错误

    Linux中新建的普通用户一般不会分配给root权限,每次都su root也太麻烦,可以通过在/etc/sudoers文件中添加当前用户的方式,给当前用户赋予sudo命令的使用权限. # 切换到roo ...

  5. 二、Unity Editor模式下,操作选中对象

    使用Unity提供的工具类 UnityEditor.Selection public static GameObject activeGameObject public static UnityEng ...

  6. 【坚持】Selenium+Python学习之从读懂代码开始 DAY5

    2018/05/22 函数作为返回值 [来源:廖雪峰的官方网站](https://www.liaoxuefeng.com/) #No.1 def lazy_sum(*args): def sum(): ...

  7. 开源ITIL管理软件iTop 2.5-2.6安装

    环境说明 : 操作系统centos 7.itop版本 iTop-2.5.0-3935.数据库:mariadb iTop 2.5只支持PHP5.6以上版本,本例安装的是php72w版本 1.下载链接: ...

  8. 使用json.dumps转换django queryset的datatime报错问题解决

    转换成json时使用的方法如下: json.dumps(list(models.userlist.objects.values("vu"))) 报错信息如下: Traceback ...

  9. 机器学习算法 --- Naive Bayes classifier

    一.引言 在开始算法介绍之前,让我们先来思考一个问题,假设今天你准备出去登山,但起床后发现今天早晨的天气是多云,那么你今天是否应该选择出去呢? 你有最近这一个月的天气情况数据如下,请做出判断. 这个月 ...

  10. 后端程序员必备的Linux基础知识

    我自己总结的Java学习的系统知识点以及面试问题,目前已经开源,会一直完善下去,欢迎建议和指导欢迎Star: https://github.com/Snailclimb/Java-Guide > ...