ObjectUtils.java
5.17 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
package com.bs.common.utils;
import com.bs.common.utils.sign.Md5Utils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.time.temporal.TemporalAccessor;
import java.util.*;
public class ObjectUtils {
/**
* Map转实体类共通方法
*
* @param type 实体类class
* @param map map
* @return Object
* @throws Exception
*/
public static Object convertMap(Class type, Map map) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
Object obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
descriptor.getWriteMethod().invoke(obj, value);
}
}
return obj;
}
/**
* 实体类转Map共通方法,不报错值为空的属性
*
* @param bean 实体类
* @return Map
* @throws Exception
*/
public static Map<String,String> convertBean(Object bean) throws Exception {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean);
if (result != null) {
returnMap.put(propertyName, String.valueOf(result));
}
// else {
// returnMap.put(propertyName, "");
// }
}
}
return returnMap;
}
public String createSign(Map<String, String> params, String privateKey){
StringBuilder sb = new StringBuilder();
// 将参数以参数名的字典升序排序
Map<String, String> sortParams = new TreeMap<String, String>(params);
// 遍历排序的字典,并拼接"key=value"格式
for (Map.Entry<String, String> entry : sortParams.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().trim();
if (StringUtils.isNotEmpty(value))
sb.append("&").append(key).append("=").append(value);
}
String stringA = sb.toString().replaceFirst("&","");
String stringSignTemp = stringA + privateKey;
String signValue = Md5Utils.md5toStr(stringSignTemp,true);
return signValue;
}
/**
* 如果obj为null,则返回默认值,不为null,则返回obj
*
* @param obj obj
* @param defaultValue 默认值
* @param <T> 值泛型
* @return obj不为null 返回obj,否则返回默认值
*/
public static <T> T defaultIfNull(T obj, T defaultValue) {
return obj != null ? obj : defaultValue;
}
//---------------------------------------------------------------------
// 对象类型判断
//---------------------------------------------------------------------
public static boolean isCollection(Object obj) {
return obj instanceof Collection;
}
public static boolean isMap(Object obj) {
return obj instanceof Map;
}
public static boolean isNumber(Object obj) {
return obj instanceof Number;
}
public static boolean isBoolean(Object obj) {
return obj instanceof Boolean;
}
public static boolean isEnum(Object obj) {
return obj instanceof Enum;
}
public static boolean isDate(Object obj) {
return obj instanceof Date || obj instanceof TemporalAccessor;
}
public static boolean isCharSequence(Object obj) {
return obj instanceof CharSequence;
}
/**
* 判断对象是否为八大基本类型包装类除外即(boolean, byte, char, short, int, long, float, and double)<br/>
*
* @param obj
* @return
*/
public static boolean isPrimitive(Object obj) {
return obj != null && obj.getClass().isPrimitive();
}
/**
* 判断对象是否为包装类或者非包装类的基本类型
*
* @param obj
* @return
*/
public static boolean isWrapperOrPrimitive(Object obj) {
return isPrimitive(obj) || isNumber(obj) || isCharSequence(obj) || isBoolean(obj);
}
/**
* 判断一个对象是否为数组
*
* @param obj
* @return
*/
public static boolean isArray(Object obj) {
return obj != null && obj.getClass().isArray();
}
/**
* 判断一个对象是否为基本类型数组即(int[], long[], boolean[], double[]....)
*
* @param obj
* @return
*/
public static boolean isPrimitiveArray(Object obj) {
return isArray(obj) && obj.getClass().getComponentType().isPrimitive();
}
}