Skip to content

Commit fd46fdf

Browse files
committed
Refactoring to help #1498 implementation (but need to be merge piecemeal wrt 3.0)
1 parent bc1f60e commit fd46fdf

File tree

1 file changed

+82
-41
lines changed

1 file changed

+82
-41
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java

+82-41
Original file line numberDiff line numberDiff line change
@@ -252,41 +252,54 @@ protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationCo
252252
BeanDescription beanDesc)
253253
throws JsonMappingException
254254
{
255-
CreatorCollector creators = new CreatorCollector(beanDesc, ctxt.getConfig());
256-
AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
257-
258-
// need to construct suitable visibility checker:
259-
final DeserializationConfig config = ctxt.getConfig();
260-
VisibilityChecker<?> vchecker = config.getDefaultVisibilityChecker(beanDesc.getBeanClass(),
261-
beanDesc.getClassInfo());
255+
final CreatorCollectionState ccState;
256+
257+
{
258+
final DeserializationConfig config = ctxt.getConfig();
259+
// need to construct suitable visibility checker:
260+
final VisibilityChecker<?> vchecker = config.getDefaultVisibilityChecker(beanDesc.getBeanClass(),
261+
beanDesc.getClassInfo());
262+
263+
// 24-Sep-2014, tatu: Tricky part first; need to merge resolved property information
264+
// (which has creator parameters sprinkled around) with actual creator
265+
// declarations (which are needed to access creator annotation, amongst other things).
266+
// Easiest to combine that info first, then pass it to remaining processing.
267+
268+
// 15-Mar-2015, tatu: Alas, this won't help with constructors that only have implicit
269+
// names. Those will need to be resolved later on.
270+
final CreatorCollector creators = new CreatorCollector(beanDesc, config);
271+
Map<AnnotatedWithParams,BeanPropertyDefinition[]> creatorDefs = _findCreatorsFromProperties(ctxt,
272+
beanDesc);
273+
ccState = new CreatorCollectionState(ctxt, beanDesc, vchecker,
274+
creators, creatorDefs);
275+
}
262276

263-
/* 24-Sep-2014, tatu: Tricky part first; need to merge resolved property information
264-
* (which has creator parameters sprinkled around) with actual creator
265-
* declarations (which are needed to access creator annotation, amongst other things).
266-
* Easiest to combine that info first, then pass it to remaining processing.
267-
*/
268-
/* 15-Mar-2015, tatu: Alas, this won't help with constructors that only have implicit
269-
* names. Those will need to be resolved later on.
270-
*/
271-
Map<AnnotatedWithParams,BeanPropertyDefinition[]> creatorDefs = _findCreatorsFromProperties(ctxt,
272-
beanDesc);
273277
// Important: first add factory methods; then constructors, so
274278
// latter can override former!
275-
_addDeserializerFactoryMethods(ctxt, beanDesc, vchecker, intr, creators, creatorDefs);
279+
_addDeserializerFactoryMethods(ctxt, ccState);
276280
// constructors only usable on concrete types:
277281
if (beanDesc.getType().isConcrete()) {
278282
// [databind#2709]: Record support
279283
if (beanDesc.getType().isRecordType()) {
280284
final List<String> names = new ArrayList<>();
281285
AnnotatedConstructor canonical = JDK14Util.findRecordConstructor(ctxt, beanDesc, names);
282286
if (canonical != null) {
283-
_addRecordConstructor(ctxt, beanDesc, creators, canonical, names);
284-
return creators.constructValueInstantiator(ctxt);
287+
_addRecordConstructor(ctxt, ccState, canonical, names);
288+
return ccState.creators.constructValueInstantiator(ctxt);
285289
}
286290
}
287-
_addDeserializerConstructors(ctxt, beanDesc, vchecker, intr, creators, creatorDefs);
291+
// 25-Jan-2017, tatu: As per [databind#1501], [databind#1502], [databind#1503], best
292+
// for now to skip attempts at using anything but no-args constructor (see
293+
// `InnerClassProperty` construction for that)
294+
final boolean isNonStaticInnerClass = beanDesc.isNonStaticInnerClass();
295+
if (isNonStaticInnerClass) {
296+
// TODO: look for `@JsonCreator` annotated ones, throw explicit exception?
297+
;
298+
} else {
299+
_addDeserializerConstructors(ctxt, ccState);
300+
}
288301
}
289-
return creators.constructValueInstantiator(ctxt);
302+
return ccState.creators.constructValueInstantiator(ctxt);
290303
}
291304

