package com.tenwa.reckon.util; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.lang.DataElement; import com.tenwa.reckon.bean.ConditionBean; import com.tenwa.reckon.bean.RentPlan; public class ObjectConvertUtils { public static void main(String[] args) { try { Map map = new HashMap(); map.put("rentList", "3"); map.put("rent", "3"); map.put("startDate", "2013-12-06"); RentPlan rp = convertMapToBean(RentPlan.class, map); System.out.println(rp.getRentList()); } catch (Exception e) { e.printStackTrace(); } } /** * 将一个对象数组转化为另一个对象数组 */ public static List convertObjectList(Class type, List objectList) throws Exception { List result = new ArrayList(); for (T rp : objectList) { Map map = ObjectConvertUtils.convertBeanToMap(rp); K temp = ObjectConvertUtils.convertMapToBean(type, map); result.add(temp); } return result; } /** * 对象转换,同名属性对转 * * @param type * @param obj * @return * @throws Exception */ public static K convertObject(Class type, T obj) throws Exception { Map map = ObjectConvertUtils.convertBeanToMap(obj); K temp = ObjectConvertUtils.convertMapToBean(type, map); return temp; } public static T converBizObjectToBean(Class type,BizObject bizObject) throws Exception{ Map maps = new HashMap(); DataElement[] elements= bizObject.getAttributes(); for(DataElement element : elements){ maps.put( element.getName().replaceAll("_", "").toLowerCase(), element.getString()); } return ObjectConvertUtils.convertMapToBean(type, maps); } /** * 将一个Map转化为一个JavaBean对象 */ public static T convertMapToBean(Class type, Map map) throws Exception { Map mapParams = new HashMap(); for (Object key : map.keySet()) { String keyValue = ""; if(key instanceof String){ keyValue = (String)key; }else{ keyValue = key+""; } mapParams.put(keyValue.replace("_", "").toLowerCase(), map.get(keyValue) + ""); } BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(); T obj = type.newInstance(); for (int i = 0; i < properties.length; i++) { PropertyDescriptor descriptor = properties[i]; String propertyName = descriptor.getName(); String value = mapParams.get(propertyName.replace("_", "").toLowerCase()); if (value != null) { Class[] parameterType = descriptor.getWriteMethod().getParameterTypes(); String first = parameterType[0].getSimpleName(); if (value != null && value.length() > 0) { if ("int".equals(first) || "Integer".equals(first)) { descriptor.getWriteMethod().invoke(obj, Integer.valueOf(new BigDecimal(value).intValue())); } else if ("Long".equalsIgnoreCase(first)) { descriptor.getWriteMethod().invoke(obj, Long.valueOf(value)); } else if ("Double".equalsIgnoreCase(first)) { descriptor.getWriteMethod().invoke(obj, Double.valueOf(value)); } else if ("Float".equalsIgnoreCase(first)) { descriptor.getWriteMethod().invoke(obj, Float.valueOf(value)); } else if ("Boolean".equalsIgnoreCase(first)) { descriptor.getWriteMethod().invoke(obj, Boolean.valueOf(value)); } else if ("Short".equalsIgnoreCase(first)) { descriptor.getWriteMethod().invoke(obj, Short.valueOf(value)); } else if ("Byte".equalsIgnoreCase(first)) { descriptor.getWriteMethod().invoke(obj, Byte.valueOf(value)); } else if ("BigDecimal".equals(first)) { descriptor.getWriteMethod().invoke(obj, new BigDecimal(value)); } else if ("Date".equals(first)) { descriptor.getWriteMethod().invoke(obj, DateTools.parseToDate(value)); } else if ("String".equals(first)) { descriptor.getWriteMethod().invoke(obj, value); } } } } return obj; } public static T convertBeanToBean(K bean,T newBean) throws Exception{ Map returnMap = ObjectConvertUtils.convertBeanToMap(bean); return (T)ObjectConvertUtils.convertMapToBean(newBean.getClass(), returnMap); } /** * 将一个 JavaBean 对象转化为一个 Map */ public static Map convertBeanToMap(T bean) throws Exception { Class type = bean.getClass(); Map returnMap = new HashMap(); BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(); for (int i = 0; i < properties.length; i++) { PropertyDescriptor descriptor = properties[i]; String propertyName = descriptor.getName().toLowerCase(); if (!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); if (readMethod != null) { Object result = readMethod.invoke(bean, new Object[0]); if (result != null) { if (result instanceof Date) { String values = DateTools.formatToDate((Date) result); returnMap.put(propertyName, values); } else { returnMap.put(propertyName, result.toString()); } } } } } return returnMap; } /** */ public static Map convertListToMap(List t) throws Exception { Map returnMap = new HashMap(); if(t!=null){ for(BizObject bizObject:t){ DataElement[] elements= bizObject.getAttributes(); for(DataElement element : elements){ returnMap.put( element.getName().replaceAll("_", "").toLowerCase(), element.getString()); } } } return returnMap; } }