#
Junjie
1 天以前 536e17a446c170a8b214aaf188b98817ee6258c1
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.core.common;
 
import com.core.annotations.CoolTranslate;
 
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
public class Cools {
 
    private static final char[] HEX_DIGITS = {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
    };
 
    public static boolean isEmpty(Object... objects) {
        for (Object object : objects) {
            if (isEmpty(object)) {
                return true;
            }
        }
        return false;
    }
 
    public static boolean isEmpty(Object object) {
        if (object == null) {
            return true;
        }
        if (object instanceof String) {
            return "".equals(object.toString().trim());
        }
        if (object instanceof List) {
            return ((List<?>) object).size() == 0;
        }
        if (object instanceof Map) {
            return ((Map<?, ?>) object).size() == 0;
        }
        if (object instanceof Set) {
            return ((Set<?>) object).size() == 0;
        }
        if (object instanceof Object[]) {
            return ((Object[]) object).length == 0;
        }
        if (object instanceof int[]) {
            return ((int[]) object).length == 0;
        }
        if (object instanceof long[]) {
            return ((long[]) object).length == 0;
        }
        return false;
    }
 
    public static int sqlLimitIndex(Integer curr, Integer limit) {
        return (curr.intValue() - 1) * limit.intValue();
    }
 
    public static String enToken(String data, String key) {
        return AesUtils.encrypt(data, zerofill(key, Integer.valueOf(16)));
    }
 
    public static String deTokn(String data, String key) {
        return AesUtils.decrypt(data, zerofill(key, Integer.valueOf(16)));
    }
 
    public static String zerofill(String str, Integer length) {
        if (str.length() == length.intValue()) {
            return str;
        }
        if (str.length() > length.intValue()) {
            return str.substring(0, 16);
        }
        StringBuilder builder = new StringBuilder(str);
        for (int i = 0; i < length.intValue() - str.length(); i++) {
            builder.append("0");
        }
        return builder.toString();
    }
 
    public static String deleteChar(String str, boolean end) {
        if (isEmpty(str)) {
            return "";
        }
        if (end) {
            return str.substring(0, str.length() - 1);
        }
        return str.substring(1);
    }
 
    public static String deleteChar(String str) {
        return deleteChar(str, true);
    }
 
    public static <T> T conver(Map<? extends String, ?> map, Class<T> clazz) {
        T target = null;
        try {
            Constructor<T> constructor = clazz.getDeclaredConstructor();
            boolean accessible = constructor.isAccessible();
            constructor.setAccessible(true);
            target = constructor.newInstance();
            constructor.setAccessible(accessible);
        } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        Class<?> current = clazz;
        while (current != null && !Object.class.equals(current)) {
            for (Field field : current.getDeclaredFields()) {
                if (Modifier.isFinal(field.getModifiers())
                        || Modifier.isStatic(field.getModifiers())
                        || Modifier.isTransient(field.getModifiers())) {
                    continue;
                }
                String fieldName = field.getName();
                Object val = map.containsKey(fieldName) ? map.get(fieldName) : null;
                if (val == null) {
                    continue;
                }
                boolean accessible = field.isAccessible();
                field.setAccessible(true);
                Class<?> fieldType = field.getType();
                try {
                    field.set(target, convertFieldValue(fieldType, val));
                } catch (Exception ex) {
                    System.err.println("convert error ===> Class[" + current + "],Field:[" + fieldName
                            + "],Type:[" + fieldType + "],Value:[" + val + "]");
                }
                field.setAccessible(accessible);
            }
            current = current.getSuperclass();
        }
        return target;
    }
 
