Skip to content

Commit c2be01a

Browse files
authored
More work towards #4515; intermediate clean up (#4538)
1 parent 56555b0 commit c2be01a

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

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

+17-13
Original file line numberDiff line numberDiff line change
@@ -630,47 +630,47 @@ protected void _addCreators(Map<String, POJOPropertyBuilder> props)
630630

631631
// First, resolve explicit annotations for all potential Creators
632632
// (but do NOT filter out DISABLED ones yet!)
633-
List<PotentialCreator> ctors = _collectCreators(_classDef.getConstructors());
633+
List<PotentialCreator> constructors = _collectCreators(_classDef.getConstructors());
634634
List<PotentialCreator> factories = _collectCreators(_classDef.getFactoryMethods());
635635

636636
final PotentialCreator canonical;
637637

638638
// Find and mark "canonical" constructor for Records.
639639
// Needs to be done early to get implicit names populated
640640
if (_isRecordType) {
641-
canonical = JDK14Util.findCanonicalRecordConstructor(_config, _classDef, ctors);
641+
canonical = JDK14Util.findCanonicalRecordConstructor(_config, _classDef, constructors);
642642
} else {
643643
// !!! TODO: fetch Canonical for Kotlin, Scala, via AnnotationIntrospector?
644644
canonical = null;
645645
}
646646

647647
// Next: remove creators marked as explicitly disabled
648-
_removeDisabledCreators(ctors);
648+
_removeDisabledCreators(constructors);
649649
_removeDisabledCreators(factories);
650650

