-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathDatabindContext.java
More file actions
506 lines (447 loc) · 19.2 KB
/
DatabindContext.java
File metadata and controls
506 lines (447 loc) · 19.2 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package tools.jackson.databind;
import java.lang.reflect.Type;
import java.util.Locale;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.*;
import tools.jackson.databind.cfg.DatatypeFeature;
import tools.jackson.databind.cfg.DatatypeFeatures;
import tools.jackson.databind.cfg.HandlerInstantiator;
import tools.jackson.databind.cfg.MapperConfig;
import tools.jackson.databind.introspect.Annotated;
import tools.jackson.databind.introspect.AnnotatedClass;
import tools.jackson.databind.introspect.ClassIntrospector;
import tools.jackson.databind.introspect.ObjectIdInfo;
import tools.jackson.databind.jsontype.PolymorphicTypeValidator;
import tools.jackson.databind.jsontype.PolymorphicTypeValidator.Validity;
import tools.jackson.databind.type.TypeFactory;
import tools.jackson.databind.util.ClassUtil;
import tools.jackson.databind.util.Converter;
/**
* Shared base class for {@link DeserializationContext} and
* {@link SerializationContext}, context objects passed through data-binding
* process. Designed so that some of implementations can rely on shared
* aspects like access to secondary contextual objects like type factories
* or handler instantiators.
*/
public abstract class DatabindContext
{
/**
* Let's limit length of error messages, for cases where underlying data
* may be very large -- no point in spamming logs with megabytes of meaningless
* data.
*/
private final static int MAX_ERROR_STR_LEN = 500;
/*
/**********************************************************************
/* Generic config access
/**********************************************************************
*/
/**
* Accessor to currently active configuration (both per-request configs
* and per-mapper config).
*/
public abstract MapperConfig<?> getConfig();
/**
* Convenience method for accessing serialization view in use (if any); equivalent to:
*<pre>
* getConfig().getAnnotationIntrospector();
*</pre>
*/
public abstract AnnotationIntrospector getAnnotationIntrospector();
/*
/**********************************************************************
/* Access to specific config settings
/**********************************************************************
*/
/**
* Convenience method for checking whether specified Mapper
* feature is enabled or not.
* Shortcut for:
*<pre>
* getConfig().isEnabled(feature);
*</pre>
*/
public abstract boolean isEnabled(MapperFeature feature);
public final boolean isAnnotationProcessingEnabled() {
return isEnabled(MapperFeature.USE_ANNOTATIONS);
}
/**
* Method for checking whether specified datatype
* feature is enabled or not.
*
* @since 2.14
*/
public abstract boolean isEnabled(DatatypeFeature feature);
/**
* @since 2.15
*/
public abstract DatatypeFeatures getDatatypeFeatures();
/**
* Convenience method for accessing serialization view in use (if any); equivalent to:
*<pre>
* getConfig().canOverrideAccessModifiers();
*</pre>
*/
public abstract boolean canOverrideAccessModifiers();
/**
* Accessor for locating currently active view, if any;
* returns null if no view has been set.
*/
public abstract Class<?> getActiveView();
public abstract Locale getLocale();
public abstract TimeZone getTimeZone();
public abstract JsonFormat.Value getDefaultPropertyFormat(Class<?> baseType);
/*
/**********************************************************************
/* Generic attributes
/**********************************************************************
*/
/**
* Method for accessing attributes available in this context.
* Per-call attributes have highest precedence; attributes set
* via {@link ObjectReader} or {@link ObjectWriter} have lower
* precedence.
*
* @param key Key of the attribute to get
* @return Value of the attribute, if any; null otherwise
*/
public abstract Object getAttribute(Object key);
/**
* Method for setting per-call value of given attribute.
* This will override any previously defined value for the
* attribute within this context.
*
* @param key Key of the attribute to set
* @param value Value to set attribute to
*
* @return This context object, to allow chaining
*/
public abstract DatabindContext setAttribute(Object key, Object value);
/*
/**********************************************************************
/* Type instantiation/resolution
/**********************************************************************
*/
/**
* Convenience method for constructing {@link JavaType} for given JDK
* type (usually {@link java.lang.Class})
*/
public JavaType constructType(Type type) {
if (type == null) {
return null;
}
return getTypeFactory().constructType(type);
}
/**
* Convenience method for constructing subtypes, retaining generic
* type parameter (if any).
*<p>
* Note: since 2.11 handling has varied a bit across serialization, deserialization.
*/
public abstract JavaType constructSpecializedType(JavaType baseType, Class<?> subclass);
/**
* Lookup method called when code needs to resolve class name from input;
* usually simple lookup.
* Note that unlike {@link #resolveAndValidateSubType} this method DOES NOT
* validate subtype against configured {@link PolymorphicTypeValidator}: usually
* because such check has already been made.
*/
public JavaType resolveSubType(JavaType baseType, String subClassName)
{
// 30-Jan-2010, tatu: Most ids are basic class names; so let's first
// check if any generics info is added; and only then ask factory
// to do translation when necessary
if (subClassName.indexOf('<') > 0) {
// note: may want to try combining with specialization (esp for EnumMap)?
// 17-Aug-2017, tatu: As per [databind#1735] need to ensure assignment
// compatibility -- needed later anyway, and not doing so may open
// security issues.
JavaType t = getTypeFactory().constructFromCanonical(subClassName);
if (t.isTypeOrSubTypeOf(baseType.getRawClass())) {
return t;
}
} else {
Class<?> cls;
try {
cls = getTypeFactory().findClass(subClassName);
} catch (ClassNotFoundException e) { // let caller handle this problem
return null;
} catch (Exception e) {
throw invalidTypeIdException(baseType, subClassName, String.format(
"problem: (%s) %s",
e.getClass().getName(),
ClassUtil.exceptionMessage(e)));
}
if (baseType.isTypeOrSuperTypeOf(cls)) {
return getTypeFactory().constructSpecializedType(baseType, cls);
}
}
throw invalidTypeIdException(baseType, subClassName, "Not a subtype");
}
/**
* Lookup method similar to {@link #resolveSubType} but one that also validates
* that resulting subtype is valid according to given {@link PolymorphicTypeValidator}.
*/
public JavaType resolveAndValidateSubType(JavaType baseType, String subClass,
PolymorphicTypeValidator ptv)
{
// Off-line the special case of generic (parameterized) type:
final int ltIndex = subClass.indexOf('<');
if (ltIndex > 0) {
return _resolveAndValidateGeneric(baseType, subClass, ptv, ltIndex);
}
PolymorphicTypeValidator.Validity vld = ptv.validateSubClassName(this, baseType, subClass);
if (vld == Validity.DENIED) {
return _throwSubtypeNameNotAllowed(baseType, subClass, ptv);
}
final Class<?> cls;
try {
cls = getTypeFactory().findClass(subClass);
} catch (ClassNotFoundException e) { // let caller handle this problem
return null;
} catch (Exception e) {
throw invalidTypeIdException(baseType, subClass, String.format(
"problem: (%s) %s",
e.getClass().getName(),
ClassUtil.exceptionMessage(e)));
}
if (!baseType.isTypeOrSuperTypeOf(cls)) {
return _throwNotASubtype(baseType, subClass);
}
final JavaType subType = getTypeFactory().constructSpecializedType(baseType, cls);
// May skip check if type was allowed by subclass name already
if (vld != Validity.ALLOWED) {
if (ptv.validateSubType(this, baseType, subType) != Validity.ALLOWED) {
return _throwSubtypeClassNotAllowed(baseType, subClass, ptv);
}
}
return subType;
}
private JavaType _resolveAndValidateGeneric(JavaType baseType, String subClass,
PolymorphicTypeValidator ptv, int ltIndex)
{
// 24-Apr-2019, tatu: Not 100% sure if we should pass name with type parameters
// or not, but guessing it's more convenient not to have to worry about it so
// strip out
PolymorphicTypeValidator.Validity vld = ptv.validateSubClassName(this, baseType, subClass.substring(0, ltIndex));
if (vld == Validity.DENIED) {
return _throwSubtypeNameNotAllowed(baseType, subClass, ptv);
}
JavaType subType = getTypeFactory().constructFromCanonical(subClass);
if (!subType.isTypeOrSubTypeOf(baseType.getRawClass())) {
return _throwNotASubtype(baseType, subClass);
}
// Unless we were approved already by name, check that actual sub-class acceptable:
if (vld != Validity.ALLOWED) {
if (ptv.validateSubType(this, baseType, subType) != Validity.ALLOWED) {
return _throwSubtypeClassNotAllowed(baseType, subClass, ptv);
}
}
return subType;
}
protected <T> T _throwNotASubtype(JavaType baseType, String subType) throws DatabindException {
throw invalidTypeIdException(baseType, subType, "Not a subtype");
}
protected <T> T _throwSubtypeNameNotAllowed(JavaType baseType, String subType,
PolymorphicTypeValidator ptv)
throws DatabindException
{
throw invalidTypeIdException(baseType, subType,
"Configured `PolymorphicTypeValidator` (of type "+ClassUtil.classNameOf(ptv)+") denied resolution");
}
protected <T> T _throwSubtypeClassNotAllowed(JavaType baseType, String subType,
PolymorphicTypeValidator ptv)
throws DatabindException
{
throw invalidTypeIdException(baseType, subType,
"Configured `PolymorphicTypeValidator` (of type "+ClassUtil.classNameOf(ptv)+") denied resolution");
}
/**
* Helper method for constructing exception to indicate that given type id
* could not be resolved to a valid subtype of specified base type.
* Most commonly called during polymorphic deserialization.
*<p>
* Note that most of the time this method should NOT be called directly: instead,
* method <code>handleUnknownTypeId()</code> should be called which will call this method
* if necessary.
*/
protected abstract DatabindException invalidTypeIdException(JavaType baseType, String typeId,
String extraDesc);
public abstract TypeFactory getTypeFactory();
/*
/**********************************************************************
/* Annotation, BeanDescription introspection
/**********************************************************************
*/
/**
* Convenience method for doing full "for serialization or deserialization"
* introspection of specified type; results may be cached for duration (lifespan)
* of this context as well.
*/
public final BeanDescription introspectBeanDescription(JavaType type) {
return introspectBeanDescription(type, introspectClassAnnotations(type));
}
public abstract BeanDescription introspectBeanDescription(JavaType type,
AnnotatedClass classDef);
public BeanDescription.Supplier lazyIntrospectBeanDescription(JavaType type) {
return new BeanDescription.LazySupplier(getConfig(), type) {
@Override
protected BeanDescription _construct(JavaType forType, AnnotatedClass ac) {
System.out.println("lazyIntrospectBeanDescription.beanDesc("+forType+")");
return introspectBeanDescription(forType);
}
@Override
protected AnnotatedClass _introspect(JavaType forType) {
System.out.println("lazyIntrospectBeanDescription.annotatedClass("+forType+")");
return introspectClassAnnotations(forType);
}
};
}
public AnnotatedClass introspectClassAnnotations(JavaType type) {
return classIntrospector().introspectClassAnnotations(type);
}
public AnnotatedClass introspectDirectClassAnnotations(JavaType type) {
return classIntrospector().introspectDirectClassAnnotations(type);
}
public AnnotatedClass introspectClassAnnotations(Class<?> rawType) {
return introspectClassAnnotations(constructType(rawType));
}
protected abstract ClassIntrospector classIntrospector();
/*
/**********************************************************************
/* Helper object construction
/**********************************************************************
*/
public ObjectIdGenerator<?> objectIdGeneratorInstance(Annotated annotated,
ObjectIdInfo objectIdInfo)
{
Class<?> implClass = objectIdInfo.getGeneratorType();
final MapperConfig<?> config = getConfig();
HandlerInstantiator hi = config.getHandlerInstantiator();
ObjectIdGenerator<?> gen = (hi == null) ? null : hi.objectIdGeneratorInstance(config, annotated, implClass);
if (gen == null) {
gen = (ObjectIdGenerator<?>) ClassUtil.createInstance(implClass,
config.canOverrideAccessModifiers());
}
return gen.forScope(objectIdInfo.getScope());
}
public ObjectIdResolver objectIdResolverInstance(Annotated annotated, ObjectIdInfo objectIdInfo)
{
Class<? extends ObjectIdResolver> implClass = objectIdInfo.getResolverType();
final MapperConfig<?> config = getConfig();
HandlerInstantiator hi = config.getHandlerInstantiator();
ObjectIdResolver resolver = (hi == null) ? null : hi.resolverIdGeneratorInstance(config, annotated, implClass);
if (resolver == null) {
resolver = ClassUtil.createInstance(implClass, config.canOverrideAccessModifiers());
}
return resolver;
}
/**
* Helper method to use to construct a {@link Converter}, given a definition
* that may be either actual converter instance, or Class for instantiating one.
*/
@SuppressWarnings("unchecked")
public Converter<Object,Object> converterInstance(Annotated annotated,
Object converterDef)
{
if (converterDef == null) {
return null;
}
if (converterDef instanceof Converter<?,?>) {
return (Converter<Object,Object>) converterDef;
}
if (!(converterDef instanceof Class)) {
throw new IllegalStateException("AnnotationIntrospector returned Converter definition of type "
+converterDef.getClass().getName()+"; expected type Converter or Class<Converter> instead");
}
Class<?> converterClass = (Class<?>)converterDef;
// there are some known "no class" markers to consider too:
if (converterClass == Converter.None.class || ClassUtil.isBogusClass(converterClass)) {
return null;
}
if (!Converter.class.isAssignableFrom(converterClass)) {
throw new IllegalStateException("AnnotationIntrospector returned Class "
+converterClass.getName()+"; expected Class<Converter>");
}
final MapperConfig<?> config = getConfig();
HandlerInstantiator hi = config.getHandlerInstantiator();
Converter<?,?> conv = (hi == null) ? null : hi.converterInstance(config, annotated, converterClass);
if (conv == null) {
conv = (Converter<?,?>) ClassUtil.createInstance(converterClass,
config.canOverrideAccessModifiers());
}
return (Converter<Object,Object>) conv;
}
/*
/**********************************************************************
/* Misc config access
/**********************************************************************
*/
public abstract PropertyName findRootName(JavaType rootType);
public abstract PropertyName findRootName(Class<?> rawRootType);
/*
/**********************************************************************
/* Error reporting
/**********************************************************************
*/
/**
* Helper method called to indicate a generic problem that stems from type
* definition(s), not input data, or input/output state; typically this
* means throwing a {@link tools.jackson.databind.exc.InvalidDefinitionException}.
*/
public abstract <T> T reportBadDefinition(JavaType type, String msg)
throws DatabindException;
public <T> T reportBadDefinition(Class<?> type, String msg)
throws DatabindException
{
return reportBadDefinition(constructType(type), msg);
}
public abstract <T> T reportBadTypeDefinition(BeanDescription bean,
String msg, Object... msgArgs)
throws DatabindException;
public <T> T reportBadTypeDefinition(BeanDescription.Supplier beanDescRef,
String msg, Object... msgArgs)
throws DatabindException {
return reportBadTypeDefinition(beanDescRef.get(), msg, msgArgs);
}
/*
/**********************************************************************
/* Helper methods
/**********************************************************************
*/
protected final String _format(String msg, Object... msgArgs) {
if (msgArgs.length > 0) {
return String.format(msg, msgArgs);
}
return msg;
}
protected final String _truncate(String desc) {
if (desc == null) {
return "";
}
if (desc.length() <= MAX_ERROR_STR_LEN) {
return desc;
}
return desc.substring(0, MAX_ERROR_STR_LEN) + "]...[" + desc.substring(desc.length() - MAX_ERROR_STR_LEN);
}
protected String _quotedString(String desc) {
if (desc == null) {
return "[N/A]";
}
// !!! should we quote it? (in case there are control chars, linefeeds)
return String.format("\"%s\"", _truncate(desc));
}
protected String _colonConcat(String msgBase, String extra) {
if (extra == null) {
return msgBase;
}
return msgBase + ": " + extra;
}
protected String _desc(String desc) {
if (desc == null) {
return "[N/A]";
}
// !!! should we quote it? (in case there are control chars, linefeeds)
return _truncate(desc);
}
}