天天看点

Java 深拷贝工具类

工具类代码:

package com.example.demo.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author ssl
 * @date 2023/2/8 10:03
 */

public class CopyUtil {
    //V3
    public static  <T> T copyClassV3(Object source, Class<T> aClass){
        if (source == null){
            return null;
        }
        T t = null;
        try {
            Class<?> sourceClass = source.getClass();
            GetAll getAll = new GetAll<>(sourceClass).invoke();
            Field[] sourceFields = getAll.getFields();
            HashMap<String, Field> fieldHashMap = new HashMap<>();
            for (Field sourceField : sourceFields) {
                fieldHashMap.put(sourceField.getName(), sourceField);
            }
            getAll = new GetAll<T>(aClass).invoke();
            Field[] fields = getAll.getFields();
            Method[] methods = getAll.getMethods();

            t = aClass.newInstance();
            for (Field field : fields) {
                Field sourceField = fieldHashMap.get(field.getName());
                if (sourceField == null) {
                    continue;
                }else {
                    sourceField.setAccessible(true);
                    Object o = sourceField.get(source);
                    if (o == null && StringUtils.isBlank(String.valueOf(o))){
                        continue;
                    }
                }
                field.setAccessible(true);
                Class<?> type = field.getType();
                Object o = null;
                if (type.isPrimitive() || isBaseType(type)) {
                    o = sourceField.get(source);
                }else {
                    o = copyClassV3(sourceField.get(source), type);
                }
                String methodName = "set" + captureName(field.getName());
                Method declaredMethod = getSetMethod(methodName, methods);
                declaredMethod.invoke(t, o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        return t;
    }


    //原始代码升级V2
    public static  <T> T copyClassConcise(Object source, Class<T> aClass){
        if (source == null){
            return null;
        }
        T t = null;
        try {
            Class<?> sourceClass = source.getClass();
            GetAll getAll = new GetAll<>(sourceClass).invoke();
            Field[] sourceFields = getAll.getFields();
            Method[] sourceMethods = getAll.getMethods();
//            Field[] sourceFields = sourceClass.getDeclaredFields();
            HashMap<String, Field> fieldHashMap = new HashMap<>();
            for (Field sourceField : sourceFields) {
                fieldHashMap.put(sourceField.getName(), sourceField);
            }
//            Field[] Fields = aClass.getDeclaredFields();
            getAll = new GetAll<T>(aClass).invoke();
            Field[] fields = getAll.getFields();
            Method[] methods = getAll.getMethods();

            t = aClass.newInstance();
            for (Field field : fields) {
                Field sourceField = fieldHashMap.get(field.getName());
                if (sourceField == null) {
                    continue;
                }else {
                    sourceField.setAccessible(true);
                    Object o = sourceField.get(source);
                    if (o == null && StringUtils.isBlank(String.valueOf(o))){
                        continue;
                    }
                }
                field.setAccessible(true);
                Class<?> type = field.getType();
                Object o = null;
                if (type.isPrimitive() || isBaseType(type)) {
                    o = sourceField.get(source);
                }else {
                    o = copyClassConcise(sourceField.get(source), type);
                }
                String methodName = "set" + captureName(field.getName());
                Method declaredMethod = getSetMethod(methodName, methods);
                declaredMethod.invoke(t, o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        return t;
    }

    //根据方法名获得方法
    public static Method getSetMethod(String setMethodName, Method[] methods){
        for (Method method : methods) {
            String name = method.getName();
            if (setMethodName.equals(name)){
                return method;
            }
        }
        return null;
    }


    //原始代码V1
    public static  <T> T copyClass(Object source, Class<T> aClass){
        if (source == null){
            return null;
        }
        T t = null;
        try {
            Class<?> sourceClass = source.getClass();
            Field[] sourceFields = sourceClass.getDeclaredFields();
            HashMap<String, Field> fieldHashMap = new HashMap<>();
            for (Field sourceField : sourceFields) {
                fieldHashMap.put(sourceField.getName(), sourceField);
            }
            Field[] Fields = aClass.getDeclaredFields();
            t = aClass.newInstance();
            for (Field field : Fields) {
                Field sourceField = fieldHashMap.get(field.getName());
                sourceField.setAccessible(true);
                if (sourceField == null) {
                    continue;
                }else {
                    Object o = sourceField.get(source);
                    if (o == null && StringUtils.isBlank(String.valueOf(o))){
                        continue;
                    }
                }
                field.setAccessible(true);

                //方法二
                /*Type type = field.getGenericType();
                String methodName = "";
                if (field.getClass().isPrimitive() || isBaseType(type)) {
                    String typeName = type.getTypeName();
                    if (typeName.equals("java.lang.Boolean") || typeName.equals("boolean")){
                        methodName = "is" + captureName(field.getName());
                    }else {
                        methodName = "set" + captureName(field.getName());
                    }
                    Method declaredMethod = aClass.getDeclaredMethod(methodName, field.getType());
                    declaredMethod.invoke(t, sourceField.get(source));
                }else {
                    Object o = copyClass(sourceField.get(source), field.getType());
                    methodName = "set" + captureName(field.getName());
                    Method declaredMethod = aClass.getDeclaredMethod(methodName, field.getType());
                    declaredMethod.invoke(t, o);
                }*/

                //方法一
                Class<?> type = field.getType();
                String sourceMethodName = "";
                Object o = null;
                if (type.isPrimitive() || isBaseType(type)) {
                    /*String typeName = type.getName();
                    if (typeName.equals("java.lang.Boolean") || typeName.equals("boolean")){
                        sourceMethodName = "is" + captureName(sourceField.getName());
                    }else {
                        sourceMethodName = "get" + captureName(sourceField.getName());
                    }
                    Method sourceClassDeclaredMethod = sourceClass.getDeclaredMethod(sourceMethodName);
                    o = sourceClassDeclaredMethod.invoke(source);*/
                    o = sourceField.get(source);
                }else {
                    o = copyClass(sourceField.get(source), type);
                }
                String methodName = "set" + captureName(field.getName());
                Method declaredMethod = aClass.getDeclaredMethod(methodName, type);
                declaredMethod.invoke(t, o);
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return t;
    }

    //首字母大写
    public static String captureName(String name) {
        char[] cs=name.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
    }

    //判断是否是基本类型的包装类并且兼容泛式的类型
    public static boolean isBaseType(Class<?> type) {
        String className = type.getName();
        if (className.equals("java.lang.Integer") ||
                className.equals("java.lang.Byte") ||
                className.equals("java.lang.Long") ||
                className.equals("java.lang.Double") ||
                className.equals("java.lang.Float") ||
                className.equals("java.lang.Character") ||
                className.equals("java.lang.Short") ||
                className.equals("java.lang.String") ||
                className.equals("java.lang.Boolean") ||
                className.equals("java.lang.Object")) {
            return true;
        }
        return false;
    }

    private static class GetAll<T> {
        private Class<T> aClass;
        private Field[] fields;
        private Method[] methods;

        public GetAll(Class<T> aClass) {
            this.aClass = aClass;
        }

        public Field[] getFields() {
            return fields;
        }

        public Method[] getMethods() {
            return methods;
        }

        public GetAll invoke() {
            List<Field> fieldList = new ArrayList<>();
            List<Method> methodList = new ArrayList<>();
            Class clazz = aClass;
            while (clazz != Object.class){
                fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
                methodList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredMethods())));
                clazz = clazz.getSuperclass();
            }
            fields = new Field[fieldList.size()];
            methods = new Method[methodList.size()];
            fieldList.toArray(fields);
            methodList.toArray(methods);
            return this;
        }
    }


    /*public static boolean isBaseType(Type type) {
        String className = String.valueOf(type);
        if (className.contains("java.lang.Integer") ||
                className.contains("java.lang.Byte") ||
                className.contains("java.lang.Long") ||
                className.contains("java.lang.Double") ||
                className.contains("java.lang.Float") ||
                className.contains("java.lang.Character") ||
                className.contains("java.lang.Short") ||
                className.contains("java.lang.String") ||
                className.contains("java.lang.Boolean")) {
            return true;
        }
        return false;
    }*/


}
           

测试代码:

package com.example.demo.test;

import com.example.demo.entity.*;
import com.example.demo.util.CopyUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;

/**
 * @author ssl
 * @date 2023/2/8 10:51
 */

public class CopyTest {
    public static void main(String[] args) {
        copyTest4();
    }
    public static void copyTest4(){
        User3 u1 = new User3();
        u1.setId(1L);
        u1.setName("小明");
        u1.setAge(12);
        u1.setB(true);
        u1.setPointXY(new PointXY("12", "13"));
        Notepad<String, String> notepad = new Notepad<>();
        notepad.setKey("key");
        notepad.setValue("value");
        u1.setNotepad(notepad);
        User3 user = CopyUtil.copyClassV3(u1, User3.class);
        System.out.println(u1);
        System.out.println(user);
        System.out.println("=====");
        user.getNotepad().setKey("test");
        user.getPointXY().setX("11");
        user.setAge(11);
        user.setName("小红");
        System.out.println(u1);
        System.out.println(user);

        //其他拷贝类工具
        User3 u2 = new User3();
        BeanUtils.copyProperties(u1, u2);
        System.out.println("-----");
        System.out.println(u2);
        u2.getNotepad().setKey("test");
        u2.getPointXY().setX("11");
        u2.setAge(11);
        u2.setName("小红");
        System.out.println(u1);
        System.out.println(u2);
        
        //其他拷贝类工具
        System.out.println("**********");
        System.out.println(u1);
        ObjectMapper objectMapper = new ObjectMapper();
        User3 u3 = null;
        try {
            u3 = objectMapper.readValue(objectMapper.writeValueAsString(u1), User3.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(u3);
        u3.getNotepad().setKey("test2");
        u3.getPointXY().setX("111");
        u3.setAge(112);
        u3.setName("小天");
        System.out.println(u1);
        System.out.println(u3);
    }


    public static void copyTest3(){
        Notepad<Object, Object> n1 = new Notepad<>();
        n1.setKey("key");
        n1.setValue("value");
        Notepad n2 = CopyUtil.copyClassConcise(n1, Notepad.class);
        System.out.println(n1);
        System.out.println(n2);
        System.out.println("------");
        n2.setKey("test");
        n2.setValue("testvalue");
        System.out.println(n1);
        System.out.println(n2);
    }

    public static void copyTest2(){
        User u1 = new User();
        u1.setId(1L);
        u1.setName("小明");
        u1.setAge(12);
        u1.setB(true);
        u1.setPointXY(new PointXY("12", "13"));
        Notepad<String, String> notepad = new Notepad<>();
        notepad.setKey("key");
        notepad.setValue("value");
        u1.setNotepad(notepad);
        User2 user = CopyUtil.copyClassConcise(u1, User2.class);
        System.out.println(u1);
        System.out.println(user);
        System.out.println("====");
        user.getNotepad().setKey("test");
        user.getPointXY().setX("11");
        user.setAge(11);
        user.setName("小红");
        System.out.println(u1);
        System.out.println(user);

        //其他拷贝类工具
        User u2 = new User();
        BeanUtils.copyProperties(u1, u2);
        System.out.println("-----");
        System.out.println(u2);
        u2.getNotepad().setKey("test");
        u2.getPointXY().setX("11");
        u2.setAge(11);
        u2.setName("小红");
        System.out.println(u1);
        System.out.println(u2);

        //其他拷贝类工具
        System.out.println("**********");
        System.out.println(u1);
        ObjectMapper objectMapper = new ObjectMapper();
        User2 u3 = null;
        try {
            u3 = objectMapper.readValue(objectMapper.writeValueAsString(u1), User2.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(u3);
        u3.getNotepad().setKey("test2");
        u3.getPointXY().setX("111");
        u3.setAge(112);
        u3.setName("小天");
        System.out.println(u1);
        System.out.println(u3);
    }

    public static void copyTest1(){
        User u1 = new User();
        u1.setId(1L);
        u1.setName("小明");
        u1.setAge(12);
        u1.setB(true);
        u1.setPointXY(new PointXY("12", "13"));
        Notepad<String, String> notepad = new Notepad<>();
        notepad.setKey("key");
        notepad.setValue("value");
        u1.setNotepad(notepad);
        User user = CopyUtil.copyClassConcise(u1, User.class);
        System.out.println(u1);
        System.out.println(user);
        System.out.println("====");
        user.getNotepad().setKey("test");
        user.getPointXY().setX("11");
        user.setAge(11);
        user.setName("小红");
        System.out.println(u1);
        System.out.println(user);

        //其他拷贝类工具
        User u2 = new User();
        BeanUtils.copyProperties(u1, u2);
        System.out.println("-----");
        System.out.println(u2);
        u2.getNotepad().setKey("test");
        u2.getPointXY().setX("11");
        u2.setAge(11);
        u2.setName("小红");
        System.out.println(u1);
        System.out.println(u2);

        //其他拷贝类工具
        System.out.println("**********");
        System.out.println(u1);
        ObjectMapper objectMapper = new ObjectMapper();
        User u3 = null;
        try {
            u3 = objectMapper.readValue(objectMapper.writeValueAsString(u1), User.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(u3);
        u3.getNotepad().setKey("test2");
        u3.getPointXY().setX("111");
        u3.setAge(112);
        u3.setName("小天");
        System.out.println(u1);
        System.out.println(u3);
    }
}
           

转载表明出处