apzl_leasing/calc/com/tenwa/reckon/util/ObjectConvertUtils.java
2018-06-03 22:26:41 +08:00

170 lines
5.8 KiB
Java

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<String, Object> map = new HashMap<String, Object>();
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 <T, K> List<K> convertObjectList(Class<K> type, List<T> objectList) throws Exception {
List<K> result = new ArrayList<K>();
for (T rp : objectList) {
Map<String, Object> map = ObjectConvertUtils.convertBeanToMap(rp);
K temp = ObjectConvertUtils.convertMapToBean(type, map);
result.add(temp);
}
return result;
}
/**
* 对象转换,同名属性对转
*
* @param type
* @param obj
* @return
* @throws Exception
*/
public static <T, K> K convertObject(Class<K> type, T obj) throws Exception {
Map<String, Object> map = ObjectConvertUtils.convertBeanToMap(obj);
K temp = ObjectConvertUtils.convertMapToBean(type, map);
return temp;
}
public static <T> T converBizObjectToBean(Class<T> type,BizObject bizObject) throws Exception{
Map<String, Object> maps = new HashMap<String, Object>();
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> T convertMapToBean(Class<T> type, Map<String, Object> map) throws Exception {
Map<String, String> mapParams = new HashMap<String, String>();
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,K> T convertBeanToBean(K bean,T newBean) throws Exception{
Map<String, Object> returnMap = ObjectConvertUtils.convertBeanToMap(bean);
return (T)ObjectConvertUtils.convertMapToBean(newBean.getClass(), returnMap);
}
/**
* 将一个 JavaBean 对象转化为一个 Map
*/
public static <T> Map<String, Object> convertBeanToMap(T bean) throws Exception {
Class<?> type = bean.getClass();
Map<String, Object> returnMap = new HashMap<String, Object>();
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 <T> Map<String, Object> convertListToMap(List<BizObject> t) throws Exception {
Map<String, Object> returnMap = new HashMap<String, Object>();
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;
}
}