使用ReflectASM替代BeanUtils实现对象拷贝

我们将一个类的值要复制到另一个类时,除了繁琐的get/set方法之外,经常会使用到BeanUtils的copyProperties方法,这样会变得非常方便。
注意:避免用 Apache Beanutils 进行属性的 copy,Apache BeanUtils 性能较差
但我在使用的过程中也遇到了一些特殊的需要,比如A类的id属性没有值,而B类的id属性有值,结果使用BeanUtils的copyProperties方法将A拷贝到B之后,B的id值就被覆盖掉了,所以想到了在copyProperties方法基础之上改造一下,由于ReflectASM反射工具的性能很高,所以基于ReflectASM实现了一个对象拷贝工具。

<dependency>
    <groupId>com.esotericsoftware</groupId>
    <artifactId>reflectasm</artifactId>
    <version>1.11.9</version>
</dependency>
import com.esotericsoftware.reflectasm.MethodAccess;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 基于ReflectASM的对象拷贝工具
 * Created by yangxiangjun on 2020/12/18.
 */
public class CopyUtil {
    private static ConcurrentMap<Class, MethodAccess> localCache = new ConcurrentHashMap<>();

    public static MethodAccess get(Class clazz) {
        if (localCache.containsKey(clazz)) {
            return localCache.get(clazz);
        }

        MethodAccess methodAccess = MethodAccess.get(clazz);
        localCache.putIfAbsent(clazz, methodAccess);
        return methodAccess;
    }

    public static void copyProperties(Object source, Object target) {
        copyProperties(source, target, false, (String[]) null);
    }

    public static void copyProperties(Object source, Object target, String... ignoreProperties) {
        copyProperties(source, target, false, ignoreProperties);
    }

    /**
     * 使用ReflectASM实现的拷贝方法
     * @param source
     * @param target
     * @param ignoreNull
     * @param ignoreProperties
     */
    public static void copyProperties(Object source, Object target, boolean ignoreNull, String... ignoreProperties) {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");

        MethodAccess sourceMethodAccess = get(source.getClass());
        MethodAccess targetMethodAccess = get(target.getClass());

        Field[] declaredFields = source.getClass().getDeclaredFields();
        List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        for (Field field : declaredFields) {
            if (targetMethodAccess != null && (ignoreList == null || !ignoreList.contains(field.getName()))) {
                //得到获取属性值得方法
                String getKey;
                if (field.getType() == boolean.class) {
                    getKey = "is" + StringUtils.capitalize(field.getName());
                } else {
                    getKey = "get" + StringUtils.capitalize(field.getName());
                }

                //忽略引用数据类型的空值
                Object value = sourceMethodAccess.invoke(source, getKey, null);
                if (ignoreNull && value == null) {
                    continue;
                }

                //将源目标的值写到目标类中
                String setKey = "set" + StringUtils.capitalize(field.getName());
                int index = targetMethodAccess.getIndex(setKey);
                targetMethodAccess.invoke(target, index, value);
            }
        }
    }

    /**
     * 改造BeanUtils的copyProperties方法
     * @param source
     * @param target
     * @param editable
     * @param ignoreNull
     * @param ignoreProperties
     * @throws BeansException
     */
    public static void copyProperties(Object source, Object target, @Nullable Class<?> editable, boolean ignoreNull, @Nullable String... ignoreProperties) throws BeansException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class<?> actualEditable = target.getClass();
        if (editable != null) {
            if (!editable.isInstance(target)) {
                throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");
            }

            actualEditable = editable;
        }

        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
        List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        PropertyDescriptor[] var7 = targetPds;
        int var8 = targetPds.length;

        for(int var9 = 0; var9 < var8; ++var9) {
            PropertyDescriptor targetPd = var7[var9];
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }

                            Object value = readMethod.invoke(source);

                            if (ignoreNull && value == null){
                                continue;
                            }

                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }

                            writeMethod.invoke(target, value);
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }

    }
}

经过对比,我发现我实现的拷贝方法在数据量非常庞大的情况下,比如执行100W次,性能其实要比BeanUtils的要低很多,这与ReflectASM的高性能这个事实不相符,若有大佬清楚其中缘由,请评论区帮小编释疑

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值