Skip to content

Commit

Permalink
More work on #4515: start refactoring BasicDeserializerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 21, 2024
1 parent c2be01a commit 812b034
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean isNonStaticInnerClass() {

/*
/**********************************************************
/* Basic API for finding creator members
/* Basic API for finding Creators, related information
/**********************************************************
*/

Expand Down Expand Up @@ -166,6 +166,15 @@ public boolean isNonStaticInnerClass() {
*/
public abstract AnnotatedConstructor findDefaultConstructor();

/**
* Method that is replacing earlier Creator introspection access methods.
*
* @since 2.18
*
* @return Container for introspected Creator candidates, if any
*/
public abstract PotentialCreators getPotentialCreators();

/*
/**********************************************************
/* Basic API for finding property accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ public List<AnnotatedAndMetadata<AnnotatedConstructor, JsonCreator.Mode>> getCon
return result;
}

@Override
public PotentialCreators getPotentialCreators() {
return _propCollector.getPotentialCreators();
}

@Override
public Object instantiateBean(boolean fixAccess) {
AnnotatedConstructor ac = _classInfo.getDefaultConstructor();
Expand All @@ -363,7 +368,7 @@ public Object instantiateBean(boolean fixAccess) {
+ClassUtil.exceptionMessage(t), t);
}
}

/*
/**********************************************************
/* Simple accessors, extended
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class POJOPropertiesCollector

protected List<POJOPropertyBuilder> _creatorProperties;

/**
* @since 2.18
*/
protected PotentialCreators _potentialCreators;

/**
* A set of "field renamings" that have been discovered, indicating
* intended renaming of other accessors: key is the implicit original
Expand Down Expand Up @@ -218,6 +223,14 @@ public List<BeanPropertyDefinition> getProperties() {
return new ArrayList<>(props.values());
}

// @since 2.18
public PotentialCreators getPotentialCreators() {
if (!_collected) {
collectAll();
}
return _potentialCreators;
}

public Map<Object, AnnotatedMember> getInjectables() {
if (!_collected) {
collectAll();
Expand Down Expand Up @@ -626,6 +639,8 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
// Completely rewritten in 2.18
protected void _addCreators(Map<String, POJOPropertyBuilder> props)
{
final PotentialCreators creators = new PotentialCreators();
_potentialCreators = creators;
_creatorProperties = new ArrayList<>();

// First, resolve explicit annotations for all potential Creators
Expand All @@ -648,35 +663,35 @@ protected void _addCreators(Map<String, POJOPropertyBuilder> props)
_removeDisabledCreators(constructors);
_removeDisabledCreators(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, constructors, props, false);
_addExplicitlyAnnotatedCreators(creators, constructors, props, false);
// followed by Factory methods (lower precedence)
_addExplicitlyAnnotatedCreators(collector, factories, props,
collector.hasPropertiesBased());
_addExplicitlyAnnotatedCreators(creators, factories, props,
creators.hasPropertiesBased());
}

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

// But if no annotation-based Creators found, find/use canonical Creator
// (JDK 17 Record/Scala/Kotlin)
if (!collector.hasPropertiesBased()) {
if (!creators.hasPropertiesBased()) {
// for Records:
if ((canonical != null) && constructors.contains(canonical)) {
constructors.remove(canonical);
collector.addPropertiesBased(_config, canonical, "canonical");
creators.addPropertiesBased(_config, canonical, "canonical");
}
}

// And finally add logical properties:
PotentialCreator primary = collector.propertiesBased;
// And finally add logical properties for the One Properties-based
// creator selected (if any):
PotentialCreator primary = creators.propertiesBased;
if (primary != null) {
_addCreatorParams(props, primary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PotentialCreators
*/
public PotentialCreator propertiesBased;

public final List<PotentialCreator> delegating = new ArrayList<>();
private List<PotentialCreator> delegating;

public PotentialCreators()
{
Expand All @@ -36,6 +36,9 @@ public void addPropertiesBased(MapperConfig<?> config, PotentialCreator ctor, St

public void addDelegating(PotentialCreator ctor)
{
if (delegating == null) {
delegating = new ArrayList<>();
}
delegating.add(ctor);
}

Expand All @@ -50,6 +53,10 @@ public boolean hasPropertiesBased() {
}

public boolean hasPropertiesBasedOrDelegating() {
return (propertiesBased != null) || !delegating.isEmpty();
return (propertiesBased != null) || (delegating != null && !delegating.isEmpty());
}

public List<PotentialCreator> getDelegating() {
return (delegating == null) ? Collections.emptyList() : delegating;
}
}

0 comments on commit 812b034

Please sign in to comment.