java自定义Bean对象复制转换工具类

使用示例:
 

public static void main(String[] args) {
	//将ProjectA的值copy到ProjectB
	ProjectB projectB = BeanCopierUtil.copy(new ProjectA("1", "项目名称"), ProjectB.class);
	List<ProjectA> list = new ArrayList<>();
	//将ProjectA数组copy到产生新的数组ProjectB
	List<ProjectB> list2 = BeanCopierUtil.copyLists(list, ProjectB.class);
}

工具类:


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter;

public class BeanCopierUtil {

    private static final ConcurrentHashMap<String, BeanCopier> BEAN_COPIER_CACHE = new ConcurrentHashMap();

    public BeanCopierUtil() {
    }

    public static void copy(Object source, Object target, Converter converter) {
        String key = genKey(source.getClass(), target.getClass());
        BeanCopier beanCopier;
        if (BEAN_COPIER_CACHE.containsKey(key)) {
            beanCopier = (BeanCopier)BEAN_COPIER_CACHE.get(key);
        } else {
            beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
            BEAN_COPIER_CACHE.put(key, beanCopier);
        }

        beanCopier.copy(source, target, converter);
    }

    public static void copy(Object source, Object target) {
        copy(source, (Object)target, (Converter)null);
    }

    public static <T> T copy(Object source, Class<T> target, Converter converter) {
        if (source != null && target != null) {
            Object obj;
            try {
                obj = target.newInstance();
            } catch (Exception var5) {
                throw new RuntimeException(var5);
            }

            copy(source, obj, converter);
            return (T) obj;
        } else {
            return null;
        }
    }

    public static <T> T copy(Object source, Class<T> target) {
        return (T) copy(source, (Class)target, (Converter)null);
    }

    public static <S, T> T copy(S source, Supplier<T> target, BiConsumer<S, T> callback) {
        if (source != null && target != null) {
            T obj = target.get();
            copy(source, (Object)obj, (Converter)null);
            if (callback != null) {
                callback.accept(source, obj);
            }

            return obj;
        } else {
            return null;
        }
    }

    public static <T, S> List<T> copyLists(List<S> sourceList, Class<T> targetClass) {
        return copyLists(sourceList, (Class)targetClass, (Converter)null);
    }

    public static <T, S> List<T> copyLists(List<S> sourceList, Class<T> targetClass, Converter converter) {
        if (CollectionUtil.isEmpty(sourceList)) {
            return ListUtil.empty();
        } else {
            List<T> target = new ArrayList(sourceList.size());
            Iterator var4 = sourceList.iterator();

            while(var4.hasNext()) {
                S source = (S) var4.next();
                T newInstance = copy(source, targetClass, converter);
                target.add(newInstance);
            }

            return target;
        }
    }

    public static <S, T> List<T> copyLists(List<S> sources, Supplier<T> targetSupplier, BiConsumer<S, T> callback) {
        if (CollectionUtil.isEmpty(sources)) {
            return ListUtil.empty();
        } else {
            List<T> target = new ArrayList(sources.size());
            Iterator var4 = sources.iterator();

            while(var4.hasNext()) {
                S source = (S) var4.next();
                T newInstance = copy(source, targetSupplier, callback);
                target.add(newInstance);
            }

            return target;
        }
    }

    private static String genKey(Class<?> srcClazz, Class<?> tgtClazz) {
        return srcClazz.getName() + tgtClazz.getName();
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值