BeanUtils.java
9.67 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.bs.common.utils.bean;
import com.esotericsoftware.reflectasm.MethodAccess;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Bean 工具类
*
* @author system
*/
public class BeanUtils extends org.springframework.beans.BeanUtils
{
/** Bean方法名中属性名开始的下标 */
private static final int BEAN_METHOD_PROP_INDEX = 3;
/** * 匹配getter方法的正则表达式 */
private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
/** * 匹配setter方法的正则表达式 */
private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
/**
* Bean属性复制工具方法。
*
* @param dest 目标对象
* @param src 源对象
*/
public static void copyBeanProp(Object dest, Object src)
{
try
{
copyProperties(src, dest);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 获取对象的setter方法。
*
* @param obj 对象
* @return 对象的setter方法列表
*/
public static List<Method> getSetterMethods(Object obj)
{
// setter方法列表
List<Method> setterMethods = new ArrayList<Method>();
// 获取所有方法
Method[] methods = obj.getClass().getMethods();
// 查找setter方法
for (Method method : methods)
{
Matcher m = SET_PATTERN.matcher(method.getName());
if (m.matches() && (method.getParameterTypes().length == 1))
{
setterMethods.add(method);
}
}
// 返回setter方法列表
return setterMethods;
}
/**
* 获取对象的getter方法。
*
* @param obj 对象
* @return 对象的getter方法列表
*/
public static List<Method> getGetterMethods(Object obj)
{
// getter方法列表
List<Method> getterMethods = new ArrayList<Method>();
// 获取所有方法
Method[] methods = obj.getClass().getMethods();
// 查找getter方法
for (Method method : methods)
{
Matcher m = GET_PATTERN.matcher(method.getName());
if (m.matches() && (method.getParameterTypes().length == 0))
{
getterMethods.add(method);
}
}
// 返回getter方法列表
return getterMethods;
}
/**
* 检查Bean方法名中的属性名是否相等。<br>
* 如getName()和setName()属性名一样,getName()和setAge()属性名不一样。
*
* @param m1 方法名1
* @param m2 方法名2
* @return 属性名一样返回true,否则返回false
*/
public static boolean isMethodPropEquals(String m1, String m2)
{
return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
}
/**
* 通过 ASM反射 速度比 Spring BeanUtils.copyProperties(source,target) 快一倍
* 类型不同可以转换
* 大小写可以忽略
* 下划线 _ 被忽略
*
* @param source 数据源
* @param target 目标是数据
* @param <T>
* @return
*/
public static <T> T copyPropertiesASM( Object source, Object target ) {
MethodAccess sourceMethodAccess = CacheMethodAccess.getMethodAccess(source.getClass());
MethodAccess targetMethodAccess = CacheMethodAccess.getMethodAccess(target.getClass());
Map<String, String> sourceGet = CacheAsmFiledMethod.getMethod("get", source.getClass());
Map<String, String> targetSet = CacheAsmFiledMethod.getMethod("set", target.getClass());
CacheFieldMap.getFieldMap(target.getClass()).keySet().forEach((it) -> {
String sourceIndex = sourceGet.get(it);
if (sourceIndex != null) {
Object value = sourceMethodAccess.invoke(source, sourceIndex);
String setIndex = targetSet.get(it);
targetMethodAccess.invoke(target, setIndex, value);
}
});
return (T) target;
}
/**
* 模仿Spring中 BeanUtils.copyProperties(source,target)
*类 型不同不可以转换
* 但是
* 大小写可以忽略
* 下划线 _ 被忽略
* @param source
* @param target
* @param <T>
* @return
*/
public static <T> T copyPropertiesignoreOther(Object source, Object target) {
Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
CacheFieldMap.getFieldMap(target.getClass()).values().forEach((it) -> {
Field field = sourceMap.get(it.getName().toLowerCase().replace("_", ""));
if (field != null) {
it.setAccessible(true);
field.setAccessible(true);
try {
it.set(target,field.get(source));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
return (T) target;
}
private static class CacheAsmFiledMethod {
private static Map<String, Map<String, String>> cacheGetMethod = new HashMap<>();
private static Map<String, Map<String, String>> cacheSetMethod = new HashMap<>();
private static Map<String, String> getMethod(String type, Class clazz) {
MethodAccess methodAccess = CacheMethodAccess.getMethodAccess(clazz);
Map<String, Field> allFields = CacheFieldMap.getFieldMap(clazz);
Map<String, String> result = null;
if (type.equals("get")) {
result = cacheGetMethod.get(clazz.getName());
} else if (type.equals("set")) {
result = cacheSetMethod.get(clazz.getName());
}
if (result == null) {
synchronized (CacheAsmFiledMethod.class) {
if (result == null) {
Map<String, String> set = new HashMap<>();
Map<String, String> get = new HashMap<>();
allFields.values().forEach((it) -> {
//判断是否是静态
if (!Modifier.isStatic(it.getModifiers())) {
//首字母大写
char[] f = it.getName().toCharArray();
f[0] -= 32;
String fieldName = new String(f);
get.put(fieldName.toLowerCase().replace("_", ""), "get" + fieldName);
set.put(fieldName.toLowerCase().replace("_", ""), "set" + fieldName);
}
});
cacheGetMethod.put(clazz.getName(), get);
cacheSetMethod.put(clazz.getName(), set);
if (type.equals("get")) {
result = cacheGetMethod.get(clazz.getName());
} else if (type.equals("set")) {
result = cacheSetMethod.get(clazz.getName());
}
}
}
}
return result;
}
}
private static class SingleClass {
private static Map<String, Object> cacheObject = new HashMap<>();
private SingleClass() {
}
private static <T> T getObject(Class<T> clazz) {
T result = (T) cacheObject.get(clazz.getName());
if (result == null) {
synchronized (SingleClass.class) {
if (result == null) {
try {
cacheObject.put(clazz.getName(), clazz.newInstance());
result = (T) cacheObject.get(clazz.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return result;
}
}
private static class CacheMethodAccess {
private CacheMethodAccess() {
}
private static Map<String, MethodAccess> cache = new HashMap<>();
private static MethodAccess getMethodAccess(Class clazz) {
MethodAccess result = cache.get(clazz.getName());
if (result == null) {
synchronized (CacheMethodAccess.class) {
if (result == null) {
cache.put(clazz.getName(), MethodAccess.get(clazz));
result = cache.get(clazz.getName());
}
}
}
return result;
}
}
private static class CacheFieldMap {
private static Map<String, Map<String, Field>> cacheMap = new HashMap<>();
private static Map<String, Field> getFieldMap(Class clazz) {
Map<String, Field> result = cacheMap.get(clazz.getName());
if (result == null) {
synchronized (CacheFieldMap.class) {
if (result == null) {
Map<String, Field> fieldMap = new HashMap<>();
for (Field field : clazz.getDeclaredFields()) {
fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
}
cacheMap.put(clazz.getName(), fieldMap);
result = cacheMap.get(clazz.getName());
}
}
}
return result;
}
}
}