实际应用中会遇到很多map和bean互相转换的问题,
bean转map容易,map转bean就稍微麻烦一点
很多时候bean的成员变量不一定都是String类型的,可能会是很多类型。
/**
* 将一个 Map 对象转化为一个 JavaBean
* @param clazz 要转化的类型
* @param map 包含属性值的 map
* @return 转化出来的 JavaBean 对象
* @throws IntrospectionException
* 如果分析类属性失败
* @throws IllegalAccessException
* 如果实例化 JavaBean 失败
* @throws InstantiationException
* 如果实例化 JavaBean 失败
* @throws InvocationTargetException
* 如果调用属性的 setter 方法失败
*/
public static <T>T convertMap(Class<T> 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;
}
/**
* 将一个 JavaBean 对象转化为一个 Map
* @param bean 要转化的JavaBean 对象
* @return 转化出来的 Map 对象
* @throws IntrospectionException 如果分析类属性失败
* @throws IllegalAccessException 如果实例化 JavaBean 失败
* @throws InvocationTargetException 如果调用属性的 setter 方法失败
*/
public static Map convertBean(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}