Skip to content

Commit

Permalink
More work towards #4515; intermediate clean up (#4538)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored May 20, 2024
1 parent 56555b0 commit c2be01a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,47 +630,47 @@ protected void _addCreators(Map<String, POJOPropertyBuilder> props)

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

final PotentialCreator canonical;

// 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());
}

// If no Explicitly annotated creators found, look
// 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");
}
}
Expand Down Expand Up @@ -701,7 +701,7 @@ private void _removeDisabledCreators(List<PotentialCreator> ctors)
Iterator<PotentialCreator> 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();
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -795,7 +795,9 @@ private void _addCreatorsWithAnnotatedNames(PotentialCreators collector,
private void _addCreatorParams(Map<String, POJOPropertyBuilder> 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);
Expand All @@ -818,8 +820,10 @@ private void _addCreatorParams(Map<String, POJOPropertyBuilder> 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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -36,6 +42,10 @@ public PotentialCreator(AnnotatedWithParams cr,
/**********************************************************************
*/

public void assignPropertyDefs(BeanPropertyDefinition[] propertyDefs) {
this.propertyDefs = propertyDefs;
}

public PotentialCreator introspectParamNames(MapperConfig<?> config)
{
if (implicitParamNames != null) {
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@

public class PotentialCreators
{
public final List<PotentialCreator> constructors;

public final List<PotentialCreator> factories;

/**
* Property-based Creator found, if any
*/
public PotentialCreator propertiesBased;

public AnnotatedWithParams defaultCreator;

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

public PotentialCreators(List<PotentialCreator> constructors,
List<PotentialCreator> factories)
public PotentialCreators()
{
this.constructors = constructors;
this.factories = factories;
}

/*
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit c2be01a

Please sign in to comment.