Repository navigation
Expand file tree
/
Copy pathDefaultMappers.java
More file actions
429 lines (386 loc) · 20.7 KB
/
Copy pathDefaultMappers.java
File metadata and controls
429 lines (386 loc) · 20.7 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package org.javawebstack.abstractdata.mapper;
import org.javawebstack.abstractdata.*;
import org.javawebstack.abstractdata.exception.AbstractCoercingException;
import org.javawebstack.abstractdata.mapper.annotation.DateFormat;
import org.javawebstack.abstractdata.mapper.exception.MapperException;
import org.javawebstack.abstractdata.mapper.exception.MapperWrongTypeException;
import org.javawebstack.abstractdata.util.Helpers;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
public final class DefaultMappers {
public static final PrimitiveMapper PRIMITIVE = new PrimitiveMapper();
public static final CollectionMapper COLLECTION = new CollectionMapper();
public static final MapMapper MAP = new MapMapper();
public static final DateMapper DATE = new DateMapper();
public static final AbstractMapper ABSTRACT = new AbstractMapper();
public static final FallbackMapper FALLBACK = new FallbackMapper();
public static Map<Class<?>, MapperTypeAdapter> create() {
Map<Class<?>, MapperTypeAdapter> map = new HashMap<>();
for(MapperTypeAdapter adapter : new MapperTypeAdapter[] {
ABSTRACT,
PRIMITIVE,
COLLECTION,
MAP,
DATE
}) {
for(Class<?> type : adapter.getSupportedTypes())
map.put(type, adapter);
}
return map;
}
public static final class PrimitiveMapper implements MapperTypeAdapter {
private PrimitiveMapper() {}
public AbstractElement toAbstract(MapperContext context, Object value) throws MapperException {
if(value instanceof String)
return new AbstractPrimitive((String) value);
if(value instanceof Boolean)
return new AbstractPrimitive((Boolean) value);
return new AbstractPrimitive((Number) value);
}
public Object fromAbstract(MapperContext context, AbstractElement element, Class<?> type) throws MapperException {
if(type.equals(String.class)) {
try {
return element.string(context.getMapper().isStrict());
} catch (AbstractCoercingException ex) {
throw new MapperWrongTypeException(context.getField().getName(), "string", Helpers.typeName(element));
}
}
if(type.equals(char.class) || type.equals(Character.class)) {
if(type.isPrimitive() && element.isNull())
throw new MapperWrongTypeException(context.getField().getName(), "number", "null");
try {
String s = element.string(context.getMapper().isStrict());
if(s.length() != 1)
throw new MapperException("Expected string of length 1 for field " + context.getField().getName() + " but received " + s.length());
return s.charAt(0);
} catch (AbstractCoercingException ex) {
throw new MapperWrongTypeException(context.getField().getName(), "string", Helpers.typeName(element));
}
}
if(type.equals(Boolean.class) || type.equals(boolean.class)) {
if(type.isPrimitive() && element.isNull())
throw new MapperWrongTypeException(context.getField().getName(), "number", "null");
try {
return element.bool(context.getMapper().isStrict());
} catch (AbstractCoercingException ex) {
throw new MapperWrongTypeException(context.getField().getName(), "boolean", Helpers.typeName(element));
}
}
if(Number.class.isAssignableFrom(type) || type.isPrimitive()) {
if(type.isPrimitive() && element.isNull())
throw new MapperWrongTypeException(context.getField().getName(), "number", "null");
try {
if(type.equals(int.class) || type.equals(Integer.class))
return element.number(context.getMapper().isStrict()).intValue();
if(type.equals(long.class) || type.equals(Long.class))
return element.number(context.getMapper().isStrict()).longValue();
if(type.equals(short.class) || type.equals(Short.class))
return element.number(context.getMapper().isStrict()).shortValue();
if(type.equals(double.class) || type.equals(Double.class))
return element.number(context.getMapper().isStrict()).doubleValue();
if(type.equals(float.class) || type.equals(Float.class))
return element.number(context.getMapper().isStrict()).floatValue();
if(type.equals(byte.class) || type.equals(Byte.class))
return element.number(context.getMapper().isStrict()).byteValue();
if(type.equals(Number.class))
return element.number(context.getMapper().isStrict());
return element.number(context.getMapper().isStrict());
} catch (AbstractCoercingException ex) {
throw new MapperWrongTypeException(context.getField().getName(), "number", Helpers.typeName(element));
}
}
throw new MapperWrongTypeException(context.getField().getName(), "primitive", Helpers.typeName(element));
}
public Class<?>[] getSupportedTypes() {
return new Class[] {
String.class,
char.class,
Character.class,
Integer.class,
int.class,
Long.class,
long.class,
Short.class,
short.class,
Float.class,
float.class,
Double.class,
double.class,
Boolean.class,
boolean.class,
Number.class
};
}
}
public static final class CollectionMapper implements MapperTypeAdapter {
private CollectionMapper() {}
public AbstractElement toAbstract(MapperContext context, Object value) throws MapperException {
Collection<Object> collection = (Collection<Object>) value;
AbstractArray array = new AbstractArray();
collection.forEach(e -> array.add(context.getMapper().map(e)));
return array;
}
public Object fromAbstract(MapperContext context, AbstractElement element, Class<?> type) throws MapperException {
if(type.equals(List.class) || type.equals(AbstractList.class))
type = ArrayList.class;
if(type.equals(Set.class))
type = HashSet.class;
try {
try {
Collection<Object> collection = (Collection<Object>) type.newInstance();
Class<?>[] genericTypes = context.getGenericTypes();
Class<?> elementType = genericTypes.length > 0 ? genericTypes[0] : null;
if(elementType == null) {
for(AbstractElement e : element.array(context.getMapper().isStrict())) {
elementType = Helpers.guessGeneric(e);
if(elementType != null)
break;
}
}
for(AbstractElement e : element.array(context.getMapper().isStrict()))
collection.add(context.getMapper().map(e, elementType));
return collection;
} catch (AbstractCoercingException ex) {
throw new MapperWrongTypeException(context.getField().getName(), "array", Helpers.typeName(element));
}
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public Class<?>[] getSupportedTypes() {
return new Class[] {
List.class,
ArrayList.class,
CopyOnWriteArrayList.class,
LinkedList.class,
AbstractList.class,
Set.class,
HashSet.class,
EnumSet.class,
TreeSet.class,
LinkedHashSet.class,
CopyOnWriteArraySet.class,
AbstractSet.class,
ConcurrentSkipListSet.class,
Vector.class,
Stack.class
};
}
}
public static final class MapMapper implements MapperTypeAdapter {
private MapMapper() {}
public AbstractElement toAbstract(MapperContext context, Object value) throws MapperException {
Map<Object, Object> map = (Map<Object, Object>) value;
AbstractObject object = new AbstractObject();
map.forEach((k, v) -> object.set(k.toString(), context.getMapper().map(v)));
return object;
}
public Object fromAbstract(MapperContext context, AbstractElement element, Class<?> type) throws MapperException {
if(type.equals(Map.class) || type.equals(AbstractMap.class))
type = HashMap.class;
try {
try {
Map<Object, Object> map = (Map<Object, Object>) type.newInstance();
Class<?>[] genericTypes = context.getGenericTypes();
Class<?> keyType = genericTypes.length > 0 ? genericTypes[0] : String.class;
Class<?> valueType = genericTypes.length > 1 ? genericTypes[1] : null;
if(valueType == null) {
for(AbstractElement e : element.object().values()) {
valueType = Helpers.guessGeneric(e);
if(valueType != null)
break;
}
}
for(String k : element.object().keys())
map.put(context.getMapper().map(new AbstractPrimitive(k), keyType), context.getMapper().map(element.object().get(k), valueType));
return map;
} catch (AbstractCoercingException ex) {
throw new MapperWrongTypeException(context.getField().getName(), "object", Helpers.typeName(element));
}
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public Class<?>[] getSupportedTypes() {
return new Class[] {
Map.class,
HashMap.class,
LinkedHashMap.class,
IdentityHashMap.class,
Hashtable.class,
Properties.class,
TreeMap.class,
EnumMap.class,
ConcurrentHashMap.class,
ConcurrentSkipListMap.class,
WeakHashMap.class,
AbstractMap.class
};
}
}
public static final class DateMapper implements MapperTypeAdapter {
private DateMapper() {}
public AbstractElement toAbstract(MapperContext context, Object value) throws MapperException {
DateFormat df = context.getAnnotation(DateFormat.class);
if(value instanceof Date) {
if(df != null && df.epoch()) {
long time = ((Date) value).getTime();
if(!df.millis())
time /= 1000;
return new AbstractPrimitive(time);
}
java.text.DateFormat dateFormat = (df != null && df.value().length() > 0) ? new SimpleDateFormat(df.value()) : context.getMapper().getDateFormat();
return new AbstractPrimitive(dateFormat.format((Date) value));
}
return null;
}
public Object fromAbstract(MapperContext context, AbstractElement element, Class<?> type) throws MapperException {
try {
DateFormat df = context.getAnnotation(DateFormat.class);
Date date;
if(df != null && df.epoch()) {
long time = element.number(context.getMapper().isStrict()).longValue();
if(!df.millis())
time *= 1000;
date = new Date(time);
} else {
java.text.DateFormat dateFormat = (df != null && df.value().length() > 0) ? new SimpleDateFormat(df.value()) : context.getMapper().getDateFormat();
date = dateFormat.parse(element.string(context.getMapper().isStrict()));
}
if(type.equals(Date.class))
return date;
if(type.equals(java.sql.Date.class))
return new java.sql.Date(date.getTime());
if(type.equals(Timestamp.class))
return new Timestamp(date.getTime());
throw new MapperException("Unsupported date type '" + type.getName() + "'");
} catch (ParseException | NumberFormatException | AbstractCoercingException ex) {
throw new MapperException("Failed to parse date '" + element.string() + "'" + (context.getField() != null ? (" for field '" + context.getField().getName() + "'") : ""));
}
}
public Class<?>[] getSupportedTypes() {
return new Class[] {
Date.class,
Timestamp.class,
java.sql.Date.class
};
}
}
public static final class AbstractMapper implements MapperTypeAdapter {
private AbstractMapper() {}
public AbstractElement toAbstract(MapperContext context, Object value) throws MapperException {
return (AbstractElement) value;
}
public Object fromAbstract(MapperContext context, AbstractElement element, Class<?> type) throws MapperException {
if(type.equals(AbstractElement.class))
return element;
if(type.equals(AbstractNull.class) && !(element instanceof AbstractNull))
throw new MapperWrongTypeException(context.getField().getName(), "null", Helpers.typeName(element));
if(type.equals(AbstractPrimitive.class) && !(element instanceof AbstractPrimitive))
throw new MapperWrongTypeException(context.getField().getName(), "primitive", Helpers.typeName(element));
if(type.equals(AbstractObject.class) && !(element instanceof AbstractObject))
throw new MapperWrongTypeException(context.getField().getName(), "object", Helpers.typeName(element));
if(type.equals(AbstractArray.class) && !(element instanceof AbstractArray))
throw new MapperWrongTypeException(context.getField().getName(), "array", Helpers.typeName(element));
return element;
}
public Class<?>[] getSupportedTypes() {
return new Class[] {
AbstractElement.class,
AbstractNull.class,
AbstractPrimitive.class,
AbstractArray.class,
AbstractObject.class
};
}
}
public static class FallbackMapper implements MapperTypeAdapter {
public AbstractElement toAbstract(MapperContext context, Object value) throws MapperException {
if(value.getClass().isEnum())
return new AbstractPrimitive(((Enum<?>) value).name());
if(value.getClass().equals(UUID.class))
return new AbstractPrimitive(value.toString());
MapperTypeSpec spec = MapperTypeSpec.get(value.getClass());
if(spec == null)
throw new MapperException("Unmappable type '" + value.getClass().getName() + "'");
try {
AbstractObject object = new AbstractObject();
for(MapperTypeSpec.FieldSpec fs : spec.getFieldSpecs()) {
if(context.getMapper().isExposeRequired() ? !fs.isExpose() : fs.isHidden())
continue;
String k = fs.getName() != null ? fs.getName() : context.getMapper().getNamingPolicy().toAbstract(fs.getField().getName());
AbstractElement e = context.getMapper().map(new MapperContext(context.getMapper(), fs.getField(), fs.getAnnotations()).adapter(fs.getAdapter()), fs.getField().get(value));
if(e.isNull() && context.getMapper().shouldOmitNull() && fs.shouldOmitNull())
continue;
object.set(k, e);
}
if(spec.getAdditionalField() != null) {
AbstractObject additional = (AbstractObject) spec.getAdditionalField().get(value);
if(additional != null)
additional.forEach(object::set);
}
return object;
} catch (IllegalAccessException ex) {
throw new MapperException(ex.getMessage());
}
}
public Object fromAbstract(MapperContext context, AbstractElement element, Class type) throws MapperException {
if(type.isEnum()) {
if(!element.isString())
throw new MapperWrongTypeException(context.getField().getName(), "string", Helpers.typeName(element));
try {
return Enum.valueOf(type, element.string());
} catch (IllegalArgumentException ex) {
throw new MapperException("There is no enum constant '" + element.string() + "'");
}
}
if(type.equals(UUID.class))
return UUID.fromString(element.string());
if(!element.isObject())
throw new MapperWrongTypeException(context.getField().getName(), "object", Helpers.typeName(element));
MapperTypeSpec spec = MapperTypeSpec.get(type);
if(spec == null)
throw new MapperException("Unmappable type '" + type.getName() + "'");
AbstractObject o = element.object();
try {
Constructor constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
Object obj = constructor.newInstance();
AbstractObject additional = spec.getAdditionalField() == null ? null : new AbstractObject();
List<String> fieldNames = spec.getFieldSpecs().stream().map(s -> s.getField().getName()).collect(Collectors.toList());
for(String k : o.keys()) {
final String fk1 = k;
MapperTypeSpec.FieldSpec fs = spec.getFieldSpecs().stream().filter(s -> fk1.equals(s.getName())).findFirst().orElse(null);
if(fs != null) {
if(context.getMapper().isExposeRequired() ? !fs.isExpose() : fs.isHidden())
continue;
fs.getField().set(obj, context.getMapper().map(new MapperContext(context.getMapper(), fs.getField(), fs.getAnnotations()).adapter(fs.getAdapter()), o.get(fk1), fs.getField().getType()));
continue;
}
final String fk2 = context.getMapper().getNamingPolicy().fromAbstract(k, fieldNames);
fs = spec.getFieldSpecs().stream().filter(s -> s.getName() == null && fk2.equals(s.getField().getName())).findFirst().orElse(null);
if(fs != null) {
if(context.getMapper().isExposeRequired() ? !fs.isExpose() : fs.isHidden())
continue;
Object val = context.getMapper().map(new MapperContext(context.getMapper(), fs.getField(), fs.getAnnotations()).adapter(fs.getAdapter()), o.get(fk1),fs.getField().getType());
fs.getField().set(obj, val);
continue;
}
if(additional != null)
additional.set(fk1, o.get(fk1));
}
if(additional != null)
spec.getAdditionalField().set(obj, additional);
return obj;
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new MapperException(e.getMessage());
}
}
}
}