Skip to content

Commit bc7667d

Browse files
authored
More work on #4515: start refactoring BasicDeserializerFactory (#4539)
1 parent c2be01a commit bc7667d

File tree

6 files changed

+79
-57
lines changed

6 files changed

+79
-57
lines changed

src/main/java/com/fasterxml/jackson/databind/BeanDescription.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
/**
1515
* Basic container for information gathered by {@link ClassIntrospector} to
1616
* help in constructing serializers and deserializers.
17-
* Note that the main implementation type is
17+
* Note that the one implementation type is
1818
* {@link com.fasterxml.jackson.databind.introspect.BasicBeanDescription},
19-
* meaning that it is safe to upcast to this type.
19+
* meaning that it is safe to upcast to that type.
2020
*/
2121
public abstract class BeanDescription
2222
{
@@ -109,7 +109,7 @@ public boolean isNonStaticInnerClass() {
109109

110110
/*
111111
/**********************************************************
112-
/* Basic API for finding creator members
112+
/* Basic API for finding Creators, related information
113113
/**********************************************************
114114
*/
115115

@@ -166,6 +166,15 @@ public boolean isNonStaticInnerClass() {
166166
*/
167167
public abstract AnnotatedConstructor findDefaultConstructor();
168168

169+
/**
170+
* Method that is replacing earlier Creator introspection access methods.
171+
*
172+
* @since 2.18
173+
*
174+
* @return Container for introspected Creator candidates, if any
175+
*/
176+
public abstract PotentialCreators getPotentialCreators();
177+
169178
/*
170179
/**********************************************************
171180
/* Basic API for finding property accessors

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

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,6 @@ protected void _addExplicitFactoryCreators(DeserializationContext ctxt,
623623
continue;
624624
}
625625

626-
// zero-arg method factory methods fine, as long as explicit
627-
if (argCount == 0) {
628-
creators.setDefaultCreator(factory);
629-
continue;
630-
}
631-
632626
switch (creatorMode) {
633627
case DELEGATING:
634628
_addExplicitDelegatingCreator(ctxt, beanDesc, creators,
@@ -707,34 +701,8 @@ protected void _addImplicitFactoryCreators(DeserializationContext ctxt,
707701
NameTransformer unwrapper = intr.findUnwrappingNameTransformer(param);
708702
if (unwrapper != null) {
709703
_reportUnwrappedCreatorProperty(ctxt, beanDesc, param);
710-
/*
711-
properties[i] = constructCreatorProperty(ctxt, beanDesc, UNWRAPPED_CREATOR_PARAM_NAME, i, param, null);
712-
++implicitNameCount;
713-
*/
714704
continue;
715705
}
716-
// One more thing: implicit names are ok iff ctor has creator annotation
717-
/*
718-
if (isCreator) {
719-
if (name != null && !name.isEmpty()) {
720-
++implicitNameCount;
721-
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
722-
continue;
723-
}
724-
}
725-
*/
726-
/* 25-Sep-2014, tatu: Actually, we may end up "losing" naming due to higher-priority constructor
727-
* (see TestCreators#testConstructorCreator() test). And just to avoid running into that problem,
728-
* let's add one more work around
729-
*/
730-
/*
731-
PropertyName name2 = _findExplicitParamName(param, intr);
732-
if (name2 != null && !name2.isEmpty()) {
733-
// Hmmh. Ok, fine. So what are we to do with it... ?
734-
// For now... skip. May need to revisit this, should this become problematic
735-
continue main_loop;
736-
}
737-
*/
738706
if (nonAnnotatedParam == null) {
739707
nonAnnotatedParam = param;
740708
}
@@ -841,10 +809,6 @@ protected void _addExplicitPropertyCreator(DeserializationContext ctxt,
841809
NameTransformer unwrapper = ctxt.getAnnotationIntrospector().findUnwrappingNameTransformer(param);
842810
if (unwrapper != null) {
843811
_reportUnwrappedCreatorProperty(ctxt, beanDesc, param);
844-
/*
845-
properties[i] = constructCreatorProperty(ctxt, beanDesc, UNWRAPPED_CREATOR_PARAM_NAME, i, param, null);
846-
++explicitNameCount;
847-
*/
848812
}
849813
name = candidate.findImplicitParamName(i);
850814
_validateNamedPropertyParameter(ctxt, beanDesc, candidate, i,

src/main/java/com/fasterxml/jackson/databind/introspect/BasicBeanDescription.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ public List<AnnotatedAndMetadata<AnnotatedConstructor, JsonCreator.Mode>> getCon
340340
return result;
341341
}
342342

343+
@Override
344+
public PotentialCreators getPotentialCreators() {
345+
return _propCollector.getPotentialCreators();
346+
}
347+
343348
@Override
344349
public Object instantiateBean(boolean fixAccess) {
345350
AnnotatedConstructor ac = _classInfo.getDefaultConstructor();

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public class POJOPropertiesCollector
9191

9292
protected List<POJOPropertyBuilder> _creatorProperties;
9393

94+
/**
95+
* @since 2.18
96+
*/
97+
protected PotentialCreators _potentialCreators;
98+
9499
/**
95100
* A set of "field renamings" that have been discovered, indicating
96101
* intended renaming of other accessors: key is the implicit original
@@ -218,6 +223,14 @@ public List<BeanPropertyDefinition> getProperties() {
218223
return new ArrayList<>(props.values());
219224
}
220225

226+
// @since 2.18
227+
public PotentialCreators getPotentialCreators() {
228+
if (!_collected) {
229+
collectAll();
230+
}
231+
return _potentialCreators;
232+
}
233+
221234
public Map<Object, AnnotatedMember> getInjectables() {
222235
if (!_collected) {
223236
collectAll();
@@ -626,6 +639,8 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
626639
// Completely rewritten in 2.18
627640
protected void _addCreators(Map<String, POJOPropertyBuilder> props)
628641
{
642+
final PotentialCreators creators = new PotentialCreators();
643+
_potentialCreators = creators;
629644
_creatorProperties = new ArrayList<>();
630645

631646
// First, resolve explicit annotations for all potential Creators
@@ -648,35 +663,35 @@ protected void _addCreators(Map<String, POJOPropertyBuilder> props)
648663
_removeDisabledCreators(constructors);
649664
_removeDisabledCreators(factories);
650665

651-
final PotentialCreators collector = new PotentialCreators();
652666
// and use annotations to find explicitly chosen Creators
653667
if (_useAnnotations) { // can't have explicit ones without Annotation introspection
654668
// Start with Constructors as they have higher precedence:
655-
_addExplicitlyAnnotatedCreators(collector, constructors, props, false);
669+
_addExplicitlyAnnotatedCreators(creators, constructors, props, false);
656670
// followed by Factory methods (lower precedence)
657-
_addExplicitlyAnnotatedCreators(collector, factories, props,
658-
collector.hasPropertiesBased());
671+
_addExplicitlyAnnotatedCreators(creators, factories, props,
672+
creators.hasPropertiesBased());
659673
}
660674

661675
// If no Explicitly annotated creators found, look
662676
// for ones with explicitly-named ({@code @JsonProperty}) parameters
663-
if (!collector.hasPropertiesBased()) {
677+
if (!creators.hasPropertiesBased()) {
664678
// only discover constructor Creators?
665-
_addCreatorsWithAnnotatedNames(collector, constructors);
679+
_addCreatorsWithAnnotatedNames(creators, constructors);
666680
}
667681

668682
// But if no annotation-based Creators found, find/use canonical Creator
669683
// (JDK 17 Record/Scala/Kotlin)
670-
if (!collector.hasPropertiesBased()) {
684+
if (!creators.hasPropertiesBased()) {
671685
// for Records:
672686
if ((canonical != null) && constructors.contains(canonical)) {
673687
constructors.remove(canonical);
674-
collector.addPropertiesBased(_config, canonical, "canonical");
688+
creators.setPropertiesBased(_config, canonical, "canonical");
675689
}
676690
}
677691

678-
// And finally add logical properties:
679-
PotentialCreator primary = collector.propertiesBased;
692+
// And finally add logical properties for the One Properties-based
693+
// creator selected (if any):
694+
PotentialCreator primary = creators.propertiesBased;
680695
if (primary != null) {
681696
_addCreatorParams(props, primary);
682697
}
@@ -766,10 +781,10 @@ private void _addExplicitlyAnnotatedCreators(PotentialCreators collector,
766781
if (propsBased) {
767782
// Skipping done if we already got higher-precedence Creator
768783
if (!skipPropsBased) {
769-
collector.addPropertiesBased(_config, ctor, "explicit");
784+
collector.setPropertiesBased(_config, ctor, "explicit");
770785
}
771786
} else {
772-
collector.addDelegating(ctor);
787+
collector.addExplicitDelegating(ctor);
773788
}
774789
}
775790
}
@@ -788,7 +803,7 @@ private void _addCreatorsWithAnnotatedNames(PotentialCreators collector,
788803
}
789804
it.remove();
790805

791-
collector.addPropertiesBased(_config, ctor, "implicit");
806+
collector.setPropertiesBased(_config, ctor, "implicit");
792807
}
793808
}
794809

src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ public String implicitNameSimple(int ix) {
152152
return (pn == null) ? null : pn.getSimpleName();
153153
}
154154

155+
public BeanPropertyDefinition[] propertyDefs() {
156+
return propertyDefs;
157+
}
158+
155159
/*
156160
/**********************************************************************
157161
/* Misc other

src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreators.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public class PotentialCreators
1111
*/
1212
public PotentialCreator propertiesBased;
1313

14-
public final List<PotentialCreator> delegating = new ArrayList<>();
14+
private List<PotentialCreator> explicitDelegating;
15+
16+
private List<PotentialCreator> implicitDelegatingConstructors;
17+
private List<PotentialCreator> implicitDelegatingFactories;
1518

1619
public PotentialCreators()
1720
{
@@ -24,7 +27,7 @@ public PotentialCreators()
2427
*/
2528

2629
// desc -> "explicit", "implicit" etc
27-
public void addPropertiesBased(MapperConfig<?> config, PotentialCreator ctor, String mode)
30+
public void setPropertiesBased(MapperConfig<?> config, PotentialCreator ctor, String mode)
2831
{
2932
if (propertiesBased != null) {
3033
throw new IllegalArgumentException(String.format(
@@ -34,9 +37,19 @@ public void addPropertiesBased(MapperConfig<?> config, PotentialCreator ctor, St
3437
propertiesBased = ctor.introspectParamNames(config);
3538
}
3639

37-
public void addDelegating(PotentialCreator ctor)
40+
public void addExplicitDelegating(PotentialCreator ctor)
41+
{
42+
if (explicitDelegating == null) {
43+
explicitDelegating = new ArrayList<>();
44+
}
45+
explicitDelegating.add(ctor);
46+
}
47+
48+
public void setImplicitDelegating(List<PotentialCreator> implicitConstructors,
49+
List<PotentialCreator> implicitFactories)
3850
{
39-
delegating.add(ctor);
51+
implicitDelegatingConstructors = implicitConstructors;
52+
implicitDelegatingFactories = implicitFactories;
4053
}
4154

4255
/*
@@ -50,6 +63,18 @@ public boolean hasPropertiesBased() {
5063
}
5164

5265
public boolean hasPropertiesBasedOrDelegating() {
53-
return (propertiesBased != null) || !delegating.isEmpty();
66+
return (propertiesBased != null) || (explicitDelegating != null && !explicitDelegating.isEmpty());
67+
}
68+
69+
public List<PotentialCreator> getExplicitDelegating() {
70+
return (explicitDelegating == null) ? Collections.emptyList() : explicitDelegating;
71+
}
72+
73+
public List<PotentialCreator> getImplicitDelegatingFactories() {
74+
return implicitDelegatingFactories;
75+
}
76+
77+
public List<PotentialCreator> getImplicitDelegatingConstructors() {
78+
return implicitDelegatingConstructors;
5479
}
5580
}

0 commit comments

Comments
 (0)