package com.unicom.ucloud.eom.fastProcess.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
public class Convert {
private static Map<String, Method> methodMap = new HashMap<String, Method>();
private static void getMethods()
{
for (Method method : Convert.class.getMethods()) {
methodMap.put(method.getName(),method);
}
}
public static <T> T MapToObject(HashMap<String,Object> map,Class<T> clazz) throws Exception {
getMethods();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
T obj = clazz.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
if(value!=null&&!"".equals(value))
{
if(map.get(propertyName).getClass() != obj.getClass().getDeclaredField(propertyName).getType())
{
String converMethod = getMethodRuleName("String",obj.getClass().getDeclaredField(propertyName).getType().getSimpleName());
Logger.getLogger(Convert.class).debug(converMethod);
if (hasConvertMethod(converMethod)) {
//调用已注册的类型转换方法
value = getConvertMethod(converMethod).invoke(new Convert(), new Object[]{value});
descriptor.getWriteMethod().invoke(obj, value);
}else {
try {
descriptor.getWriteMethod().invoke(obj, value);
} catch (Exception e) {
Logger.getLogger(Convert.class).error("属性:"+propertyName+" 无法转换,类型:"+map.get(propertyName).getClass().getName()+"需要转换成:" +
obj.getClass().getDeclaredField(propertyName).getType().getName(),e);
}
}
}else
{
descriptor.getWriteMethod().invoke(obj, value);
}
}
}
}
return obj;
}
private static String getMethodRuleName(String sourceTypeName, String targetTypeName) {
return "to"+targetTypeName;
}
private static boolean hasConvertMethod(String methodName) {
return methodMap.containsKey(methodName);
}
public static boolean toboolean(Object obj)
{
String bool = String.valueOf(obj);
if("1".equals(bool) || "true".equals(obj))
{
return true;
}else{
return false;
}
}
private static Method getConvertMethod(String methodName) {
return methodMap.get(methodName);
}
public static Date toDate(Object obj) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(obj!=null&&!"".equals(obj)){
try {
return format.parse(String.valueOf(obj));
} catch (ParseException e) {
Logger.getLogger(Convert.class).error(obj,e);
}
}
return null;
}
public static Long toLong(Object obj)
{
String retLong = String.valueOf(String.valueOf(obj));
return Long.valueOf(retLong);
}
public static Integer toInteger(Object obj)
{
String retInt = String.valueOf(String.valueOf(obj));
return Integer.valueOf(retInt);
}
public static int toint(Object obj)
{
return toInteger(obj);
}
public static String toString(Object obj) {
return obj.toString();
}
public static Timestamp toTimestamp(Object obj) {
return Timestamp.valueOf(String.valueOf(obj));
}
}
转载于:https://www.cnblogs.com/yanfan/archive/2013/03/05/2989461.html