EntityUtils.java
7.82 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
package com.dituhui.pea.util;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 对象转换工具类
*/
public class EntityUtils {
/**
* 数组集合转化为指定对象集合
* 指定的实体对象必须包含所以字段的构造方法,数组的元素的顺序将和构造方法顺序和类型一一对应
*
* @param list 集合
* @param clazz c
* @param <T> 类型
* @return List<T>
* @description 用于jpa查询自定义vo用的
*/
public static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz) {
List<T> returnList = new ArrayList<>();
if (list.size() == 0) {
return returnList;
}
Class[] c2 = null;
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
Class[] tClass = constructor.getParameterTypes();
if (tClass.length == list.get(0).length) {
c2 = tClass;
break;
}
}
//构造方法实例化对象
for (Object[] o : list) {
Constructor<T> constructor = null;
try {
constructor = clazz.getConstructor(c2);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
assert constructor != null;
returnList.add(constructor.newInstance(o));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return returnList;
}
/**
* @param object 要强转的对象 , entityClass 强转后的类型
* @return T
* @descprition 对象类型强转
* @version 1.0
*/
public static <T> T convertBean(Object object, Class<T> entityClass) {
if (null == object) {
return null;
}
return JSON.parseObject(JSON.toJSONString(object), entityClass);
}
/**
* @param object 要转话的对象
* @return java.util.Map<? , ?>
* @descprition 对象转为map
* @version 1.0
*/
public static Map<?, ?> objectToMap(Object object) {
return convertBean(object, Map.class);
}
/**
* @param source 资源对象, target 目标对象, ignoreProperties 赋值new String[]{}
* @return T target对象
* @descprition 对象转换
* @version 1.0
*/
public static <T> T copy(Object source, Class<T> target, String... ignoreProperties) {
T targetInstance = null;
try {
targetInstance = target.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
if (ArrayUtils.isEmpty(ignoreProperties)) {
assert targetInstance != null;
BeanUtils.copyProperties(source, targetInstance);
} else {
assert targetInstance != null;
BeanUtils.copyProperties(source, targetInstance, ignoreProperties);
}
return targetInstance;
}
/**
* @param list, target, ignoreProperties]
* @return java.util.List<T>
* @descprition 对象list转换
* @version 1.0
*/
public static <T, E> List<T> copyList(List<E> list, Class<T> target, String... ignoreProperties) {
List<T> targetList = new ArrayList<>();
if (CollectionUtils.isEmpty(list)) {
return targetList;
}
for (E e : list) {
targetList.add(copy(e, target, ignoreProperties));
}
return targetList;
}
}