1
1
package com .fasterxml .jackson .databind .introspect ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .List ;
3
+ import java .util .*;
5
4
6
5
import org .junit .jupiter .api .Test ;
7
6
8
7
import com .fasterxml .jackson .annotation .JsonCreator ;
9
-
8
+ import com . fasterxml . jackson . annotation . JsonProperty ;
10
9
import com .fasterxml .jackson .databind .*;
11
10
import com .fasterxml .jackson .databind .cfg .MapperConfig ;
12
11
import com .fasterxml .jackson .databind .json .JsonMapper ;
19
18
// @since 2.18
20
19
public class DefaultCreatorResolution4620Test extends DatabindTestUtil
21
20
{
22
- static class PrimaryCreatorFindingIntrospector extends ImplicitNameIntrospector
21
+ static class POJO4620 {
22
+ String value ;
23
+
24
+ public POJO4620 (@ JsonProperty ("int" ) int i ) {
25
+ throw new RuntimeException ("Should not get called" );
26
+ }
27
+
28
+ public POJO4620 (@ JsonProperty ("str" ) String str , @ JsonProperty ("int" ) int v ) {
29
+ value = str + "/" + v ;
30
+ }
31
+
32
+ public POJO4620 (@ JsonProperty ("str" ) String str ,
33
+ @ JsonProperty ("int" ) int v ,
34
+ @ JsonProperty ("long" ) long l ) {
35
+ throw new RuntimeException ("Should not get called" );
36
+ }
37
+ }
38
+
39
+ static class PrimaryConstructorFindingIntrospector extends ImplicitNameIntrospector
23
40
{
24
41
private static final long serialVersionUID = 1L ;
25
42
26
43
private final Class <?>[] _argTypes ;
27
44
28
- private JsonCreator .Mode _mode ;
29
-
30
- private final String _factoryName ;
31
-
32
- public PrimaryCreatorFindingIntrospector (JsonCreator .Mode mode ,
33
- Class <?>... argTypes ) {
34
- _mode = mode ;
35
- _factoryName = null ;
45
+ public PrimaryConstructorFindingIntrospector (Class <?>... argTypes ) {
36
46
_argTypes = argTypes ;
37
47
}
38
48
39
- public PrimaryCreatorFindingIntrospector (JsonCreator .Mode mode ,
40
- String factoryName ) {
41
- _mode = mode ;
42
- _factoryName = factoryName ;
43
- _argTypes = new Class <?>[0 ];
44
- }
45
-
46
49
@ Override
47
50
public PotentialCreator findDefaultCreator (MapperConfig <?> config ,
48
51
AnnotatedClass valueClass ,
49
52
List <PotentialCreator > declaredConstructors ,
50
53
List <PotentialCreator > declaredFactories )
51
54
{
52
55
// Apply to all test POJOs here but nothing else
53
- if (!valueClass .getRawType ().toString ().contains ("4584 " )) {
56
+ if (!valueClass .getRawType ().toString ().contains ("4620 " )) {
54
57
return null ;
55
58
}
56
59
57
- if (_factoryName != null ) {
58
- for (PotentialCreator ctor : declaredFactories ) {
59
- if (ctor .creator ().getName ().equals (_factoryName )) {
60
- return ctor ;
61
- }
62
- }
63
- return null ;
64
- }
65
-
66
- List <PotentialCreator > combo = new ArrayList <>(declaredConstructors );
67
- combo .addAll (declaredFactories );
68
60
final int argCount = _argTypes .length ;
69
- for (PotentialCreator ctor : combo ) {
61
+ for (PotentialCreator ctor : declaredConstructors ) {
70
62
if (ctor .paramCount () == argCount ) {
71
63
int i = 0 ;
72
64
for (; i < argCount ; ++i ) {
@@ -75,7 +67,7 @@ public PotentialCreator findDefaultCreator(MapperConfig<?> config,
75
67
}
76
68
}
77
69
if (i == argCount ) {
78
- ctor .overrideMode (_mode );
70
+ ctor .overrideMode (JsonCreator . Mode . PROPERTIES );
79
71
return ctor ;
80
72
}
81
73
}
@@ -93,7 +85,11 @@ public PotentialCreator findDefaultCreator(MapperConfig<?> config,
93
85
@ Test
94
86
public void testCanonicalConstructor1ArgPropertiesCreator () throws Exception
95
87
{
96
- // TODO
88
+ // Select the "middle one"
89
+ POJO4620 result = readerWith (new PrimaryConstructorFindingIntrospector (
90
+ String .class , Integer .TYPE ))
91
+ .readValue (a2q ("{'str':'value', 'int':42}" ));
92
+ assertEquals ("value/42" , result .value );
97
93
}
98
94
99
95
/*
@@ -102,11 +98,9 @@ public void testCanonicalConstructor1ArgPropertiesCreator() throws Exception
102
98
/**********************************************************************
103
99
*/
104
100
105
- /*
106
101
private ObjectReader readerWith (AnnotationIntrospector intr ) {
107
- return mapperWith(intr).readerFor(POJO4584 .class);
102
+ return mapperWith (intr ).readerFor (POJO4620 .class );
108
103
}
109
- */
110
104
111
105
private ObjectMapper mapperWith (AnnotationIntrospector intr ) {
112
106
return JsonMapper .builder ()
0 commit comments