    private static Object convertFieldValue(Class<?> fieldType, Object val)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        if (fieldType.isInstance(val)) {
            return val;
        }
        if (fieldType == String.class) {
            return String.valueOf(val);
        }
        if (fieldType == Integer.class || fieldType == int.class) {
            return Integer.valueOf(String.valueOf(val));
        }
        if (fieldType == Long.class || fieldType == long.class) {
            return Long.valueOf(String.valueOf(val));
        }
        if (fieldType == Short.class || fieldType == short.class) {
            return Short.valueOf(String.valueOf(val));
        }
        if (fieldType == Double.class || fieldType == double.class) {
            return Double.valueOf(String.valueOf(val));
        }
        if (fieldType == Float.class || fieldType == float.class) {
            return Float.valueOf(String.valueOf(val));
        }
        if (fieldType == Boolean.class || fieldType == boolean.class) {
            return Boolean.valueOf(String.valueOf(val));
        }
        if (fieldType == Byte.class || fieldType == byte.class) {
            return Byte.valueOf(String.valueOf(val));
        }
        if (fieldType.isEnum()) {
            return Enum.valueOf((Class<? extends Enum>) fieldType, String.valueOf(val));
        }
        Constructor<?> constructor = fieldType.getDeclaredConstructor(String.class);
        boolean constructorAccessible = constructor.isAccessible();
        constructor.setAccessible(true);
        Object result = constructor.newInstance(String.valueOf(val));
        constructor.setAccessible(constructorAccessible);
        return result;
    }
 
    public static Map<String, Object> conver(Object object) {
        Field[] fields = getAllFields(object.getClass());
        Map<String, Object> result = new HashMap<>();
        for (Field field : fields) {
            String fieldName = field.getName();
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            Object val = null;
            try {
                val = field.get(object);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            field.setAccessible(accessible);
            if (val != null) {
                result.put(fieldName, val);
            }
        }
        return result;
    }
 
    public static Field[] getAllFields(Class<?> clazz) {
        return getAllFields(clazz, null);
    }
 
    private static Field[] getAllFields(Class<?> clazz, Field[] fields) {
        Field[] allFields = fields == null ? clazz.getDeclaredFields() : fields;
        Class<?> superClass = clazz.getSuperclass();
        if (superClass == null || superClass == Object.class) {
            return allFields;
        }
        Field[] superFields = superClass.getDeclaredFields();
        allFields = addAll(allFields, superFields);
        return getAllFields(superClass, allFields);
    }
 
    public static <T> T[] addAll(T[] array1, T... array2) {
        if (array1 == null) {
            return clone(array2);
        }
        if (array2 == null) {
            return clone(array1);
        }
        Class<?> type1 = array1.getClass().getComponentType();
        T[] joined = (T[]) Array.newInstance(type1, array1.length + array2.length);
        System.arraycopy(array1, 0, joined, 0, array1.length);
        try {
            System.arraycopy(array2, 0, joined, array1.length, array2.length);
            return joined;
        } catch (ArrayStoreException ex) {
            Class<?> type2 = array2.getClass().getComponentType();
            if (!type1.isAssignableFrom(type2)) {
                throw new RuntimeException("Cannot store" + type2.getName() + " in an array of" + type1.getName(), ex);
            }
            throw ex;
        }
    }
 
    private static <T> T[] clone(T[] array) {
        return array == null ? null : (T[]) array.clone();
    }
 
    public static CoolMap add(String key, Object value) {
        CoolMap map = new CoolMap();
        map.put(key, value);
        return map;
    }
 
    public static String md5(String str) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] bytes = digest.digest(str.getBytes(StandardCharsets.UTF_8));
            char[] chars = new char[bytes.length * 2];
            for (int i = 0; i < bytes.length; i++) {
                int val = bytes[i];
                chars[i * 2] = HEX_DIGITS[(val & 0xF0) >> 4];
                chars[i * 2 + 1] = HEX_DIGITS[val & 0x0F];
            }
            return new String(chars).toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("md5加密失败,str=".concat(str));
        }
    }
 
    public static Map<String, Object> translate(Object object) {
        Field[] fields = getAllFields(object.getClass());
        Map<String, Object> result = new HashMap<>();
        for (Field field : fields) {
            String fieldName = field.getName();
            if (field.isAnnotationPresent(CoolTranslate.class)) {
                CoolTranslate translate = field.getAnnotation(CoolTranslate.class);
                if (!isEmpty(translate.value())) {
                    fieldName = translate.value();
                }
            }
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            Object val = null;
            try {
                val = field.get(object);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            field.setAccessible(accessible);
            if (val != null) {
                result.put(fieldName, val);
            }
        }
        return result;
    }
 
    public static boolean eq(String left, String right) {
        if (isEmpty(left) && isEmpty(right)) {
            return true;
        }
        if (isEmpty(left) && !isEmpty(right)) {
            return false;
        }
        if (isEmpty(right) && !isEmpty(left)) {
            return false;
        }
        return left.equals(right);
    }
 
    public static class CoolMap extends HashMap<String, Object> {
 
        public CoolMap add(String key, Object value) {
            put(key, value);
            return this;
        }
 
        public CoolMap $(String key, Object value) {
            put(key, value);
            return this;
        }
    }
}