package com.vci.starter.web.util;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.util.CollectionUtils;
|
|
import java.lang.reflect.Field;
|
import java.util.*;
|
|
/**
|
* 业务类型专用的拷贝工具,这个是浅拷贝,
|
* @author weidy
|
* @date 2022-1-18
|
*/
|
public class BeanUtilForVCI {
|
|
/**
|
* 日志
|
*/
|
private static Logger logger = LoggerFactory.getLogger(BeanUtilForVCI.class);
|
|
/**
|
* 拷贝数据
|
* 大小写可以忽略
|
* 下划线 _ 被忽略
|
* null和空字符串的也会被覆盖
|
* @param source 来源属性
|
* @param target 目标属性
|
*/
|
public static void copyPropertiesIgnoreCase(Object source, Object target) {
|
copyPropertiesIgnoreCase(source,target,false);
|
}
|
|
/**
|
* 拷贝属性
|
* @param source 源对象
|
* @param target 目标对象
|
* @param fieldMap 属性的映射
|
*/
|
public static void copyPropertiesIgnoreCase(Object source,Object target,Map<String/**原对象的属性名字**/,String/**目标对象的属性名称**/> fieldMap){
|
copyPropertiesIgnoreCase(source,target,false,fieldMap);
|
}
|
|
/**
|
* 拷贝数据
|
* 大小写可以忽略
|
* 下划线 _ 被忽略
|
* @param source 来源属性
|
* @param target 目标属性
|
* @param ignoreNull 是否忽略控制
|
*/
|
public static void copyPropertiesIgnoreCase(Object source,Object target,boolean ignoreNull){
|
copyPropertiesIgnoreCase(source, target, ignoreNull,null);
|
}
|
|
/**
|
* 拷贝本体的属性内容
|
* @param source 来源对象
|
* @param target 目标对象
|
*/
|
public static void copyDeclaredIgnoreCase(Object source,Object target){
|
copyDeclaredIgnoreCase(source,target,false);
|
}
|
|
/**
|
* 拷贝本体的属性内容
|
* @param source 来源对象
|
* @param target 目标对象
|
* @param ignoreNull 忽略空值的属性
|
*/
|
public static void copyDeclaredIgnoreCase(Object source,Object target,boolean ignoreNull){
|
copyDeclaredIgnoreCase(source,target,ignoreNull,null);
|
}
|
|
/**
|
* 拷贝本体的属性内容
|
* @param source 来源对象
|
* @param target 目标对象
|
* @param ignoreNull 忽略空值的属性
|
* @param fieldMap 字段的映射
|
*/
|
public static void copyDeclaredIgnoreCase(Object source,Object target,boolean ignoreNull,Map<String/**原对象的属性名字**/,String/**目标对象的属性名称**/> fieldMap){
|
copyDeclaredIgnoreCase(source,target,ignoreNull,fieldMap,null);
|
}
|
|
/**
|
* 拷贝本体的属性内容
|
* @param source 来源对象
|
* @param target 目标对象
|
* @param ignoreNull 忽略空值的属性
|
* @param fieldMap 字段的映射
|
* @param ignoreField 忽略字段
|
*/
|
public static void copyDeclaredIgnoreCase(Object source,Object target,boolean ignoreNull,Map<String/**原对象的属性名字**/,String/**目标对象的属性名称**/> fieldMap,Collection<String> ignoreField){
|
Map<String, Field> sourceMap = CacheFieldMap.getDeclaredFieldMap(source.getClass());
|
Map<String, Field> targetMap = CacheFieldMap.getDeclaredFieldMap(target.getClass());
|
copyPropertiesIgnoreCase(source,target,ignoreNull,fieldMap,ignoreField,sourceMap,targetMap);
|
}
|
/**
|
* 拷贝数据
|
* 大小写可以忽略
|
* 下划线 _ 被忽略
|
* @param source 来源属性
|
* @param target 目标属性
|
* @param ignoreNull 是否忽略控制
|
*/
|
public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String/**原对象的属性名字**/,String/**目标对象的属性名称**/> fieldMap, Collection<String> ignoreField) {
|
Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
|
Map<String, Field> targetMap = CacheFieldMap.getFieldMap(target.getClass());
|
copyPropertiesIgnoreCase(source,target,ignoreNull,fieldMap,ignoreField,sourceMap,targetMap);
|
}
|
|
/**
|
* 拷贝数据
|
* 大小写可以忽略
|
* 下划线 _ 被忽略
|
* @param source 来源属性
|
* @param target 目标属性
|
* @param ignoreNull 是否忽略控制
|
*/
|
public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull,
|
Map<String/**原对象的属性名字**/,String/**目标对象的属性名称**/> fieldMap, Collection<String> ignoreField,
|
Map<String, Field> sourceMap,Map<String, Field> targetMap ) {
|
if(fieldMap == null){
|
fieldMap = new HashMap<>();
|
}
|
Map<String, String> finalFieldMap = fieldMap;
|
targetMap.values().forEach((it) -> {
|
boolean not = false;
|
if(ignoreField!=null && ignoreField.contains(it.getName())){
|
not = true;
|
}
|
if(!not) {
|
String itFieldName = it.getName().toLowerCase().replace("_", "");
|
itFieldName = finalFieldMap.getOrDefault(itFieldName, itFieldName);
|
Field field = sourceMap.getOrDefault(itFieldName, null);
|
if (field != null) {
|
it.setAccessible(true);
|
field.setAccessible(true);
|
try {
|
//忽略null和空字符串
|
//处理boolean和String
|
String sourceClassName = field.getClass().getName();
|
String targetClassName = it.getClass().getName();
|
if (((sourceClassName.equalsIgnoreCase(Boolean.class.getName())
|
|| sourceClassName.equalsIgnoreCase(boolean.class.getName())
|
) && targetClassName.equalsIgnoreCase(String.class.getName()))
|
|| ((targetClassName.equalsIgnoreCase(Boolean.class.getName())
|
|| targetClassName.equalsIgnoreCase(boolean.class.getName()))
|
&& sourceClassName.equalsIgnoreCase(String.class.getName()))) {
|
if (targetClassName.equalsIgnoreCase(String.class.getName())) {
|
it.set(target, String.valueOf((Boolean) field.get(source)));
|
} else {
|
it.set(target, Boolean.valueOf((String) field.get(source)));
|
}
|
} else {
|
if (!ignoreNull) {
|
it.set(target, field.get(source));
|
} else {
|
//需要判断是否为空
|
Object sourceValue = field.get(source);
|
if (sourceValue != null && StringUtils.isNotBlank(sourceValue.toString())) {
|
it.set(target, sourceValue);
|
}
|
}
|
}
|
} catch (IllegalAccessException e) {
|
if (logger.isErrorEnabled()) {
|
logger.error("拷贝属性出错" + e);
|
}
|
}
|
}
|
}
|
});
|
}
|
|
|
|
|
/**
|
* 拷贝数据
|
* 大小写可以忽略
|
* 下划线 _ 被忽略
|
* @param source 来源属性
|
* @param target 目标属性
|
* @param ignoreNull 是否忽略控制
|
*/
|
public static void copyPropertiesIgnoreCase(Object source,Object target,boolean ignoreNull,Map<String/**原对象的属性名字**/,String/**目标对象的属性名称**/> fieldMap){
|
copyPropertiesIgnoreCase(source,target,ignoreNull,fieldMap,null);
|
}
|
|
/**
|
* 缓存对象的属性
|
*/
|
private static class CacheFieldMap {
|
/**
|
* 对象属性的缓存
|
*/
|
private static Map<String/**类的名字**/, Map<String/**属性的名称小写**/, Field/**属性的对象**/>> cacheMap = new HashMap<>();
|
|
/**
|
* 对象的自己类的环境,不包括父类
|
*/
|
private static Map<String/**类的名称**/,Map<String/**属性的名称小写**/,Field/**属性的对象**/>> declaredCacheMap = new HashMap<>();
|
|
/**
|
* 获取属性的映射
|
* @param clazz 类
|
* @return 属性名称小写和属性的映射
|
*/
|
private static Map<String/**属性名称**/, Field> getFieldMap(Class clazz) {
|
Map<String, Field> result = cacheMap.get(clazz.getName());
|
if (result == null) {
|
synchronized (CacheFieldMap.class) {
|
if (result == null) {
|
Map<String, Field> fieldMap = new HashMap<>();
|
List<Field> allFields = VciBaseUtil.getAllFieldForObj(clazz);
|
if(!CollectionUtils.isEmpty(allFields)){
|
allFields.stream().forEach(field -> {
|
fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
|
});
|
}
|
cacheMap.put(clazz.getName(), fieldMap);
|
result = cacheMap.get(clazz.getName());
|
}
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 获取属性的映射(不包含父类)
|
* @param clazz 类
|
* @return 属性名称小写和属性的映射
|
*/
|
private static Map<String/**属性名称**/,Field> getDeclaredFieldMap(Class clazz){
|
Map<String, Field> result = declaredCacheMap.get(clazz.getName());
|
if(result == null){
|
synchronized(CacheFieldMap.class){
|
if (result == null) {
|
Map<String, Field> fieldMap = new HashMap<>();
|
Field[] allFields = clazz.getDeclaredFields();
|
if(allFields!=null && allFields.length>0){
|
Arrays.stream(allFields).forEach(field -> {
|
fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
|
});
|
}
|
declaredCacheMap.put(clazz.getName(), fieldMap);
|
result = declaredCacheMap.get(clazz.getName());
|
}
|
}
|
}
|
return result;
|
}
|
}
|
|
}
|