BeanUtils.copyProperties的批量賦值帶有前後綴的對象

根據BeanUtils.copyProperties實現帶有前綴字符串或後綴字符串的兩個對象的屬性字段賦值

package org.jeecg.modules.hrb.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;

/**
 * EIP的類操作工具
 */
@Component
@Slf4j
public class EipBeanUtils {
    /**
     *
     * @param source 原始對象
     * @param target 目標對象
     * @param prefix 前綴字符串
     * @param sourcePrefix true表示是source帶有前綴,false表示是target帶有前綴
     * @throws BeansException
     */
    public static void copyPropertiesPrefix(Object source, Object target, String prefix, Boolean sourcePrefix) throws BeansException {
        copyPropertiesPrefix(source, target, prefix, sourcePrefix, (String[])null);
    }
    /**
     *
     * @param source 原始對象
     * @param target 目標對象
     * @param prefix 前綴字符串
     * @param sourcePrefix true表示是source帶有前綴,false表示是target帶有前綴
     * @param ignoreProperties 忽視賦值的目標對象的字段屬性
     * @throws BeansException
     */
    public static void copyPropertiesPrefix(Object source, Object target, String prefix, Boolean sourcePrefix, String... ignoreProperties) throws BeansException {
        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
        List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        PropertyDescriptor[] propertyDescriptors = targetPds;
        for(int i = 0; i < targetPds.length; ++i){
            PropertyDescriptor targetPd = propertyDescriptors[i];
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                String targetPdName = targetPd.getName();
                if(null == sourcePrefix || !sourcePrefix){
                    //目標對象的前綴,拼接原始對象的字段屬性名稱
                    if(targetPdName.startsWith(prefix) && (targetPdName.length() > prefix.length())){
                        targetPdName = targetPdName.substring(prefix.length(), prefix.length() + 1).toLowerCase() + targetPdName.substring(prefix.length() + 1);
                    }
                }
                PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPdName);
                if(null == sourcePd && sourcePrefix){
                    //原始對象的前綴,拼接目標對象的字段屬性名稱
                    targetPdName = prefix + targetPdName.substring(0,1).toUpperCase() + targetPdName.substring(1);
                    sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPdName);
                }
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }
                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        } catch (Throwable e) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' to source", e);
                        }
                    }
                }
            }
        }
    }
    /**
     *
     * @param source 原始對象
     * @param target 目標對象
     * @param suffix 後綴字符串
     * @param sourceSuffix true表示是source帶有後綴,false表示是target帶有後綴
     * @throws BeansException
     */
    public static void copyPropertiesSuffix(Object source, Object target, String suffix, Boolean sourceSuffix) throws BeansException {
        copyPropertiesSuffix(source, target, suffix, sourceSuffix, (String[])null);
    }
    /**
     *
     * @param source 原始對象
     * @param target 目標對象
     * @param suffix 後綴字符串
     * @param sourceSuffix true表示是source帶有後綴,false表示是target帶有後綴
     * @param ignoreProperties 忽視賦值的目標對象的字段屬性
     * @throws BeansException
     */
    public static void copyPropertiesSuffix(Object source, Object target, String suffix, Boolean sourceSuffix, String... ignoreProperties) throws BeansException {
        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
        List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        PropertyDescriptor[] propertyDescriptors = targetPds;
        for(int i = 0; i < targetPds.length; ++i){
            PropertyDescriptor targetPd = propertyDescriptors[i];
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                String targetPdName = targetPd.getName();
                if(null == sourceSuffix || !sourceSuffix){
                    //目標對象的後綴,拼接原始對象的字段屬性名稱
                    if(targetPdName.endsWith(suffix) && (targetPdName.length() > suffix.length())){
                        targetPdName = targetPdName + suffix;
                    }
                }
                PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPdName);
                if(null == sourcePd && sourceSuffix){
                    //原始對象的後綴,拼接目標對象的字段屬性名稱
                    targetPdName = targetPdName + suffix;
                    sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPdName);
                }
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }
                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        } catch (Throwable e) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' to source", e);
                        }
                    }
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章