651-
final PotentialCreators collector = new PotentialCreators(ctors, factories);
651+
final PotentialCreators collector = new PotentialCreators();
652652
// and use annotations to find explicitly chosen Creators
653653
if (_useAnnotations) { // can't have explicit ones without Annotation introspection
654654
// Start with Constructors as they have higher precedence:
655-
_addExplicitlyAnnotatedCreators(collector, collector.constructors, props, false);
655+
_addExplicitlyAnnotatedCreators(collector, constructors, props, false);
656656
// followed by Factory methods (lower precedence)
657-
_addExplicitlyAnnotatedCreators(collector, collector.factories, props,
657+
_addExplicitlyAnnotatedCreators(collector, factories, props,
658658
collector.hasPropertiesBased());
659659
}
660660

661661
// If no Explicitly annotated creators found, look
662662
// for ones with explicitly-named ({@code @JsonProperty}) parameters
663663
if (!collector.hasPropertiesBased()) {
664664
// only discover constructor Creators?
665-
_addCreatorsWithAnnotatedNames(collector, collector.constructors);
665+
_addCreatorsWithAnnotatedNames(collector, constructors);
666666
}
667667

668668
// But if no annotation-based Creators found, find/use canonical Creator
669669
// (JDK 17 Record/Scala/Kotlin)
670670
if (!collector.hasPropertiesBased()) {
671671
// for Records:
672-
if ((canonical != null) && ctors.contains(canonical)) {
673-
ctors.remove(canonical);
672+
if ((canonical != null) && constructors.contains(canonical)) {
673+
constructors.remove(canonical);
674674
collector.addPropertiesBased(_config, canonical, "canonical");
675675
}
676676
}
@@ -701,7 +701,7 @@ private void _removeDisabledCreators(List<PotentialCreator> ctors)
701701
Iterator<PotentialCreator> it = ctors.iterator();
702702
while (it.hasNext()) {
703703
// explicitly prevented? Remove
704-
if (it.next().creatorMode == JsonCreator.Mode.DISABLED) {
704+
if (it.next().creatorMode() == JsonCreator.Mode.DISABLED) {
705705
it.remove();
706706
}
707707
}
@@ -719,14 +719,14 @@ private void _addExplicitlyAnnotatedCreators(PotentialCreators collector,
719719

720720
// If no explicit annotation, skip for now (may be discovered
721721
// at a later point)
722-
if (ctor.creatorMode == null) {
722+
if (ctor.creatorMode() == null) {
723723
continue;
724724
}
725725
it.remove();
726726

727727
Boolean propsBased = null;
728728

729-
switch (ctor.creatorMode) {
729+
switch (ctor.creatorMode()) {
730730
case DELEGATING:
731731
propsBased = false;
732732
break;
@@ -795,7 +795,9 @@ private void _addCreatorsWithAnnotatedNames(PotentialCreators collector,
795795
private void _addCreatorParams(Map<String, POJOPropertyBuilder> props,
796796
PotentialCreator ctor)
797797
{
798-
for (int i = 0, len = ctor.paramCount(); i < len; ++i) {
798+
final int paramCount = ctor.paramCount();
799+
final BeanPropertyDefinition[] propertyDefs = new BeanPropertyDefinition[paramCount];
800+
for (int i = 0; i < paramCount; ++i) {
799801
final AnnotatedParameter param = ctor.param(i);
800802
final PropertyName explName = ctor.explicitName(i);
801803
PropertyName implName = ctor.implicitName(i);
@@ -818,8 +820,10 @@ private void _addCreatorParams(Map<String, POJOPropertyBuilder> props,
818820
POJOPropertyBuilder prop = (implName == null)
819821
? _property(props, explName) : _property(props, implName);
820822
prop.addCtor(param, hasExplicit ? explName : implName, hasExplicit, true, false);
823+
propertyDefs[i] = prop;
821824
_creatorProperties.add(prop);
822825
}
826+
ctor.assignPropertyDefs(propertyDefs);
823827
}
824828

825829
/*

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ public class PotentialCreator
1515
{
1616
private static final PropertyName[] NO_NAMES = new PropertyName[0];
1717

18-
public final AnnotatedWithParams creator;
18+
private final AnnotatedWithParams creator;
1919

20-
public final JsonCreator.Mode creatorMode;
20+
private final JsonCreator.Mode creatorMode;
2121

2222
private PropertyName[] implicitParamNames;
2323

2424
private PropertyName[] explicitParamNames;
2525

26+
/**
27+
* Parameter definitions if (and only if) this represents a
28+
* Property-based Creator.
29+
*/
30+
private BeanPropertyDefinition[] propertyDefs;
31+
2632
public PotentialCreator(AnnotatedWithParams cr,
2733
JsonCreator.Mode cm)
2834
{
@@ -36,6 +42,10 @@ public PotentialCreator(AnnotatedWithParams cr,
3642
/**********************************************************************
3743
*/
3844

45+
public void assignPropertyDefs(BeanPropertyDefinition[] propertyDefs) {
46+
this.propertyDefs = propertyDefs;
47+
}
48+
3949
public PotentialCreator introspectParamNames(MapperConfig<?> config)
4050
{
4151
if (implicitParamNames != null) {
@@ -104,6 +114,14 @@ public PotentialCreator introspectParamNames(MapperConfig<?> config,
104114
/**********************************************************************
105115
*/
106116

117+
public AnnotatedWithParams creator() {
118+
return creator;
119+
}
120+
121+
public JsonCreator.Mode creatorMode() {
122+
return creatorMode;
123+
}
124+
107125
public int paramCount() {
108126
return creator.getParameterCount();
109127
}

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

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,15 @@
66

77
public class PotentialCreators
88
{
9-
public final List<PotentialCreator> constructors;
10-
11-
public final List<PotentialCreator> factories;
12-
139
/**
1410
* Property-based Creator found, if any
1511
*/
1612
public PotentialCreator propertiesBased;
1713

18-
public AnnotatedWithParams defaultCreator;
19-
2014
public final List<PotentialCreator> delegating = new ArrayList<>();
2115

22-
public PotentialCreators(List<PotentialCreator> constructors,
23-
List<PotentialCreator> factories)
16+
public PotentialCreators()
2417
{
25-
this.constructors = constructors;
26-
this.factories = factories;
2718
}
2819

2920
/*
@@ -38,7 +29,7 @@ public void addPropertiesBased(MapperConfig<?> config, PotentialCreator ctor, St
3829
if (propertiesBased != null) {
3930
throw new IllegalArgumentException(String.format(
4031
"Conflicting property-based creators: already had %s creator %s, encountered another: %s",
41-
mode, propertiesBased.creator, ctor.creator));
32+
mode, propertiesBased.creator(), ctor.creator()));
4233
}
4334
propertiesBased = ctor.introspectParamNames(config);
4435
}

src/main/java/com/fasterxml/jackson/databind/jdk14/JDK14Util.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static PotentialCreator findCanonicalRecordConstructor(MapperConfig<?> co
6262
continue;
6363
}
6464
for (int i = 0; i < argCount; ++i) {
65-
if (!ctor.creator.getRawParameterType(i).equals(recordFields[i].rawType)) {
65+
if (!ctor.creator().getRawParameterType(i).equals(recordFields[i].rawType)) {
6666
continue main_loop;
6767
}
6868
}

0 commit comments

Comments
 (0)