package com.zzg.common.utils;
|
|
|
import org.apache.commons.beanutils.BeanUtils;
|
import org.springframework.beans.BeanWrapper;
|
import org.springframework.beans.BeanWrapperImpl;
|
|
import java.beans.BeanInfo;
|
import java.beans.Introspector;
|
import java.beans.PropertyDescriptor;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.Method;
|
import java.lang.reflect.Modifier;
|
import java.util.HashMap;
|
import java.util.HashSet;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* @author: xxxg
|
* @date: 2022/2/7 17:18
|
*/
|
public class BeanUtil {
|
/**
|
* 使用org.apache.commons.beanutils进行转换
|
* <p>
|
* 注意:org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都有copyProperties方法,但前者支持忽略个别属性的方法,即copyProperties(source, target, ignoreProperties)
|
*/
|
public static Object mapToObject1(Map<String, Object> map, Class<?> beanClass) throws Exception {
|
if (map == null) {
|
return null;
|
}
|
Object obj = beanClass.newInstance();
|
BeanUtils.populate(obj, map);
|
return obj;
|
}
|
|
public static Map<?, ?> objectToMap1(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
if (obj == null) {
|
return null;
|
}
|
return BeanUtils.describe(obj);
|
}
|
|
|
/**
|
* 使用Introspector进行转换
|
*/
|
public static Object mapToObject2(Map<String, Object> map, Class<?> beanClass) throws Exception {
|
if (map == null) {
|
return null;
|
}
|
|
Object obj = beanClass.newInstance();
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
|
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
for (PropertyDescriptor property : propertyDescriptors) {
|
Method setter = property.getWriteMethod();
|
if (setter != null) {
|
setter.invoke(obj, map.get(property.getName()));
|
}
|
}
|
|
return obj;
|
}
|
|
public static Map<String, Object> objectToMap2(Object obj) throws Exception {
|
if (obj == null) {
|
return null;
|
}
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
|
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
for (PropertyDescriptor property : propertyDescriptors) {
|
String key = property.getName();
|
if (key.compareToIgnoreCase("class") == 0) {
|
continue;
|
}
|
Method getter = property.getReadMethod();
|
Object value = getter != null ? getter.invoke(obj) : null;
|
map.put(key, value);
|
}
|
|
return map;
|
}
|
|
/**
|
* 使用reflect进行转换
|
*/
|
public static Object mapToObject3(Map<String, Object> map, Class<?> beanClass) throws Exception {
|
if (map == null) {
|
return null;
|
}
|
|
Object obj = beanClass.newInstance();
|
|
Field[] fields = obj.getClass().getDeclaredFields();
|
for (Field field : fields) {
|
int mod = field.getModifiers();
|
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
|
continue;
|
}
|
|
field.setAccessible(true);
|
field.set(obj, map.get(field.getName()));
|
}
|
|
return obj;
|
}
|
|
public static Map<String, Object> objectToMap3(Object obj) throws Exception {
|
if (obj == null) {
|
return null;
|
}
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
Field[] declaredFields = obj.getClass().getDeclaredFields();
|
for (Field field : declaredFields) {
|
field.setAccessible(true);
|
map.put(field.getName(), field.get(obj));
|
}
|
|
return map;
|
}
|
|
/**
|
* 判断实体中是否包含某个属性
|
*
|
* @param clazz
|
* @param propertyName
|
* @return
|
*/
|
public static boolean containField(Class<?> clazz, String propertyName) {
|
/**
|
* 循环遍历所有的元素,检测有没有这个名字
|
*/
|
Field[] fields = clazz.getDeclaredFields();
|
boolean b = false;
|
for (int i = 0; i < fields.length; i++) {
|
if (fields[i].getName().equals(propertyName)) {
|
b = true;
|
break;
|
}
|
}
|
return b;
|
}
|
|
public static String[] getNullPropertyNames(Object source) {
|
final BeanWrapper src = new BeanWrapperImpl(source);
|
PropertyDescriptor[] pds = src.getPropertyDescriptors();
|
|
Set<String> emptyNames = new HashSet<>();
|
for (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);
|
}
|
|
/**
|
* 非空拷贝
|
*
|
* @param source
|
* @param target
|
*/
|
public static void copyNotNullBean(Object source, Object target) {
|
org.springframework.beans.BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
|
}
|
}
|