java 用反射简单应用,将Object简单转换成map

转自:http://blog.csdn.net/tarrant1/article/details/10954633

[java]  view plain copy
  1. package com.appdev.bsf.server.common;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.lang.reflect.Method;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. public class ObjectDynamicCreator {  
  9.     /** 
  10.      * 返回由对象的属性为key,值为map的value的Map集合 
  11.      *  
  12.      * @param obj 
  13.      *            Object 
  14.      * @return mapValue Map<String,String> 
  15.      * @throws Exception 
  16.      */  
  17.     public static Map<String, String> getFieldVlaue(Object obj) throws Exception {  
  18.         Map<String, String> mapValue = new HashMap<String, String>();  
  19.         Class<?> cls = obj.getClass();  
  20.         Field[] fields = cls.getDeclaredFields();  
  21.         for (Field field : fields) {  
  22.             String name = field.getName();  
  23.             String strGet = "get" + name.substring(01).toUpperCase() + name.substring(1, name.length());  
  24.             Method methodGet = cls.getDeclaredMethod(strGet);  
  25.             Object object = methodGet.invoke(obj);  
  26.             String value = object != null ? object.toString() : "";  
  27.             mapValue.put(name, value);  
  28.         }  
  29.         return mapValue;  
  30.     }  
  31.   
  32.     /** 
  33.      * 返回由Map的key对属性,value对应值组成的对应 
  34.      *  
  35.      * @param map 
  36.      *            Map<String,String> 
  37.      * @param cls 
  38.      *            Class 
  39.      * @return obj Object 
  40.      * @throws Exception 
  41.      */  
  42.     public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception {  
  43.         Field[] fields = cls.getDeclaredFields();  
  44.         Object obj = cls.newInstance();  
  45.         for (Field field : fields) {  
  46.             Class<?> clsType = field.getType();  
  47.             String name = field.getName();  
  48.             String strSet = "set" + name.substring(01).toUpperCase() + name.substring(1, name.length());  
  49.             Method methodSet = cls.getDeclaredMethod(strSet, clsType);  
  50.             if (map.containsKey(name)) {  
  51.                 Object objValue = typeConversion(clsType, map.get(name));  
  52.                 methodSet.invoke(obj, objValue);  
  53.             }  
  54.         }  
  55.         return obj;  
  56.     }  
  57.   
  58.     /** 
  59.      * 将Map里面的部分值通过反射设置到已有对象里去 
  60.      *  
  61.      * @param obj 
  62.      *            Object 
  63.      * @param data 
  64.      *            Map<String,String> 
  65.      * @return obj Object 
  66.      * @throws Exception 
  67.      */  
  68.     public static Object setObjectFileValue(Object obj, Map<String, String> data) throws Exception {  
  69.         Class<?> cls = obj.getClass();  
  70.         Field[] fields = cls.getDeclaredFields();  
  71.         for (Field field : fields) {  
  72.             Class<?> clsType = field.getType();  
  73.             String name = field.getName();  
  74.             String strSet = "set" + name.substring(01).toUpperCase() + name.substring(1, name.length());  
  75.             Method methodSet = cls.getDeclaredMethod(strSet, clsType);  
  76.             if (data.containsKey(name)) {  
  77.                 Object objValue = typeConversion(clsType, data.get(name));  
  78.                 methodSet.invoke(obj, objValue);  
  79.             }  
  80.         }  
  81.         return obj;  
  82.     }  
  83.   
  84.     /** 
  85.      * 把对象的值用Map对应装起来 
  86.      *  
  87.      * @param map 
  88.      *            Map<String,String> 
  89.      * @param obj 
  90.      *            Object 
  91.      * @return 与对象属性对应的Map Map<String,String> 
  92.      */  
  93.     public static Map<String, String> compareMap(Map<String, String> map, Object obj) {  
  94.         Map<String, String> mapValue = new HashMap<String, String>();  
  95.         Field[] fields = obj.getClass().getDeclaredFields();  
  96.         for (Field field : fields) {  
  97.             String name = field.getName();  
  98.             if (map.containsKey(name)) {  
  99.                 mapValue.put(name, map.get(name));  
  100.             }  
  101.         }  
  102.         return mapValue;  
  103.     }  
  104.   
  105.     /** 
  106.      * 把临时对象的值复制到持久化对象上 
  107.      *  
  108.      * @param oldObject 
  109.      *            Object 持久化对象 
  110.      * @param newObject 
  111.      *            Object 临时对象 
  112.      * @return 持久化对象 
  113.      * @throws Exception 
  114.      */  
  115.     public static Object mergedObject(Object oldObject, Object newObject) throws Exception {  
  116.         Class<?> cls = newObject.getClass();  
  117.         Field[] fields = cls.getDeclaredFields();  
  118.         for (Field field : fields) {  
  119.             Class<?> clsType = field.getType();  
  120.             String name = field.getName();  
  121.             String method = name.substring(01).toUpperCase() + name.substring(1, name.length());  
  122.             String strGet = "get" + method;  
  123.             Method methodGet = cls.getDeclaredMethod(strGet);  
  124.             Object object = methodGet.invoke(newObject);  
  125.             if (object != null) {  
  126.                 String strSet = "set" + method;  
  127.                 Method methodSet = cls.getDeclaredMethod(strSet, clsType);  
  128.                 Object objValue = typeConversion(clsType, object.toString());  
  129.                 methodSet.invoke(oldObject, objValue);  
  130.             }  
  131.         }  
  132.         return oldObject;  
  133.     }  
  134.   
  135.     public static Object typeConversion(Class<?> cls, String str) {  
  136.         Object obj = null;  
  137.         String nameType = cls.getSimpleName();  
  138.         if ("Integer".equals(nameType)) {  
  139.             obj = Integer.valueOf(str);  
  140.         }  
  141.         if ("String".equals(nameType)) {  
  142.             obj = str;  
  143.         }  
  144.         if ("Float".equals(nameType)) {  
  145.             obj = Float.valueOf(str);  
  146.         }  
  147.         if ("Double".equals(nameType)) {  
  148.             obj = Double.valueOf(str);  
  149.         }  
  150.   
  151.         if ("Boolean".equals(nameType)) {  
  152.             obj = Boolean.valueOf(str);  
  153.         }  
  154.         if ("Long".equals(nameType)) {  
  155.             obj = Long.valueOf(str);  
  156.         }  
  157.   
  158.         if ("Short".equals(nameType)) {  
  159.             obj = Short.valueOf(str);  
  160.         }  
  161.   
  162.         if ("Character".equals(nameType)) {  
  163.             obj = str.charAt(1);  
  164.         }  
  165.   
  166.         return obj;  
  167.     }  
  168. }  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值