From 5c1058c073caf69aa6d7ecf2818c418791a36a71 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 20 May 2024 16:02:23 -0700 Subject: [PATCH] More work towards #4515; intermediate clean up --- .../introspect/POJOPropertiesCollector.java | 30 +++++++++++-------- .../databind/introspect/PotentialCreator.java | 22 ++++++++++++-- .../introspect/PotentialCreators.java | 13 ++------ .../jackson/databind/jdk14/JDK14Util.java | 2 +- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java index f3e51b5184..c11e360a33 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java @@ -630,7 +630,7 @@ protected void _addCreators(Map props) // First, resolve explicit annotations for all potential Creators // (but do NOT filter out DISABLED ones yet!) - List ctors = _collectCreators(_classDef.getConstructors()); + List constructors = _collectCreators(_classDef.getConstructors()); List factories = _collectCreators(_classDef.getFactoryMethods()); final PotentialCreator canonical; @@ -638,23 +638,23 @@ protected void _addCreators(Map props) // Find and mark "canonical" constructor for Records. // Needs to be done early to get implicit names populated if (_isRecordType) { - canonical = JDK14Util.findCanonicalRecordConstructor(_config, _classDef, ctors); + canonical = JDK14Util.findCanonicalRecordConstructor(_config, _classDef, constructors); } else { // !!! TODO: fetch Canonical for Kotlin, Scala, via AnnotationIntrospector? canonical = null; } // Next: remove creators marked as explicitly disabled - _removeDisabledCreators(ctors); + _removeDisabledCreators(constructors); _removeDisabledCreators(factories); - final PotentialCreators collector = new PotentialCreators(ctors, factories); + final PotentialCreators collector = new PotentialCreators(); // and use annotations to find explicitly chosen Creators if (_useAnnotations) { // can't have explicit ones without Annotation introspection // Start with Constructors as they have higher precedence: - _addExplicitlyAnnotatedCreators(collector, collector.constructors, props, false); + _addExplicitlyAnnotatedCreators(collector, constructors, props, false); // followed by Factory methods (lower precedence) - _addExplicitlyAnnotatedCreators(collector, collector.factories, props, + _addExplicitlyAnnotatedCreators(collector, factories, props, collector.hasPropertiesBased()); } @@ -662,15 +662,15 @@ protected void _addCreators(Map props) // for ones with explicitly-named ({@code @JsonProperty}) parameters if (!collector.hasPropertiesBased()) { // only discover constructor Creators? - _addCreatorsWithAnnotatedNames(collector, collector.constructors); + _addCreatorsWithAnnotatedNames(collector, constructors); } // But if no annotation-based Creators found, find/use canonical Creator // (JDK 17 Record/Scala/Kotlin) if (!collector.hasPropertiesBased()) { // for Records: - if ((canonical != null) && ctors.contains(canonical)) { - ctors.remove(canonical); + if ((canonical != null) && constructors.contains(canonical)) { + constructors.remove(canonical); collector.addPropertiesBased(_config, canonical, "canonical"); } } @@ -701,7 +701,7 @@ private void _removeDisabledCreators(List ctors) Iterator it = ctors.iterator(); while (it.hasNext()) { // explicitly prevented? Remove - if (it.next().creatorMode == JsonCreator.Mode.DISABLED) { + if (it.next().creatorMode() == JsonCreator.Mode.DISABLED) { it.remove(); } } @@ -719,14 +719,14 @@ private void _addExplicitlyAnnotatedCreators(PotentialCreators collector, // If no explicit annotation, skip for now (may be discovered // at a later point) - if (ctor.creatorMode == null) { + if (ctor.creatorMode() == null) { continue; } it.remove(); Boolean propsBased = null; - switch (ctor.creatorMode) { + switch (ctor.creatorMode()) { case DELEGATING: propsBased = false; break; @@ -795,7 +795,9 @@ private void _addCreatorsWithAnnotatedNames(PotentialCreators collector, private void _addCreatorParams(Map props, PotentialCreator ctor) { - for (int i = 0, len = ctor.paramCount(); i < len; ++i) { + final int paramCount = ctor.paramCount(); + final BeanPropertyDefinition[] propertyDefs = new BeanPropertyDefinition[paramCount]; + for (int i = 0; i < paramCount; ++i) { final AnnotatedParameter param = ctor.param(i); final PropertyName explName = ctor.explicitName(i); PropertyName implName = ctor.implicitName(i); @@ -818,8 +820,10 @@ private void _addCreatorParams(Map props, POJOPropertyBuilder prop = (implName == null) ? _property(props, explName) : _property(props, implName); prop.addCtor(param, hasExplicit ? explName : implName, hasExplicit, true, false); + propertyDefs[i] = prop; _creatorProperties.add(prop); } + ctor.assignPropertyDefs(propertyDefs); } /* diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreator.java b/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreator.java index 99fb019829..f46e7b7734 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreator.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreator.java @@ -15,14 +15,20 @@ public class PotentialCreator { private static final PropertyName[] NO_NAMES = new PropertyName[0]; - public final AnnotatedWithParams creator; + private final AnnotatedWithParams creator; - public final JsonCreator.Mode creatorMode; + private final JsonCreator.Mode creatorMode; private PropertyName[] implicitParamNames; private PropertyName[] explicitParamNames; + /** + * Parameter definitions if (and only if) this represents a + * Property-based Creator. + */ + private BeanPropertyDefinition[] propertyDefs; + public PotentialCreator(AnnotatedWithParams cr, JsonCreator.Mode cm) { @@ -36,6 +42,10 @@ public PotentialCreator(AnnotatedWithParams cr, /********************************************************************** */ + public void assignPropertyDefs(BeanPropertyDefinition[] propertyDefs) { + this.propertyDefs = propertyDefs; + } + public PotentialCreator introspectParamNames(MapperConfig config) { if (implicitParamNames != null) { @@ -104,6 +114,14 @@ public PotentialCreator introspectParamNames(MapperConfig config, /********************************************************************** */ + public AnnotatedWithParams creator() { + return creator; + } + + public JsonCreator.Mode creatorMode() { + return creatorMode; + } + public int paramCount() { return creator.getParameterCount(); } diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreators.java b/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreators.java index 0db4951569..5a3ad070a4 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreators.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/PotentialCreators.java @@ -6,24 +6,15 @@ public class PotentialCreators { - public final List constructors; - - public final List factories; - /** * Property-based Creator found, if any */ public PotentialCreator propertiesBased; - public AnnotatedWithParams defaultCreator; - public final List delegating = new ArrayList<>(); - public PotentialCreators(List constructors, - List factories) + public PotentialCreators() { - this.constructors = constructors; - this.factories = factories; } /* @@ -38,7 +29,7 @@ public void addPropertiesBased(MapperConfig config, PotentialCreator ctor, St if (propertiesBased != null) { throw new IllegalArgumentException(String.format( "Conflicting property-based creators: already had %s creator %s, encountered another: %s", - mode, propertiesBased.creator, ctor.creator)); + mode, propertiesBased.creator(), ctor.creator())); } propertiesBased = ctor.introspectParamNames(config); } diff --git a/src/main/java/com/fasterxml/jackson/databind/jdk14/JDK14Util.java b/src/main/java/com/fasterxml/jackson/databind/jdk14/JDK14Util.java index 470c6a98c1..0a06a926a6 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jdk14/JDK14Util.java +++ b/src/main/java/com/fasterxml/jackson/databind/jdk14/JDK14Util.java @@ -62,7 +62,7 @@ public static PotentialCreator findCanonicalRecordConstructor(MapperConfig co continue; } for (int i = 0; i < argCount; ++i) { - if (!ctor.creator.getRawParameterType(i).equals(recordFields[i].rawType)) { + if (!ctor.creator().getRawParameterType(i).equals(recordFields[i].rawType)) { continue main_loop; } }