Skip to content

Commit 0ac0b46

Browse files
committed
More work on #1498
1 parent 151a6b6 commit 0ac0b46

4 files changed

Lines changed: 50 additions & 50 deletions

File tree

src/main/java/com/fasterxml/jackson/databind/cfg/ConstructorDetector.java

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package com.fasterxml.jackson.databind.cfg;
22

3-
import java.io.IOException;
4-
import java.util.List;
5-
6-
import com.fasterxml.jackson.databind.DatabindContext;
7-
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
8-
93
/**
104
* Configurable handler used to select aspects of selecting
115
* constructor to use as "Creator" for POJOs.
@@ -62,18 +56,6 @@ public enum SingleArgConstructor {
6256
REQUIRE_MODE;
6357
}
6458

65-
// @FunctionalInterface
66-
/**
67-
* Simple interface for optionally defined handler that can select
68-
* specific Constructor to use if more than one implicitly detected ones
69-
* (and no explicitly annotated one) found.
70-
*/
71-
public interface ConstructorSelector {
72-
public AnnotatedConstructor select(DatabindContext ctxt,
73-
List<AnnotatedConstructor> ctors)
74-
throws IOException;
75-
}
76-
7759
/*
7860
/**********************************************************************
7961
/* Global default instances to use
@@ -85,9 +67,6 @@ public AnnotatedConstructor select(DatabindContext ctxt,
8567
*<ul>
8668
* <li>Uses {@link SingleArgConstructor#HEURISTIC} for single-argument constructor case
8769
* </li>
88-
* <li>Does not specify {@link ConstructorSelector} to solve ambiguous
89-
* (multiple implicitly discovered argument-taking Constructors}
90-
* </li>
9170
* <li>Does not require explicit {@code @JsonCreator} annotations (so allows
9271
* auto-detection of Visible constructors} (except for JDK types)
9372
* </li>
@@ -128,8 +107,6 @@ public AnnotatedConstructor select(DatabindContext ctxt,
128107

129108
protected final SingleArgConstructor _singleArgMode;
130109

131-
protected final ConstructorSelector _selector;
132-
133110
/**
134111
* Whether explicit {@link com.fasterxml.jackson.annotation.JsonCreator}
135112
* is always required for detecting constructors (even if visible) other
@@ -139,7 +116,7 @@ public AnnotatedConstructor select(DatabindContext ctxt,
139116

140117
/**
141118
* Whether auto-detection of constructors of "JDK types" (those in
142-
* packages {@code java.} and {@code javax.}) is allowed or not (
119+
* packages {@code java.} and {@code javax.}) is allowed or not
143120
*/
144121
protected final boolean _allowJDKTypeCtors;
145122

