如何快速替换BeanUtil

public class BeanUtils {
    private static final Map<String, BeanCopier> BEAN_COPIER_MAP = new HashMap<>();

    /**
     * 复制数组信息
     *
     * @param source      原数组
     * @param targetClass 目标数组类型
     * @param <S>         原数组类型
     * @param <T>         目标数组类型
     * @return 目标数组
     */
    public static <S, T> List<T> cloneList(List<S> source, Class<T> targetClass) {
        if (CollectionUtils.isEmpty(source)) {
            return new ArrayList<>();
        }

        List<T> result = new ArrayList<>();
        for (S item : source) {
            result.add(copy(item, targetClass));
        }

        return result;
    }

    public static <S, T> T copy(S source, Class<T> tClass) {
        if (source == null) {
            return null;
        }
        try {
            String key = source.getClass().getName() + "_" + tClass.getName();
            if (!BEAN_COPIER_MAP.containsKey(key)) {
                BEAN_COPIER_MAP.put(key, BeanCopier.create(source.getClass(), tClass, false));
            }
            T target = tClass.newInstance();
            BEAN_COPIER_MAP.get(key).copy(source, target, null);
            return target;
        } catch (Exception e) {
        }
        return null;
    }

    public static <S, T> T packingBean(S source,  T target) {
        Class sourceBeanClass = source.getClass();
        Class targetBeanClass = target.getClass();

        Field[] sourceFields = sourceBeanClass.getDeclaredFields();
        Field[] targetFields = targetBeanClass.getDeclaredFields();
        for(int i=0; i<sourceFields.length; i++){
            Field sourceField = sourceFields[i];
            Field targetField = targetFields[i];
            if(Modifier.isStatic(sourceField.getModifiers())){
                continue;
            }
            if(Modifier.isStatic(targetField.getModifiers())){
                continue;
            }
            sourceField.setAccessible(true);
            targetField.setAccessible(true);
            try {
                if( !(sourceField.get(source) == null) &&  !"serialVersionUID".equals(sourceField.getName().toString())){
                    targetField.set(target,sourceField.get(source));
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return target;
    }

    public static <S, T> T fastJsonCopy(S source, Class<T> tClass) {
        if (source == null) {
            return null;
        }
        try {
            return JSONObject.parseObject(JSONObject.toJSONString(source), tClass);
        } catch (Exception e) {
        }
        return null;
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值