java的兩個bean之間複製屬性,所有屬性中替換某幾個字元

木头左發表於2024-04-13

以下是實現程式碼:

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

public class BeanCopyUtil {

    /**
     * 複製bean屬性
     * 
     * @param source 源bean
     * @param target 目標bean
     * @param replaceChars 需要替換的字元列表
     */
    public static void copyProperties(Object source, Object target, List<String> replaceChars) {
        BeanUtils.copyProperties(source, target);
        if (replaceChars != null && !replaceChars.isEmpty()) {
            PropertyDescriptor[] sourcePds = BeanUtils.getPropertyDescriptors(source.getClass());
            PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
            for (PropertyDescriptor sourcePd : sourcePds) {
                if (sourcePd.getReadMethod() != null) {
                    for (PropertyDescriptor targetPd : targetPds) {
                        if (targetPd.getWriteMethod() != null && sourcePd.getName().equals(targetPd.getName())) {
                            try {
                                Object sourceValue = sourcePd.getReadMethod().invoke(source);
                                if (sourceValue != null && sourceValue instanceof String) {
                                    String targetValue = (String) sourceValue;
                                    for (String replaceChar : replaceChars) {
                                        targetValue = targetValue.replace(replaceChar, "");
                                    }
                                    targetPd.getWriteMethod().invoke(target, targetValue);
                                } else {
                                    targetPd.getWriteMethod().invoke(target, sourceValue);
                                }
                            } catch (IllegalAccessException | IllegalArgumentException
                                    | InvocationTargetException e) {
                                e.printStackTrace();
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

    /**
     * 複製bean屬性
     * 
     * @param source 源bean
     * @param targetClass 目標bean的class
     * @param replaceChars 需要替換的字元列表
     * @return 目標bean
     */
    public static <T> T copyProperties(Object source, Class<T> targetClass, List<String> replaceChars) {
        T target = null;
        try {
            target = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        copyProperties(source, target, replaceChars);
        return target;
    }

    /**
     * 複製bean屬性
     * 
     * @param sourceList 源bean列表
     * @param targetClass 目標bean的class
     * @param replaceChars 需要替換的字元列表
     * @return 目標bean列表
     */
    public static <T> List<T> copyProperties(List<?> sourceList, Class<T> targetClass, List<String> replaceChars) {
        List<T> targetList = new ArrayList<>();
        for (Object source : sourceList) {
            T target = copyProperties(source, targetClass, replaceChars);
            targetList.add(target);
        }
        return targetList;
    }
}

使用示例:

public class Test {
    public static void main(String[] args) {
        // 複製單個bean屬性
        User source = new User();
        source.setId(1L);
        source.setName("Tom");
        source.setAge(20);
        User target = new User();
        BeanCopyUtil.copyProperties(source, target, Arrays.asList("o"));
        System.out.println(target.getId()); // 1
        System.out.println(target.getName()); // Tm
        System.out.println(target.getAge()); // 20

        // 複製bean列表屬性
        List<User> sourceList = new ArrayList<>();
        sourceList.add(source);
        List<UserVO> targetList = BeanCopyUtil.copyProperties(sourceList, UserVO.class, Arrays.asList("o"));
        System.out.println(targetList.get(0).getId()); // 1
        System.out.println(targetList.get(0).getName()); // Tm
        System.out.println(targetList.get(0).getAge()); // 20
    }
}

class User {
    private Long id;
    private String name;
    private Integer age;
    // 省略getter和setter方法
}

class UserVO {
    private Long id;
    private String name;
    private Integer age;
    // 省略getter和setter方法
}

相關文章