package com.vci.rmip.code.client.codeapply.Apply410.utils; // // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; 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 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 fieldMap) { copyDeclaredIgnoreCase(source, target, ignoreNull, fieldMap, (Collection)null); } public static void copyDeclaredIgnoreCase(Object source, Object target, boolean ignoreNull, Map fieldMap, Collection ignoreField) { Map sourceMap = CacheFieldMap.getDeclaredFieldMap(source.getClass()); Map targetMap = CacheFieldMap.getDeclaredFieldMap(target.getClass()); copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, ignoreField, sourceMap, targetMap); } public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map fieldMap, Collection ignoreField) { Map sourceMap = CacheFieldMap.getFieldMap(source.getClass()); Map targetMap = CacheFieldMap.getFieldMap(target.getClass()); copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, ignoreField, sourceMap, targetMap); } public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map fieldMap, Collection ignoreField, Map sourceMap, Map targetMap) { if (fieldMap == null) { fieldMap = new HashMap(); } Map finalFieldMap = fieldMap; for (Field it : targetMap.values()) { boolean not = false; if (ignoreField != null && ignoreField.contains(it.getName())) { not = true; } if (!not) { String itFieldName = it.getName().toLowerCase().replace("_", ""); itFieldName = finalFieldMap.get(itFieldName)==null?itFieldName: (String)finalFieldMap.get(itFieldName); Field field = sourceMap.get(itFieldName)==null?null:(Field)sourceMap.get(itFieldName); 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 fieldMap) { copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, (Collection)null); } private static class CacheFieldMap { private static Map> cacheMap = new HashMap(); private static Map> declaredCacheMap = new HashMap(); private CacheFieldMap() { } private static Map getFieldMap(Class clazz) { Map result = (Map)cacheMap.get(clazz.getName()); if (result == null) { Class var2 = CacheFieldMap.class; synchronized(CacheFieldMap.class) { if (result == null) { Map fieldMap = new HashMap(); List allFields = VciBaseUtil.getAllFieldForObj(clazz); if (!CollectionUtils.isEmpty(allFields)) { for (Field allField : allFields) { fieldMap.put(allField.getName().toLowerCase().replace("_", ""), allField); } } cacheMap.put(clazz.getName(), fieldMap); result = (Map)cacheMap.get(clazz.getName()); } } } return result; } private static Map getDeclaredFieldMap(Class clazz) { Map result = (Map)declaredCacheMap.get(clazz.getName()); if (result == null) { Class var2 = CacheFieldMap.class; synchronized(CacheFieldMap.class) { if (result == null) { Map fieldMap = new HashMap(); Field[] allFields = clazz.getDeclaredFields(); if (allFields != null && allFields.length > 0) { for (Field allField : allFields) { fieldMap.put(allField.getName().toLowerCase().replace("_", ""), allField); } } declaredCacheMap.put(clazz.getName(), fieldMap); result = (Map)declaredCacheMap.get(clazz.getName()); } } } return result; } } /** * 将一个 Map 对象转化为一个 JavaBean * @param clazz 要转化的类型 * @param map 包含属性值的 map * @return 转化出来的 JavaBean 对象 * @throws IntrospectionException * 如果分析类属性失败 * @throws IllegalAccessException * 如果实例化 JavaBean 失败 * @throws InstantiationException * 如果实例化 JavaBean 失败 * @throws InvocationTargetException * 如果调用属性的 setter 方法失败 */ public static T convertMap(Class clazz, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); // 获取类属性 T obj = clazz.newInstance(); // 给 JavaBean 对象的属性赋值 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i< propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。 Object value = map.get(propertyName); Object[] args = new Object[1]; args[0] = value; Field privateField = getPrivateField(propertyName, clazz); if (privateField == null) { } privateField.setAccessible(true); String type = privateField.getGenericType().toString(); if (type.equals("class java.lang.String")) { privateField.set(obj, value); } else if (type.equals("class java.lang.Boolean")) { privateField.set(obj, Boolean.parseBoolean(String.valueOf(value))); } else if (type.equals("class java.lang.Long")) { privateField.set(obj, Long.parseLong(String.valueOf(value))); } else if (type.equals("class java.lang.Integer")) { privateField.set(obj, Integer.parseInt(String.valueOf(value))); } else if (type.equals("class java.lang.Double")) { privateField.set(obj,Double.parseDouble(String.valueOf(value))); } else if (type.equals("class java.lang.Float")) { privateField.set(obj,Float.parseFloat(String.valueOf(value))); } else if (type.equals("class java.math.BigDecimal")){ privateField.set(obj,new BigDecimal(String.valueOf(value))); }//可继续追加类型 } } return obj; } /*拿到反射父类私有属性*/ private static Field getPrivateField(String name, Class cls) { Field declaredField = null; try { declaredField = cls.getDeclaredField(name); } catch (NoSuchFieldException ex) { if (cls.getSuperclass() == null) { return declaredField; } else { declaredField = getPrivateField(name, cls.getSuperclass()); } } return declaredField; } /** * 获取到对象中属性为null的属性名 * * @param source * @return *//* private static String[] getNullPropertyNames(Object source) { final BeanWrapper src = new BeanWrapperImpl(source); PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set emptyNames = new HashSet<>(); for (PropertyDescriptor pd : pds) { Object srcValue = src.getPropertyValue(pd.getName()); if (ObjectUtils.isEmpty(srcValue)) { emptyNames.add(pd.getName()); } } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); }*/ /** * 拷贝非空对象属性值 * * @param source * @param target *//* public static void copyPropertiesIgnoreNull(Object source, Object target) { BeanUtils.copyProperties(source, target, getNullPropertyNames(source)); }*/ /** * 集合拷贝非空数据 * * @param source 数据源 * @param target 目标 * @param propertyName 要匹配的属性名,例如两个集合使用id进行匹配拷贝 propertyName: "id" */ // public static void copyListPropertiesIgnoreNull(List source, List target, String propertyName) { // if (CollectionUtils.isEmpty(source)) { // throw new NullPointerException("copyListPropertiesIgnoreNull source源数据为空!"); // } // Map map = new HashMap<>(source.size()); // for (Object s : source) { // final BeanWrapper sourceBean = new BeanWrapperImpl(s); // Object value = sourceBean.getPropertyValue(propertyName); // if (value == null) { // throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常"); // } // map.put(value, s); // } // // for (Object s : target) { // final BeanWrapper targetBean = new BeanWrapperImpl(s); // Object value = targetBean.getPropertyValue(propertyName); // if (value == null) { // throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常"); // } // map.put(value, s); // } // } }