首页 > 代码库 > java对象与map对象相互转换

java对象与map对象相互转换

  1 /**   2  * 使用org.apache.commons.beanutils进行转换   3  */    4 class A {    5         6     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {      7         if (map == null)    8             return null;    9    10         Object obj = beanClass.newInstance();   11    12         org.apache.commons.beanutils.BeanUtils.populate(obj, map);   13    14         return obj;   15     }     16        17     public static Map<?, ?> objectToMap(Object obj) {   18         if(obj == null)   19             return null;    20    21         return new org.apache.commons.beanutils.BeanMap(obj);   22     }     23        24 }   25    26 /**  27  * 使用Introspector进行转换  28  */   29 class B {   30    31     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {     32         if (map == null)    33             return null;     34    35         Object obj = beanClass.newInstance();   36    37         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());     38         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();     39         for (PropertyDescriptor property : propertyDescriptors) {   40             Method setter = property.getWriteMethod();     41             if (setter != null) {   42                 setter.invoke(obj, map.get(property.getName()));    43             }   44         }   45    46         return obj;   47     }     48        49     public static Map<String, Object> objectToMap(Object obj) throws Exception {     50         if(obj == null)   51             return null;       52    53         Map<String, Object> map = new HashMap<String, Object>();    54    55         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());     56         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();     57         for (PropertyDescriptor property : propertyDescriptors) {     58             String key = property.getName();     59             if (key.compareToIgnoreCase("class") == 0) {    60                 continue;   61             }   62             Method getter = property.getReadMethod();   63             Object value = http://www.mamicode.com/getter!=null ? getter.invoke(obj) : null;   64             map.put(key, value);   65         }     66    67         return map;   68     }     69        70 }   71    72 /**  73  * 使用reflect进行转换  74  */   75 class C {   76    77     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {     78         if (map == null)   79             return null;     80    81         Object obj = beanClass.newInstance();   82    83         Field[] fields = obj.getClass().getDeclaredFields();    84         for (Field field : fields) {     85             int mod = field.getModifiers();     86             if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){     87                 continue;     88             }     89    90             field.setAccessible(true);     91             field.set(obj, map.get(field.getName()));    92         }    93    94         return obj;     95     }     96    97     public static Map<String, Object> objectToMap(Object obj) throws Exception {     98         if(obj == null){     99             return null;    100         }   101   102         Map<String, Object> map = new HashMap<String, Object>();    103   104         Field[] declaredFields = obj.getClass().getDeclaredFields();    105         for (Field field : declaredFields) {    106             field.setAccessible(true);  107             map.put(field.getName(), field.get(obj));  108         }    109   110         return map;  111     }   112 }  

 

java对象与map对象相互转换