利用反射实现MapStr、MapObj与bean之间的转换
程序员文章站
2024-03-14 09:26:04
...
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MapUtils {
private static Logger logger = LoggerFactory.getLogger(MapUtils.class);
/**
*
* bean对象转mapObj
* @param obj 待转换bean对象
* @return mapObj
*/
public static Map<String, Object> beanToMapObj(Object obj) {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
logger.error("beanToMapObj Error:{}", e.getMessage(), e);
}
return map;
}
/**
*
* bean对象转mapStr
* @param obj 待转换bean对象
* @return mapStr
*/
public static Map<String, String> beanToMapStr(Object obj) {
if (obj == null) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
String value = (String) getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
logger.error("beanToMapStr Error:{}", e.getMessage(), e);
}
return map;
}
/**
*
* 利用反射将mapObj集合封装成bean对象
* @param map 待转换mapObj
* @param beanType 对象中的object类型
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static <T> T mapObjToBean(Map<String, Object> map, Class<?> beanType) throws Exception {
Object obj = beanType.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);
Field field = getClassField(beanType, propertyName); // 获取和map的key匹配的属性名称
if (field == null) {
continue;
}
Class<?> fieldTypeClass = field.getType();
value = convertValType(value, fieldTypeClass);
try {
beanType.getMethod(setMethodName, field.getType()).invoke(obj, value);
} catch (NoSuchMethodException e) {
logger.error("mapToBean Error:{}", e.getMessage(), e);
}
}
}
return (T) obj;
}
/**
*
* 利用反射将mapStr集合封装成bean对象
* @param map 待转换mapStr
* @param beanType 对象中的object类型
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static <T> T mapStrToBean(Map<String, String> map, Class<?> beanType) throws Exception {
Object obj = beanType.newInstance();
if (map != null && !map.isEmpty() && map.size() > 0) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String propertyName = entry.getKey(); // 属性名
Object value = entry.getValue(); // 属性值
String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
Field field = getClassField(beanType, propertyName); // 获取和map的key匹配的属性名称
if (field == null) {
continue;
}
Class<?> fieldTypeClass = field.getType();
value = convertValType(value, fieldTypeClass);
try {
beanType.getMethod(setMethodName, field.getType()).invoke(obj, value);
} catch (NoSuchMethodException e) {
logger.error("mapStrToBean Error:{}", e.getMessage(), e);
}
}
}
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 = null;
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;
}
}