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 conver(Map map, Class clazz) { T target = null; try { Constructor 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) 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 conver(Object object) { Field[] fields = getAllFields(object.getClass()); Map 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[] 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[] 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 translate(Object object) { Field[] fields = getAllFields(object.getClass()); Map 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 { public CoolMap add(String key, Object value) { put(key, value); return this; } public CoolMap $(String key, Object value) { put(key, value); return this; } } }