对象拷贝类cglib的使用实例

目前流行的较为公用认可的工具类:

Apache的两个版本:(反射机制)

org.apache.commons.beanutils.PropertyUtils.copyProperties(Object dest, Object orig) org.apache.commons.beanutils.BeanUtils.copyProperties(Object dest, Object orig)

spring版本:(反射机制)

org.springframework.beans.BeanUtils.copyProperties(Object source, Object target, Class editable, String[] ignoreProperties)

cglib版本:(使用动态代理,效率高)

net.sf.cglib.beans.BeanCopier.copy(Object paramObject1, Object paramObject2, Converter paramConverter)

在网上找了一个cglib的的对象copy,现记录以下,以备查找:

package cn.qazit.common.utils;

import java.util.HashMap; import java.util.Map;

import net.sf.cglib.beans.BeanCopier;

/** *

  • @ClassName:CglibBeanCopierUtils.java

  • @Title:CglibBeanCopierUtils.java

  • @Description:

  • @CopyRight:CopyRight(c)2016

  • @Company:www.qazit.cn

  • @author ly

  • @date 2016年10月13日 下午5:55:01

  • @version:v1.0 */ public class CglibBeanCopierUtils {

    public static Map<String, BeanCopier> beanCopierMap = new HashMap<String, BeanCopier>();

    public static void copyProperties(Object source,Object target){
    String beanKey = generateKey(source.getClass(),target.getClass());
    BeanCopier copier = null;
    if (!beanCopierMap.containsKey(beanKey)) {
    copier = BeanCopier.create(source.getClass(), target.getClass(), false);
    beanCopierMap.put(beanKey, copier);
    }else {
    copier = beanCopierMap.get(beanKey);
    }
    copier.copy(source, target, null);
    }
    private static String generateKey(Class<?>class1,Class<?>class2){
    return class1.toString() + class2.toString();
    }
    /注: (1)相同属性名,且类型不匹配时候的处理,ok,但是未满足的属性不拷贝; (2)get和set方法不匹配的处理,创建拷贝的时候报错,无法拷贝任何属性(当且仅当sourceClass的get方法超过set方法时出现) (3)BeanCopier
    初始化例子:BeanCopier copier = BeanCopier.create(Source.class, Target.class, useConverter=true) 第三个参数userConverter,是否开启Convert,默认BeanCopier只会做同名,同类型属性的copier,否则就会报错. copier = BeanCopier.create(source.getClass(), target.getClass(), false); copier.copy(source, target, null); (4)修复beanCopier对set方法强限制的约束 改写net.sf.cglib.beans.BeanCopier.Generator.generateClass(ClassVisitor)方法 将133行的 MethodInfo write = ReflectUtils.getMethodInfo(setter.getWriteMethod()); 预先存一个names2放入 109 Map names2 = new HashMap(); 110 for (int i = 0; i < getters.length; ++i) { 111 names2.put(setters[i].getName(), getters[i]); } 调用这行代码前判断查询下,如果没有改writeMethod则忽略掉该字段的操作,这样就可以避免异常的发生。
    /
    }

MAVEN包依赖

 <dependency>  
        <groupId>asm</groupId>  
        <artifactId>asm</artifactId>  
        <version>3.3.1</version>  
    </dependency>  
    <dependency>  
        <groupId>asm</groupId>  
        <artifactId>asm-commons</artifactId>  
        <version>3.3.1</version>  
    </dependency>  
    <dependency>  
        <groupId>asm</groupId>  
        <artifactId>asm-util</artifactId>  
        <version>3.3.1</version>  
    </dependency>  
    <dependency>  
        <groupId>cglib</groupId>  
        <artifactId>cglib-nodep</artifactId>  
        <version>2.2.2</version>  
    </dependency>

转载于:https://my.oschina.net/maojindaoGG/blog/758156

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值