package com.doc;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class DataMapUtil {
public static Map<String, Object> dataMap = new HashMap<String, Object>();
/**
* 将对象转换成Map
* @param obj 对象类
* @return
*/
public static Map<String,Object> setObjToMap(Object obj){
Class c;
try {
c = Class.forName(obj.getClass().getName());
Method[] methods = c.getMethods();
for(int i=0,l=methods.length;i<l;i++){
String method = methods[i].getName();
System.out.println("The method is:" + method);
if(method.startsWith("get")){
Object value = methods[i].invoke(obj);
if(value != null){
if(value.getClass().getClassLoader() != null){ //处理自定义的对象类型
setObjToMap(value);
}
String key = method.substring(3);
key = key.substring(0, 1).toLowerCase() + key.substring(1);
if("java.util.Date".equals(value.getClass().getName())){
value=dateToStr((Date)value);
}
dataMap.put(key, value);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return dataMap;
}
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/**pojo转为map
* 根据下载类型获取需要传递的Map参数
* @param oid 对象Id
*/
public static Map<String, Object> getDataMap(){
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap = DataMapUtil.setObjToMap(new Company("深圳市腾讯计算机系统有限公司", "tencent", "深圳","创新 发展 和谐 拼搏",new Date(),"500","80000000","马化腾"));
return dataMap;
}
}
评论1
最新资源