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
|
public class ToStringUtils {
@SuppressWarnings("all") public static <T> T toObject(Class<T> clazz, String toString) throws Exception { if (Objects.isNull(clazz) || Objects.isNull(toString) || StringUtils.isEmpty(toString)) { return clazz == String.class ? (T) toString : null; } else if (TypeValueUtils.isBasicType(clazz)) { return (T) TypeValueUtils.basicTypeValue(clazz, toString.trim()); }
toString = TokenUtils.cleanClassPrefix(clazz, toString.trim()); toString = StringUtils.removeStart(toString, "(").trim(); toString = StringUtils.removeEnd(toString, ")").trim();
String token = null; T result = clazz.newInstance(); while (StringUtils.isNotEmpty(toString) && StringUtils.isNotEmpty(token = TokenUtils.splitToken(toString))) { toString = StringUtils.removeStart(StringUtils.removeStart(toString, token).trim(), ",").trim();
Pair<String, String> keyValue = TokenUtils.parseToken(token); Field field = FieldUtils.getField(clazz, keyValue.getKey(), true); Object value = TypeValueUtils.buildTypeValue(field, keyValue.getValue()); FieldUtils.writeField(field, result, value, true); } return result; }
static class TokenUtils {
static String cleanClassPrefix(Class clazz, String toString) { String simpleName = clazz.getSimpleName(); if (clazz.getName().contains("$")) { String rowSimpleName = StringUtils.substringAfterLast(clazz.getName(), "."); simpleName = StringUtils.replace(rowSimpleName, "$", "."); } return toString.startsWith(simpleName) ? StringUtils.removeStart(toString, simpleName).trim() : toString; }
private final static Map<Character, Character> tokenMap = new HashMap<>(); static { tokenMap.put(')', '('); tokenMap.put('}', '{'); tokenMap.put(']', '['); }
static String splitToken(String toString) { if (StringUtils.isBlank(toString)) { return toString; }
int bracketNum = 0; Stack<Character> stack = new Stack<>(); for (int i = 0; i < toString.length(); i++) { Character c = toString.charAt(i); if (tokenMap.containsValue(c)) { stack.push(c); } else if (tokenMap.containsKey(c) && Objects.equals(stack.peek(), tokenMap.get(c))) { stack.pop(); } else if ((c == ',') && stack.isEmpty()) { return toString.substring(0, i); } } if (stack.isEmpty()) { return toString; } throw new RuntimeException("splitFirstToken error, bracketNum=" + bracketNum + ", toString=" + toString); }
static Pair<String, String> parseToken(String token) { assert Objects.nonNull(token) && token.contains("="); int pos = token.indexOf("="); return new javafx.util.Pair<>(token.substring(0, pos), token.substring(pos + 1)); } }
static class TypeValueUtils {
static Set<Class> BASIC_TYPE = Stream.of( char.class, Character.class, boolean.class, Boolean.class, short.class, Short.class, int.class, Integer.class, float.class, Float.class, double.class, Double.class, long.class, Long.class, String.class).collect(Collectors.toSet());
static boolean isBasicType(Class clazz) { return BASIC_TYPE.contains(clazz); }
@SuppressWarnings("all") static Object buildTypeValue(Field field, String value) throws Exception { if (StringUtils.isBlank(value) || "null".equalsIgnoreCase(value)) { return field.getType() == String.class ? value : null; }
Class clazz = field.getType(); if (isBasicType(clazz)) { return basicTypeValue(field.getGenericType(), value); } else if (field.getGenericType() == Date.class) { return new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("us")).parse(value); } else if (clazz.isArray() || clazz.isAssignableFrom(Array.class)) { return arrayTypeValue(field.getType().getComponentType(), value); } else if (clazz.isAssignableFrom(List.class)) { return listTypeValue(field, value); } else if (clazz.isAssignableFrom(Map.class)) { return mapTypeValue(field, value); } else { return toObject(clazz, value); } }
static Object basicTypeValue(Type type, String value) { if (type == Character.class || type == char.class) { return value.charAt(0); } else if (type == Boolean.class || type == boolean.class) { return Boolean.valueOf(value); } else if (type == Short.class || type == short.class) { return Short.valueOf(value); } else if (type == Integer.class || type == int.class) { return Integer.valueOf(value); } else if (type == Float.class || type == float.class) { return Float.valueOf(value); } else if (type == Double.class || type == double.class) { return Double.valueOf(value); } else if (type == Long.class || type == long.class) { return Long.valueOf(value); } else if (type == String.class) { return value; } throw new RuntimeException("basicTypeValue error, type=" + type + ", value=" + value); }
@SuppressWarnings("unchecked") static Object listTypeValue(Field field, String fieldValue) throws Exception { fieldValue = StringUtils.removeStart(fieldValue, "[").trim(); fieldValue = StringUtils.removeEnd(fieldValue, "]").trim();
String token; List<Object> result = new ArrayList<>(); while (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(token = TokenUtils.splitToken(fieldValue))) { fieldValue = StringUtils.removeStart(StringUtils.removeStart(fieldValue, token).trim(), ",").trim(); result.add(toObject((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0], token)); } return result; }
@SuppressWarnings("unchecked") static <T> T[] arrayTypeValue(Class<?> componentType, String fieldValue) throws Exception { fieldValue = StringUtils.removeStart(fieldValue, "[").trim(); fieldValue = StringUtils.removeEnd(fieldValue, "]").trim();
String token; T[] result = newArray(componentType, fieldValue); for (int i = 0; StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(token = TokenUtils.splitToken(fieldValue)); i++) { fieldValue = StringUtils.removeStart(StringUtils.removeStart(fieldValue, token).trim(), ",").trim(); result[i] = (T) toObject(componentType, token); } return result; }
private static <T> T[] newArray(Class<?> componentType, String fieldValue) { String token; int lengh = 0; while (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(token = TokenUtils.splitToken(fieldValue))) { fieldValue = StringUtils.removeStart(StringUtils.removeStart(fieldValue, token).trim(), ",").trim(); lengh++; }
return (T[]) Array.newInstance(componentType, lengh); }
@SuppressWarnings("unchecked") static Map mapTypeValue(Field field, String toString) throws Exception { toString = StringUtils.removeStart(toString, "{").trim(); toString = StringUtils.removeEnd(toString, "}").trim();
String token; Map result = new HashMap(); while (StringUtils.isNotEmpty(token = TokenUtils.splitToken(toString))) { toString = StringUtils.removeStart(StringUtils.removeStart(toString, token).trim(), ",").trim(); assert token.contains("="); String fieldName = StringUtils.substringBefore(token, "=").trim(); String fieldValue = StringUtils.substringAfter(token, "=").trim();
result.put(basicTypeValue(((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0], fieldName), toObject((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[1], fieldValue));
} return result; } } }
|