map转java实体类
反射实现转换,目前只支持属性值为List和String的
package com.test;
import SystemContants;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Test {
private static Map<String,Object> fieldMaps = new ConcurrentHashMap<String,Object>();
private static Map<String,Object> methodMaps = new ConcurrentHashMap<String,Object>();
//将map转换成对象
public static Object setProperties(Map<String,Object> item, Class clazz) throws IllegalAccessException, InstantiationException {
Object object = clazz.newInstance();
Field field;
for (Map.Entry<String, Object> objectEntry : item.entrySet()) {
try {
String key = objectEntry.getKey();
Object objectEntryValue = objectEntry.getValue();
if (key != null && !SystemContants.NULL_STRING.equals(key) && !SystemContants.RESPCOMMON_STRING.equals(objectEntry.getKey())) {
field = getField(clazz,key);
field.setAccessible(true);
//String属性
if (objectEntryValue instanceof String) {
field.set(object, objectEntry.getValue().toString());
}
//有Map属性 多层嵌套 对象属性中有对象时
if (objectEntryValue instanceof Map) {
try {
String name = field.getType().getName();
if (field.getType().equals(List.class)){
Type genericType = field.getGenericType();
if (null == genericType) {
continue;
}
if (genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericType;
// 得到泛型里的class类型对象
Class<?> actualTypeArgument = (Class<?>) pt.getActualTypeArguments()[0];
Object o = setProperties((Map<String, Object>) objectEntryValue, actualTypeArgument);
List<Object> curEleList = new ArrayList<>();
curEleList.add(o);
field.set(object,curEleList);
}
}else {
Object o = setProperties((Map<String, Object>) objectEntryValue, Class.forName(name));
field.set(object,o);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//有集合属性
if (objectEntryValue instanceof List){
Class<?> curFieldType = field.getType();
// 集合List元素
if (curFieldType.equals(List.class)) {
setListProperties(field,objectEntryValue,object);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return object;
}
private static void setListProperties(Field field,Object objectEntryValue,Object object) throws InstantiationException, IllegalAccessException {
// 当前集合的泛型类型
Type genericType = field.getGenericType();
if (null == genericType) {
return;
}
if (genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericType;
// 得到泛型里的class类型对象
Class<?> actualTypeArgument = (Class<?>)pt.getActualTypeArguments()[0];
List<Object> curEleList = new ArrayList<>();
for (Object listEntity : (List) objectEntryValue) {
Map<String,Object> listMap = (Map<String,Object>)listEntity;
System.out.println(actualTypeArgument.getName());
Object actualType = setProperties(listMap,actualTypeArgument);
if (actualType != null){
curEleList.add(actualType);
}
}
field.set(object, curEleList);
}
}
private static Field getField(Class clazz,String fieldName) {
String name = clazz.getName();
Object o = fieldMaps.get(clazz.getTypeName());
if (null == o) {
//初次使用存入Map
Field[] declaredFields = clazz.getDeclaredFields();
Map<String, Object> map = new ConcurrentHashMap<String, Object>();
for (Field declaredField : declaredFields) {
map.put(declaredField.getName(), declaredField);
}
fieldMaps.put(name, map);
o = map;
}
Map<String, Object> map = (Map<String, Object>)o;
return (Field)map.get(fieldName);
}
private static Method getMethod(Class clazz, String methodName) {
String name = clazz.getName();
Object o = methodMaps.get(clazz.getTypeName());
if (null == o) {
//初次使用存入Map
Method[] declaredMethods = clazz.getDeclaredMethods();
Map<String, Object> map = new ConcurrentHashMap<String, Object>();
for (Method method : declaredMethods) {
map.put(method.getName(), method);
}
methodMaps.put(name, map);
o = map;
}
Map<String, Object> map = (Map<String, Object>)o;
return (Method)map.get(methodName);
}
}