@@ -150,12 +127,10 @@ public AnnotatedConstructor select(DatabindContext ctxt,
150127
*/
151128

152129
protected ConstructorDetector(SingleArgConstructor singleArgMode,
153-
ConstructorSelector selector,
154130
boolean requireCtorAnnotation,
155131
boolean allowJDKTypeCtors)
156132
{
157133
_singleArgMode = singleArgMode;
158-
_selector = selector;
159134
_requireCtorAnnotation = requireCtorAnnotation;
160135
_allowJDKTypeCtors = allowJDKTypeCtors;
161136
}
@@ -165,26 +140,21 @@ protected ConstructorDetector(SingleArgConstructor singleArgMode,
165140
* by {@code _singleArgMode}
166141
*/
167142
protected ConstructorDetector(SingleArgConstructor singleArgMode) {
168-
this(singleArgMode, null, false, false);
143+
this(singleArgMode, false, false);
169144
}
170145

171146
protected ConstructorDetector withSingleArgMode(SingleArgConstructor singleArgMode) {
172-
return new ConstructorDetector(singleArgMode, _selector,
173-
_requireCtorAnnotation, _allowJDKTypeCtors);
174-
}
175-
176-
protected ConstructorDetector withSelector(ConstructorSelector selector) {
177-
return new ConstructorDetector(_singleArgMode, selector,
147+
return new ConstructorDetector(singleArgMode,
178148
_requireCtorAnnotation, _allowJDKTypeCtors);
179149
}
180150

181151
protected ConstructorDetector withRequireAnnotation(boolean state) {
182-
return new ConstructorDetector(_singleArgMode, _selector,
152+
return new ConstructorDetector(_singleArgMode,
183153
state, _allowJDKTypeCtors);
184154
}
185155

186156
protected ConstructorDetector withAllowJDKTypes(boolean state) {
187-
return new ConstructorDetector(_singleArgMode, _selector,
157+
return new ConstructorDetector(_singleArgMode,
188158
_requireCtorAnnotation, state);
189159
}
190160

@@ -198,10 +168,6 @@ public SingleArgConstructor singleArgMode() {
198168
return _singleArgMode;
199169
}
200170

201-
public ConstructorSelector constructorSelector() {
202-
return _selector;
203-
}
204-
205171
public boolean requireCtorAnnotation() {
206172
return _requireCtorAnnotation;
207173
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.util.concurrent.atomic.AtomicReference;
99

1010
import com.fasterxml.jackson.annotation.*;
11-
import com.fasterxml.jackson.annotation.JacksonInject.Value;
12-
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
1311

1412
import com.fasterxml.jackson.core.JsonParser;
1513

@@ -441,7 +439,7 @@ protected void _addExplicitConstructorCreators(DeserializationContext ctxt,
441439
// 21-Sep-2017, tatu: First let's handle explicitly annotated ones
442440
for (AnnotatedConstructor ctor : beanDesc.getConstructors()) {
443441
JsonCreator.Mode creatorMode = intr.findCreatorAnnotation(ctxt.getConfig(), ctor);
444-
if (Mode.DISABLED == creatorMode) {
442+
if (JsonCreator.Mode.DISABLED == creatorMode) {
445443
continue;
446444
}
447445
if (creatorMode == null) {
@@ -645,7 +643,7 @@ protected void _addExplicitFactoryCreators(DeserializationContext ctxt,
645643
}
646644
continue;
647645
}
648-
if (creatorMode == Mode.DISABLED) {
646+
if (creatorMode == JsonCreator.Mode.DISABLED) {
649647
continue;
650648
}
651649

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ public Object getValue(Object pojo)
165165

166166
@Override
167167
public String toString() {
168-
return "[constructor for "+getName()+", annotations: "+_annotations+"]";
168+
final int argCount = _constructor.getParameterCount();
169+
return String.format("[constructor for %s (%d arg%s), annotations: %s",
170+
ClassUtil.nameOf(_constructor.getDeclaringClass()), argCount,
171+
(argCount == 1) ? "" : "s", _annotations);
169172
}
170173

171174
@Override

src/test/java/com/fasterxml/jackson/databind/deser/creators/ConstructorDetector1498Test.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,38 @@ public String findImplicitPropertyName(//MapperConfig<?> config,
3636
static class SingleArgNotAnnotated {
3737
protected int v;
3838

39-
// SingleArgNotAnnotated() { throw new Error("Should not be used"); }
39+
SingleArgNotAnnotated() { v = -1; }
4040

41-
public SingleArgNotAnnotated(@ImplicitName("value") @com.fasterxml.jackson.annotation.JsonSetter int value) {
41+
public SingleArgNotAnnotated(@ImplicitName("value") int value) {
4242
v = value;
4343
}
4444
}
4545

4646
static class SingleArgNoMode {
4747
protected int v;
4848

49-
// SingleArgNoMode() { throw new Error("Should not be used"); }
49+
SingleArgNoMode() { v = -1; }
5050

5151
@JsonCreator
5252
public SingleArgNoMode(@ImplicitName("value") int value) {
5353
v = value;
5454
}
5555
}
5656

57+
static class SingleArg2CtorsNotAnnotated {
58+
protected int v;
59+
60+
SingleArg2CtorsNotAnnotated() { v = -1; }
61+
62+
public SingleArg2CtorsNotAnnotated(@ImplicitName("value") int value) {
63+
v = value;
64+
}
65+
66+
public SingleArg2CtorsNotAnnotated(@ImplicitName("value") long value) {
67+
v = (int) (value * 2);
68+
}
69+
}
70+
5771
static class SingleArg1498 {
5872
final int _bar;
5973

@@ -97,6 +111,19 @@ public void test1ArgDefaultsToPropertiesIssue1498() throws Exception
97111
assertEquals(404, value._bar);
98112
}
99113

114+
// 18-Sep-2020, tatu: For now there is a problematic case of multiple eligible
115+
// choices; not cleanly solvable for 2.12
116+
public void test1ArgDefaultsToPropsMultipleCtors() throws Exception
117+
{
118+
try {
119+
MAPPER_PROPS.readValue(a2q("{'value' : 137 }"),
120+
SingleArg2CtorsNotAnnotated.class);
121+
fail("Should not pass");
122+
} catch (InvalidDefinitionException e) {
123+
verifyException(e, "Conflicting property-based creators");
124+
}
125+
}
126+
100127
/*
101128
/**********************************************************************
102129
/* Test methods, selecting from 1-arg constructors, delegating
@@ -119,7 +146,7 @@ public void test1ArgDefaultsToDelegatingNoMode() throws Exception
119146

120147
/*
121148
/**********************************************************************
122-
/* Test methods, selecting from 1-arg constructors, other
149+
/* Test methods, selecting from 1-arg constructors, heuristic (pre-2.12)
123150
/**********************************************************************
124151
*/
125152

@@ -137,9 +164,15 @@ public void test1ArgDefaultsToHeuristics() throws Exception
137164
assertEquals(13117, v2.v);
138165
}
139166

140-
// 15-Sep-2020, tatu: Tricky semantics... should this require turning
141-
// off of auto-detection?
142167
/*
168+
/**********************************************************************
169+
/* Test methods, selecting from 1-arg constructors, explicit fail
170+
/**********************************************************************
171+
*/
172+
173+
// 15-Sep-2020, tatu: Tricky semantics... should this require turning
174+
// off of auto-detection? If there is 0-arg ctor, that is to be used
175+
/*
143176
public void test1ArgFailsNoAnnotation() throws Exception
144177
{
145178
// First: fail if nothing annotated (for 1-arg case)

0 commit comments

Comments
 (0)