1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
| package com.oo.gzdec.config;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Blob;
import java.sql.Clob;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.ConvertUtils;
public class BeanUtils {
/**
* 将一个 TO对象 转化为一个Map
*
* @param <T>
* @param TO
* @return
*/
public static <T> Map<String, Object> setTo2Map(T TO) {
Class<? extends Object> toType = TO.getClass();
Map<String, Object> returnMap = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(toType);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propName = descriptor.getName();
if (!propName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(TO, new Object[0]);
returnMap.put(propName, result != null ? result : "");
}
}
} catch (IllegalArgumentException e) {
throw new RuntimeException("不合法或不正确的参数", e);
} catch (IntrospectionException e) {
throw new RuntimeException("分析类属性失败", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("实例化JavaBean失败", e);
} catch (InvocationTargetException e) {
throw new RuntimeException("调用目标失败", e);
}
return returnMap;
}
/**
* 将一个 Map对象 转换为对应的 TO
*
* @param <T>
* @param valueMap -
* 包含属性值的map
* @param clazz -
* 要转化的类型
* @return 转化出来的结果TO
*/
public static <T> T setMap2To(Map<String, Object> paraMap, Class<T> clazz) {
T TO = null;
try {
TO = clazz.newInstance();
String propName = null;
Object propValue = null;
for (Map.Entry<String, Object> entity : paraMap.entrySet()) {
propName = entity.getKey();
propValue = entity.getValue();
setProperties(TO, propName, propValue);
}
} catch (IllegalArgumentException e) {
throw new RuntimeException("不合法或不正确的参数", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("实例化JavaBean失败", e);
} catch (Exception e) {
throw new RuntimeException("Map转换为Java Bean对象失败", e);
}
return TO;
}
/**
* 根据 TO 和 属性名/值对,设置TO值
*
* @param <T>
* @param entity
* @param propName
* @param propValue
*/
public static <T> void setProperties(T entity, String propName,Object propValue) throws IntrospectionException,
IllegalArgumentException, IllegalAccessException,InvocationTargetException, ParseException {
BeanInfo beanInfo = Introspector.getBeanInfo(entity.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
Object[] args = new Object[1];
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
String properyName = propertyDescriptor.getName();
Class propertyTypeClass = propertyDescriptor.getPropertyType();
String propertyTypeClassName = propertyTypeClass.getName();
if (propName.equals(properyName)) {
if (!propValue.getClass().getName().equals(propertyTypeClassName)) {
if (propertyTypeClassName.equals("java.sql.Date")) {
args[0] = SimpleDateConverter.convert2SqlDate(propValue);
} else if(propertyTypeClassName.equals("java.sql.Timestamp")){
args[0] = SimpleDateConverter.convert2Timestamp(propValue);
} else if (propertyTypeClassName.equals("java.util.Date")) {
args[0] = SimpleDateConverter.convert2UtilDate(propValue);
} else {
args[0] = ConvertUtils.convert(propValue.toString(),propertyTypeClass);
}
} else {
args[0] = propValue;
}
propertyDescriptor.getWriteMethod().invoke(entity, args[0]);
break;
}
}
}
}
|