package com.vci.ubcs.com.vci.starter.web.util;
|
//
|
// Source code recreated from a .class file by IntelliJ IDEA
|
// (powered by FernFlower decompiler)
|
//
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.vci.ubcs.com.vci.starter.util.VciBaseUtil;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.util.CollectionUtils;
|
|
import java.lang.reflect.Field;
|
import java.util.*;
|
|
public class BeanUtilForVCI {
|
private static Logger logger = LoggerFactory.getLogger(BeanUtilForVCI.class);
|
|
public BeanUtilForVCI() {
|
}
|
|
public static void copyPropertiesIgnoreCase(Object source, Object target) {
|
copyPropertiesIgnoreCase(source, target, false);
|
}
|
|
public static void copyPropertiesIgnoreCase(Object source, Object target, Map<String, String> fieldMap) {
|
copyPropertiesIgnoreCase(source, target, false, fieldMap);
|
}
|
|
public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull) {
|
copyPropertiesIgnoreCase(source, target, ignoreNull, (Map)null);
|
}
|
|
public static void copyDeclaredIgnoreCase(Object source, Object target) {
|
copyDeclaredIgnoreCase(source, target, false);
|
}
|
|
public static void copyDeclaredIgnoreCase(Object source, Object target, boolean ignoreNull) {
|
copyDeclaredIgnoreCase(source, target, ignoreNull, (Map)null);
|
}
|
|
public static void copyDeclaredIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap) {
|
copyDeclaredIgnoreCase(source, target, ignoreNull, fieldMap, (Collection)null);
|
}
|
|
public static void copyDeclaredIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap, Collection<String> ignoreField) {
|
Map<String, Field> sourceMap = BeanUtilForVCI.CacheFieldMap.getDeclaredFieldMap(source.getClass());
|
Map<String, Field> targetMap = BeanUtilForVCI.CacheFieldMap.getDeclaredFieldMap(target.getClass());
|
copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, ignoreField, sourceMap, targetMap);
|
}
|
|
public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap, Collection<String> ignoreField) {
|
Map<String, Field> sourceMap = BeanUtilForVCI.CacheFieldMap.getFieldMap(source.getClass());
|
Map<String, Field> targetMap = BeanUtilForVCI.CacheFieldMap.getFieldMap(target.getClass());
|
copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, ignoreField, sourceMap, targetMap);
|
}
|
|
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 = (String) finalFieldMap.getOrDefault(itFieldName, itFieldName);
|
Field field = (Field)sourceMap.getOrDefault(itFieldName, (Field) null);
|
if (field != null) {
|
it.setAccessible(true);
|
field.setAccessible(true);
|
|
try {
|
String sourceClassName = field.getClass().getName();
|
String targetClassName = it.getClass().getName();
|
if ((sourceClassName.equalsIgnoreCase(Boolean.class.getName()) || sourceClassName.equalsIgnoreCase(Boolean.TYPE.getName())) && targetClassName.equalsIgnoreCase(String.class.getName()) || (targetClassName.equalsIgnoreCase(Boolean.class.getName()) || targetClassName.equalsIgnoreCase(Boolean.TYPE.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 var13) {
|
if (logger.isErrorEnabled()) {
|
logger.error("拷贝属性出错" + var13);
|
}
|
}
|
}
|
}
|
|
});
|
}
|
|
public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap) {
|
copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, (Collection)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();
|
|
private CacheFieldMap() {
|
}
|
|
private static Map<String, Field> getFieldMap(Class clazz) {
|
Map<String, Field> result = (Map)cacheMap.get(clazz.getName());
|
if (result == null) {
|
Class var2 = BeanUtilForVCI.CacheFieldMap.class;
|
synchronized(BeanUtilForVCI.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 = (Map)cacheMap.get(clazz.getName());
|
}
|
}
|
}
|
|
return result;
|
}
|
|
private static Map<String, Field> getDeclaredFieldMap(Class clazz) {
|
Map<String, Field> result = (Map)declaredCacheMap.get(clazz.getName());
|
if (result == null) {
|
Class var2 = BeanUtilForVCI.CacheFieldMap.class;
|
synchronized(BeanUtilForVCI.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 = (Map)declaredCacheMap.get(clazz.getName());
|
}
|
}
|
}
|
|
return result;
|
}
|
}
|
}
|