package com.jilongda.common.utils;
|
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanWrapper;
|
import org.springframework.beans.BeanWrapperImpl;
|
|
import java.lang.reflect.Field;
|
import java.util.HashSet;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* @author xiaochen
|
* @ClassName SpringUtils
|
* @Description
|
* @date 2021-06-17 15:37
|
*/
|
public class SpringUtils {
|
|
/**
|
* 对象属性复制
|
*
|
* @param a
|
* @param clazz
|
* @param <A>
|
* @param <B>
|
* @return
|
*/
|
public static <A, B> B beanCopy(A a, Class<B> clazz) {
|
try {
|
B b = clazz.newInstance();
|
BeanUtils.copyProperties(a, b);
|
return b;
|
} catch (InstantiationException | IllegalAccessException e) {
|
throw new RuntimeException("获取对象实例失败");
|
}
|
}
|
|
/**
|
* 对象属性复制
|
*
|
* @param a
|
* @param a
|
* @param <A>
|
* @param <B>
|
* @return
|
*/
|
public static <A, B> B beanCopy(A a, B b) {
|
BeanUtils.copyProperties(a, b);
|
return b;
|
}
|
|
public static <A, B> B beanCopyNotNull(A a, B b) {
|
BeanUtils.copyProperties(a, b, getNullPropertyNames(a));
|
return b;
|
}
|
|
/**
|
* 只拷贝不为null的属性
|
*
|
* @param source
|
* @return java.lang.String[]
|
* @author xiaochen
|
* @date 2022-01-04 18:27
|
*/
|
public static String[] getNullPropertyNames(Object source) {
|
final BeanWrapper src = new BeanWrapperImpl(source);
|
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
|
|
Set<String> emptyNames = new HashSet<String>();
|
for (java.beans.PropertyDescriptor pd : pds) {
|
Object srcValue = src.getPropertyValue(pd.getName());
|
if (srcValue == null) {
|
emptyNames.add(pd.getName());
|
}
|
}
|
String[] result = new String[emptyNames.size()];
|
return emptyNames.toArray(result);
|
}
|
|
/**
|
* 利用反射将map集合封装成bean对象
|
*
|
* @param map
|
* @param clazz
|
* @return
|
*/
|
public static <T> T mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception {
|
Object obj = clazz.newInstance();
|
if (map != null && !map.isEmpty() && map.size() > 0) {
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
// 属性名
|
String propertyName = entry.getKey();
|
// 属性值
|
Object value = entry.getValue();
|
String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
|
//获取和map的key匹配的属性名称
|
Field field = getClassField(clazz, propertyName);
|
if (field == null) {
|
continue;
|
}
|
Class<?> fieldTypeClass = field.getType();
|
value = convertValType(value, fieldTypeClass);
|
try {
|
clazz.getMethod(setMethodName, field.getType()).invoke(obj, value);
|
} catch (NoSuchMethodException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return (T) obj;
|
}
|
|
/**
|
* 根据给定对象类匹配对象中的特定字段
|
*
|
* @param clazz
|
* @param fieldName
|
* @return
|
*/
|
private static Field getClassField(Class<?> clazz, String fieldName) {
|
if (Object.class.getName().equals(clazz.getName())) {
|
return null;
|
}
|
Field[] declaredFields = clazz.getDeclaredFields();
|
for (Field field : declaredFields) {
|
if (field.getName().equals(fieldName)) {
|
return field;
|
}
|
}
|
//如果该类还有父类,将父类对象中的字段也取出
|
Class<?> superClass = clazz.getSuperclass();
|
//递归获取
|
if (superClass != null) {
|
return getClassField(superClass, fieldName);
|
}
|
return null;
|
}
|
|
/**
|
* 将map的value值转为实体类中字段类型匹配的方法
|
*
|
* @param value
|
* @param fieldTypeClass
|
* @return
|
*/
|
private static Object convertValType(Object value, Class<?> fieldTypeClass) {
|
Object retVal;
|
if (Long.class.getName().equals(fieldTypeClass.getName())
|
|| long.class.getName().equals(fieldTypeClass.getName())) {
|
retVal = Long.parseLong(value.toString());
|
} else if (Integer.class.getName().equals(fieldTypeClass.getName())
|
|| int.class.getName().equals(fieldTypeClass.getName())) {
|
retVal = Integer.parseInt(value.toString());
|
} else if (Float.class.getName().equals(fieldTypeClass.getName())
|
|| float.class.getName().equals(fieldTypeClass.getName())) {
|
retVal = Float.parseFloat(value.toString());
|
} else if (Double.class.getName().equals(fieldTypeClass.getName())
|
|| double.class.getName().equals(fieldTypeClass.getName())) {
|
retVal = Double.parseDouble(value.toString());
|
} else {
|
retVal = value;
|
}
|
return retVal;
|
}
|
}
|