已经知道对象的类型,以及属性对应的值,且值保存在map对象中,将该map转换为已知类型的对象。
/** * 将Map对象通过反射机制转换成Bean对象 * * @param map存放数据的map对象 * @param clazz待转换的class * @return 转换后的Bean对象 * @throws Exception异常 */ public static Object mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception { Object object = clazz.newInstance(); if (map != null && 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(clazz, propertyName); if (field == null) continue; Class<?> fieldTypeClass = field.getType(); value = convertValType(value, fieldTypeClass); try { clazz.getMethod(setMethodName, field.getType()).invoke(object, value); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } return object; } /** * 获取指定字段名称查找在class中的对应的Field对象(包括查找父类) * * @param clazz指定的class * @param fieldName字段名称 * @return Field对象 */ 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; } /** * 将Object类型的值,转换成bean对象属性里对应的类型值 * * @param value,Object对象值 * @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; }
public class Student { private String name; private Integer age; private String hobbies; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getHobbies() { return hobbies; } public void setHobbies(String hobbies) { this.hobbies = hobbies; }
Test Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "scofield"); map.put("age", 20); map.put("hobbies", "music、riding"); try { Student student = (Student) Util.mapToBean(map, Student.class); System.out.println(student.getName()); System.out.println(student.getAge()); System.out.println(student.getHobbies()); } catch (Exception e) { e.printStackTrace(); }
利用反射,将Map转化为Object
猜你喜欢
转载自blog.csdn.net/weixin_42171657/article/details/82767520
今日推荐
周排行