puhanshu
2022-07-23 bbda2ee1af4e86d76f93e00386d77efb56c60d5f
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/CopyUtil.java
@@ -1,19 +1,20 @@
package com.panzhihua.common.utlis;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.reflectasm.MethodAccess;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.reflectasm.MethodAccess;
import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
/**
 * 通用情况拷贝
@@ -38,7 +39,9 @@
    }
    /**
     * <p>对数组进行拷贝 </p>
     * <p>
     * 对数组进行拷贝
     * </p>
     *
     * @author tkq
     * @date 2018年12月19日
@@ -50,7 +53,7 @@
        if (desc.isEmpty()) {
            return new ArrayList<>();
        }
        //创建返回对象
        // 创建返回对象
        List<T> targetList = new ArrayList<>(desc.size());
        for (Object des : desc) {
            if (null != des) {
@@ -67,7 +70,9 @@
    }
    /**
     * <p>对数组进行拷贝 </p>
     * <p>
     * 对数组进行拷贝
     * </p>
     *
     * @author manailin
     * @date 2018年12月19日
@@ -79,7 +84,7 @@
        if (desc.isEmpty()) {
            return new ArrayList<>();
        }
        //创建返回对象
        // 创建返回对象
        List<T> targetList = new ArrayList<>(desc.size());
        for (Object des : desc) {
            if (null != des) {
@@ -118,8 +123,10 @@
    /**
     * 进行复制方法
     *
     * @param dataSource 源目标对象
     * @param target     目标对象
     * @param dataSource
     *            源目标对象
     * @param target
     *            目标对象
     * @author tkq
     * @date 14:37 2019-11-13
     */
@@ -162,7 +169,8 @@
    /**
     * 单例模式
     *
     * @param object 实体对象
     * @param object
     *            实体对象
     * @return MethodAccess
     * @author tkq
     * @date 14:36 2019/4/10
@@ -172,8 +180,8 @@
        synchronized (name) {
            MethodAccess methodAccess = MethodAccess.get(name);
            Class<?> className = object.getClass();
            Set<Field> fields =new HashSet<>();
            for(;className != Object.class ; className = className.getSuperclass()) {
            Set<Field> fields = new HashSet<>();
            for (; className != Object.class; className = className.getSuperclass()) {
                fields.addAll(Arrays.asList(className.getDeclaredFields()));
            }
            List<String> fieldList = new ArrayList<>(fields.size());
@@ -204,48 +212,52 @@
    /**
     * @description 复制属性
     * @param source 源数据
     * @param targetClass 目标对象类型
     * @return
     * @author  weifei
     * @date 2020/8/20 10:38
     * @param source
     *            源数据
     * @param targetClass
     *            目标对象类型
     * @return
     * @author weifei
     * @date 2020/8/20 10:38
     */
    public static <T,K> K copyProperties(T source,Class<K> targetClass){
        if(source == null || targetClass == null){
    public static <T, K> K copyProperties(T source, Class<K> targetClass) {
        if (source == null || targetClass == null) {
            return null;
        }
        //创建对象
        // 创建对象
        try {
            K k = targetClass.newInstance();
            //复制对象
            BeanUtils.copyProperties(source,k);
            // 复制对象
            BeanUtils.copyProperties(source, k);
            return k;
        } catch (Exception e) {
           throw new RuntimeException(e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }
    /**
     * @description 复制属性
     * @param source 源数据
     * @param targetClass 目标对象类型
     * @return
     * @author  weifei
     * @date 2020/12/16 10:12
     * @param source
     *            源数据
     * @param targetClass
     *            目标对象类型
     * @return
     * @author weifei
     * @date 2020/12/16 10:12
     */
    public static <T,K> List<K> copyProperties(List<T> source,Class<K> targetClass){
        if(source == null || targetClass == null){
    public static <T, K> List<K> copyProperties(List<T> source, Class<K> targetClass) {
        if (source == null || targetClass == null) {
            return null;
        }
        List<K> returnValue = new ArrayList<>();
        try {
            source.forEach(s -> {
                K k = copyProperties(s, targetClass);
                if(k != null){
                if (k != null) {
                    returnValue.add(k);
                }
            });
        }catch (Exception e){
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return returnValue;
@@ -253,31 +265,35 @@
    /**
     * @description 深度复制对象
     * @param source 元数据
     * @param targetClass 目标对象类型
     * @return
     * @author  weifei
     * @date 2021/3/1 14:21
     * @param source
     *            元数据
     * @param targetClass
     *            目标对象类型
     * @return
     * @author weifei
     * @date 2021/3/1 14:21
     */
    public static <T,K> K deepCopyObject(T source,Class<K> targetClass){
        if(source == null || targetClass == null){
    public static <T, K> K deepCopyObject(T source, Class<K> targetClass) {
        if (source == null || targetClass == null) {
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONBytes(source),targetClass);
        return JSONObject.parseObject(JSONObject.toJSONBytes(source), targetClass);
    }
    /**
     * @description 深度复制list对象
     * @param source 元数据
     * @param targetClass 目标对象类型
     * @param source
     *            元数据
     * @param targetClass
     *            目标对象类型
     * @return
     * @author  weifei
     * @author weifei
     * @date 2021/3/1 14:26
     */
    public static <T,K> List<K> deepCopyListObject(List<T> source,Class<K> targetClass){
        if(source == null || targetClass == null){
    public static <T, K> List<K> deepCopyListObject(List<T> source, Class<K> targetClass) {
        if (source == null || targetClass == null) {
            return null;
        }
        return JSONObject.parseArray(JSONObject.toJSONString(source),targetClass);
        return JSONObject.parseArray(JSONObject.toJSONString(source), targetClass);
    }
}