292305
protected Map<AnnotatedWithParams,BeanPropertyDefinition[]> _findCreatorsFromProperties(DeserializationContext ctxt,
@@ -364,20 +377,15 @@ public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config
364377
*/
365378

366379
protected void _addDeserializerConstructors(DeserializationContext ctxt,
367-
BeanDescription beanDesc, VisibilityChecker<?> vchecker,
368-
AnnotationIntrospector intr, CreatorCollector creators,
369-
Map<AnnotatedWithParams,BeanPropertyDefinition[]> creatorParams)
380+
CreatorCollectionState ccState)
370381
throws JsonMappingException
371382
{
372-
// 25-Jan-2017, tatu: As per [databind#1501], [databind#1502], [databind#1503], best
373-
// for now to skip attempts at using anything but no-args constructor (see
374-
// `InnerClassProperty` construction for that)
375-
final boolean isNonStaticInnerClass = beanDesc.isNonStaticInnerClass();
376-
if (isNonStaticInnerClass) {
377-
// TODO: look for `@JsonCreator` annotated ones, throw explicit exception?
378-
return;
379-
}
380-
383+
final BeanDescription beanDesc = ccState.beanDesc;
384+
final CreatorCollector creators = ccState.creators;
385+
final AnnotationIntrospector intr = ccState.annotationIntrospector();
386+
final VisibilityChecker<?> vchecker = ccState.vchecker;
387+
final Map<AnnotatedWithParams, BeanPropertyDefinition[]> creatorParams = ccState.creatorDefs;
388+
381389
// First things first: the "default constructor" (zero-arg
382390
// constructor; whether implicit or explicit) is NOT included
383391
// in list of constructors, so needs to be handled separately.
@@ -556,8 +564,7 @@ protected void _addDeserializerConstructors(DeserializationContext ctxt,
556564
*
557565
* @since 2.12
558566
*/
559-
protected void _addRecordConstructor(DeserializationContext ctxt,
560-
BeanDescription beanDesc, CreatorCollector creators,
567+
protected void _addRecordConstructor(DeserializationContext ctxt, CreatorCollectionState ccState,
561568
AnnotatedConstructor canonical, List<String> implicitNames)
562569
throws JsonMappingException
563570
{
@@ -572,9 +579,9 @@ protected void _addRecordConstructor(DeserializationContext ctxt,
572579
if (name == null || name.isEmpty()) {
573580
name = PropertyName.construct(implicitNames.get(i));
574581
}
575-
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
582+
properties[i] = constructCreatorProperty(ctxt, ccState.beanDesc, name, i, param, injectable);
576583
}
577-
creators.addPropertyCreator(canonical, false, properties);
584+
ccState.creators.addPropertyCreator(canonical, false, properties);
578585
}
579586

580587
/**
@@ -852,11 +859,15 @@ private void _checkImplicitlyNamedConstructors(DeserializationContext ctxt,
852859
}
853860

854861
protected void _addDeserializerFactoryMethods
855-
(DeserializationContext ctxt, BeanDescription beanDesc, VisibilityChecker<?> vchecker,
856-
AnnotationIntrospector intr, CreatorCollector creators,
857-
Map<AnnotatedWithParams,BeanPropertyDefinition[]> creatorParams)
862+
(DeserializationContext ctxt, CreatorCollectionState ccState)
858863
throws JsonMappingException
859864
{
865+
final BeanDescription beanDesc = ccState.beanDesc;
866+
final CreatorCollector creators = ccState.creators;
867+
final AnnotationIntrospector intr = ccState.annotationIntrospector();
868+
final VisibilityChecker<?> vchecker = ccState.vchecker;
869+
final Map<AnnotatedWithParams, BeanPropertyDefinition[]> creatorParams = ccState.creatorDefs;
870+
860871
List<CreatorCandidate> nonAnnotated = new LinkedList<>();
861872
int explCount = 0;
862873

@@ -2437,4 +2448,34 @@ public static Class<?> findMapFallback(JavaType type) {
24372448
return _mapFallbacks.get(type.getRawClass().getName());
24382449
}
24392450
}
2451+
2452+
/**
2453+
* Helper class to contain largish number of parameters that need to be passed
2454+
* during Creator introspection.
2455+
*
2456+
* @since 2.12
2457+
*/
2458+
protected static class CreatorCollectionState {
2459+
public final DeserializationContext context;
2460+
public final BeanDescription beanDesc;
2461+
public final VisibilityChecker<?> vchecker;
2462+
public final CreatorCollector creators;
2463+
public final Map<AnnotatedWithParams,BeanPropertyDefinition[]> creatorDefs;
2464+
2465+
public CreatorCollectionState(DeserializationContext ctxt, BeanDescription bd,
2466+
VisibilityChecker<?> vc,
2467+
CreatorCollector cc,
2468+
Map<AnnotatedWithParams,BeanPropertyDefinition[]> cdefs)
2469+
{
2470+
context = ctxt;
2471+
beanDesc = bd;
2472+
vchecker = vc;
2473+
creators = cc;
2474+
creatorDefs = cdefs;
2475+
}
2476+
2477+
public AnnotationIntrospector annotationIntrospector() {
2478+
return context.getAnnotationIntrospector();
2479+
}
2480+
}
24402481
}

0 commit comments

Comments
 (0)