BeanUtils.java
5.52 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jflyfox.util.extend;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import com.jflyfox.util.DateUtils;
import com.jflyfox.util.NumberUtils;
import com.jflyfox.util.StrUtils;
/**
* Bean处理
*
* @author flyfox 2012.08.08
* @email 330627517@qq.com
*/
public class BeanUtils {
private BeanUtils() {
}
/**
* 将Map注入Bean
*
* 2015年4月3日 下午12:02:28 flyfox 330627517@qq.com
*
* @param srcMap
* @param des
* @return
*/
public static Object copy(Map<?, ?> srcMap, Object des) {
try {
if (des == null) {
return null;
}
Method[] methods = des.getClass().getMethods();
Class<?> _class = null;
String vTmp = null;
for (int i = 0; i < methods.length; i++) {
Method aMethod = methods[i];
String name = aMethod.getName();
if (name.indexOf("set") != 0)
continue;
name = StrUtils.toLowerCaseFirst(name.substring(3));
if (!srcMap.containsKey(name)) { // Bean里有这个set方法同时request里有这个参数
// ,那么自动注入
continue;
}
_class = aMethod.getParameterTypes()[0];
try {
vTmp = handlerType(srcMap, des, _class, aMethod, name);
} catch (ClassCastException e) {
throw new RuntimeException("类型转换错误[类名:" + des.getClass().getName() + ",字段名:" + name + "]", e);
} catch (Exception e1) {
throw new Exception("[" + name + "][" + vTmp + "]" + e1.getMessage());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return des;
}
/**
* 调用src对象 传递给 des对象 如果是null,不注入
*
* 2015年4月3日 下午12:06:37 flyfox 330627517@qq.com
*
* @param src
* 源对象
* @param des
* 目标对象
* @param flag
* 如果是true全部注入,如果是false不注入null
*/
public static void copy(Object src, Object des) {
copy(src, des, false);
}
/**
* 调用src对象 传递给 des对象
*
* 2015年4月3日 下午12:06:37 flyfox 330627517@qq.com
*
* @param src
* 源对象
* @param des
* 目标对象
* @param flag
* 如果是true全部注入,如果是false不注入null
*/
public static void copy(Object src, Object des, boolean flag) {
Method[] ms = des.getClass().getMethods();
Method m;
Method g;
String name;
for (int i = 0; i < ms.length; i++) {
m = ms[i];
name = m.getName();
// PO Index不处理
if (name.startsWith("set") && !"setIndex".equals(name)) {
try {
g = src.getClass().getMethod(name.replaceFirst("s", "g"));
if (g == null)
continue;
} catch (Exception e) {
continue;
}
try {
Object obj = g.invoke(src);
if (obj != null || flag) {
m.invoke(des, obj);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private static String handlerType(Map<?, ?> map, Object o, Class<?> _class, Method aMethod, String name)
throws Exception, IllegalAccessException, InvocationTargetException {
String vTmp;
Object obj = map.get(name);
vTmp = obj == null ? "" : obj.toString();
paresType(o, _class, name, vTmp, aMethod);
return vTmp;
}
private static void paresType(Object o, Class<?> _class, String name, String vTmp, Method aMethod)
throws IllegalAccessException, InvocationTargetException {
if ((_class == Float.class || _class == Float.TYPE)) {
aMethod.invoke(o, new Object[] { NumberUtils.parseFloat(vTmp) });
} else if (_class == Long.class || _class == Long.TYPE) {
aMethod.invoke(o, new Object[] { NumberUtils.parseLong(vTmp) });
} else if (_class == Double.class || _class == Double.TYPE) {
aMethod.invoke(o, new Object[] { NumberUtils.parseDbl(vTmp) });
} else if (_class == BigDecimal.class) {
aMethod.invoke(o, new Object[] { NumberUtils.parseBigDecimal(vTmp) });
} else if (_class == Integer.class || _class == Integer.TYPE) {
aMethod.invoke(o, new Object[] { NumberUtils.parseInt(vTmp) });
} else if (_class == String.class) {
aMethod.invoke(o, new Object[] { vTmp });
} else if (_class == Date.class) {
aMethod.invoke(o, new Object[] { DateUtils.parse(vTmp) });
} else if (_class == Timestamp.class) {
aMethod.invoke(o, new Object[] { DateUtils.parseByAll(vTmp) });
} else if (_class == Boolean.class || _class == Boolean.TYPE) {
aMethod.invoke(o, new Object[] { parseBoolean(vTmp) });
} else {
throw new RuntimeException("不支持的参数类型: " + _class + " | " + name + " | " + vTmp);
}
}
/**
* @see 安全处理boolean转换
* @param vTmp
* @return Boolean
* @author zb 330627517@qq.com
* @create Mar 29, 2013 4:43:59 PM
*/
private static Boolean parseBoolean(String vTmp) {
if (StrUtils.isEmpty(vTmp)) {
return false;
}
try {
return Boolean.valueOf(vTmp);
} catch (Exception e) {
return false;
}
}
}