Skip to content

Commit 61ef2cb

Browse files
authored
Prep work for #4659 (reduce use of TypeFactory.defaultInstance()) (#4745)
1 parent f8e2930 commit 61ef2cb

23 files changed

+93
-86
lines changed

src/test/java/com/fasterxml/jackson/databind/SerializeUsingJDKTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public void testObjectMapper() throws Exception
223223
@Test
224224
public void testTypeFactory() throws Exception
225225
{
226-
TypeFactory orig = TypeFactory.defaultInstance();
226+
TypeFactory orig = defaultTypeFactory();
227227
JavaType t = orig.constructType(JavaType.class);
228228
assertNotNull(t);
229229

src/test/java/com/fasterxml/jackson/databind/contextual/ContextualKeyTypesTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import com.fasterxml.jackson.databind.deser.ContextualKeyDeserializer;
1212
import com.fasterxml.jackson.databind.module.SimpleModule;
1313
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
14-
import com.fasterxml.jackson.databind.type.TypeFactory;
14+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
1515

1616
import static org.junit.jupiter.api.Assertions.assertEquals;
1717
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -20,7 +20,7 @@
2020
* Tests to ensure that we can do contextual key serializers and
2121
* deserializers as well as value ser/deser.
2222
*/
23-
public class ContextualKeyTypesTest
23+
public class ContextualKeyTypesTest extends DatabindTestUtil
2424
{
2525
/*
2626
/**********************************************************
@@ -99,7 +99,7 @@ public void testSimpleKeySer() throws Exception
9999
mapper.registerModule(module);
100100
Map<String,Object> input = new HashMap<String,Object>();
101101
input.put("a", Integer.valueOf(3));
102-
String json = mapper.writerFor(TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, Object.class))
102+
String json = mapper.writerFor(defaultTypeFactory().constructMapType(HashMap.class, String.class, Object.class))
103103
.writeValueAsString(input);
104104
assertEquals("{\"prefix:a\":3}", json);
105105
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static class MyDelegateMapInstantiator extends ValueInstantiator.Base
251251

252252
@Override
253253
public JavaType getDelegateType(DeserializationConfig config) {
254-
return TypeFactory.defaultInstance().constructType(Object.class);
254+
return defaultTypeFactory().constructType(Object.class);
255255
}
256256

257257
@Override

src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapWithGenericValuesDeserTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
import com.fasterxml.jackson.databind.*;
1010
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
11+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
1112
import com.fasterxml.jackson.databind.type.TypeFactory;
1213

1314
import static org.junit.jupiter.api.Assertions.assertEquals;
1415
import static org.junit.jupiter.api.Assertions.assertNotNull;
1516

1617
@SuppressWarnings("serial")
17-
public class MapWithGenericValuesDeserTest
18+
public class MapWithGenericValuesDeserTest extends DatabindTestUtil
1819
{
1920
/*
2021
/**********************************************************
@@ -154,7 +155,7 @@ public void testKeyViaCtor() throws Exception
154155
{
155156
ObjectMapper mapper = new ObjectMapper();
156157
Map<KeyTypeCtor,Integer> map = mapper.readValue("{\"a\":123}",
157-
TypeFactory.defaultInstance().constructMapType(HashMap.class, KeyTypeCtor.class, Integer.class));
158+
defaultTypeFactory().constructMapType(HashMap.class, KeyTypeCtor.class, Integer.class));
158159
assertEquals(1, map.size());
159160
Map.Entry<?,?> entry = map.entrySet().iterator().next();
160161
assertEquals(Integer.valueOf(123), entry.getValue());
@@ -168,7 +169,7 @@ public void testKeyViaFactory() throws Exception
168169
{
169170
ObjectMapper mapper = new ObjectMapper();
170171
Map<KeyTypeCtor,Integer> map = mapper.readValue("{\"a\":123}",
171-
TypeFactory.defaultInstance().constructMapType(HashMap.class, KeyTypeFactory.class, Integer.class));
172+
defaultTypeFactory().constructMapType(HashMap.class, KeyTypeFactory.class, Integer.class));
172173
assertEquals(1, map.size());
173174
Map.Entry<?,?> entry = map.entrySet().iterator().next();
174175
assertEquals(Integer.valueOf(123), entry.getValue());

src/test/java/com/fasterxml/jackson/databind/exc/BasicExceptionTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import com.fasterxml.jackson.core.*;
1111
import com.fasterxml.jackson.databind.*;
1212
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
13+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
1314
import com.fasterxml.jackson.databind.type.TypeFactory;
1415

1516
import static org.junit.jupiter.api.Assertions.*;
1617

1718
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
1819

19-
public class BasicExceptionTest
20+
public class BasicExceptionTest extends DatabindTestUtil
2021
{
2122
static class User {
2223
public String user;
@@ -32,7 +33,7 @@ static class Users {
3233
@Test
3334
public void testBadDefinition() throws Exception
3435
{
35-
JavaType t = TypeFactory.defaultInstance().constructType(String.class);
36+
JavaType t = defaultTypeFactory().constructType(String.class);
3637
JsonParser p = JSON_F.createParser("[]");
3738
InvalidDefinitionException e = new InvalidDefinitionException(p,
3839
"Testing", t);

src/test/java/com/fasterxml/jackson/databind/ext/MiscJavaXMLTypesReadWriteTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private String removeZ(String dateStr) {
102102
public void testDeserializerLoading()
103103
{
104104
CoreXMLDeserializers sers = new CoreXMLDeserializers();
105-
TypeFactory f = TypeFactory.defaultInstance();
105+
TypeFactory f = defaultTypeFactory();
106106
sers.findBeanDeserializer(f.constructType(Duration.class), null, null);
107107
sers.findBeanDeserializer(f.constructType(XMLGregorianCalendar.class), null, null);
108108
sers.findBeanDeserializer(f.constructType(QName.class), null, null);

src/test/java/com/fasterxml/jackson/databind/introspect/MethodGenericTypeResolverTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import java.util.concurrent.atomic.AtomicReference;
66

77
import com.fasterxml.jackson.annotation.JsonCreator;
8+
89
import com.fasterxml.jackson.core.type.TypeReference;
10+
911
import com.fasterxml.jackson.databind.JavaType;
1012
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
1113
import com.fasterxml.jackson.databind.type.TypeBindings;
12-
import com.fasterxml.jackson.databind.type.TypeFactory;
1314

1415
import org.junit.jupiter.api.Test;
1516

@@ -18,7 +19,7 @@
1819
public class MethodGenericTypeResolverTest extends DatabindTestUtil {
1920

2021
private static final TypeResolutionContext EMPTY_CONTEXT =
21-
new TypeResolutionContext.Empty(TypeFactory.defaultInstance());
22+
new TypeResolutionContext.Empty(defaultTypeFactory());
2223

2324
public static <T> AtomicReference<T> simple(T input) {
2425
throw new UnsupportedOperationException();

src/test/java/com/fasterxml/jackson/databind/jsontype/TestGenericListSerialization.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testSubTypesFor356() throws Exception
6464
.configure(MapperFeature.USE_STATIC_TYPING, true)
6565
.build();
6666

67-
JavaType rootType = TypeFactory.defaultInstance().constructType(new TypeReference<JSONResponse<List<Parent>>>() { });
67+
JavaType rootType = defaultTypeFactory().constructType(new TypeReference<JSONResponse<List<Parent>>>() { });
6868
byte[] json = mapper.writerFor(rootType).writeValueAsBytes(input);
6969

7070
JSONResponse<List<Parent>> out = mapper.readValue(json, 0, json.length, rootType);

src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicDeduction.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ public void testContainedInferenceOfEmptySubtype() throws Exception {
185185

186186
@Test
187187
public void testListInference() throws Exception {
188-
JavaType listOfCats = TypeFactory.defaultInstance().constructParametricType(List.class, Cat.class);
188+
JavaType listOfCats = defaultTypeFactory().constructParametricType(List.class, Cat.class);
189189
List<Cat> boxes = MAPPER.readValue(arrayOfCatsJson, listOfCats);
190190
assertTrue(boxes.get(0) instanceof LiveCat);
191191
assertTrue(boxes.get(1) instanceof DeadCat);
192192
}
193193

194194
@Test
195195
public void testMapInference() throws Exception {
196-
JavaType mapOfCats = TypeFactory.defaultInstance().constructParametricType(Map.class, String.class, Cat.class);
196+
JavaType mapOfCats = defaultTypeFactory().constructParametricType(Map.class, String.class, Cat.class);
197197
Map<String, Cat> map = MAPPER.readValue(mapOfCatsJson, mapOfCats);
198198
assertEquals(1, map.size());
199199
assertTrue(map.entrySet().iterator().next().getValue() instanceof LiveCat);
@@ -281,7 +281,7 @@ public void testDefaultImpl() throws Exception {
281281
@Test
282282
public void testSimpleSerialization() throws Exception {
283283
// Given:
284-
JavaType listOfCats = TypeFactory.defaultInstance().constructParametricType(List.class, Cat.class);
284+
JavaType listOfCats = defaultTypeFactory().constructParametricType(List.class, Cat.class);
285285
List<Cat> list = MAPPER.readValue(arrayOfCatsJson, listOfCats);
286286
Cat cat = list.get(0);
287287
// When:
@@ -293,7 +293,7 @@ public void testSimpleSerialization() throws Exception {
293293
@Test
294294
public void testListSerialization() throws Exception {
295295
// Given:
296-
JavaType listOfCats = TypeFactory.defaultInstance().constructParametricType(List.class, Cat.class);
296+
JavaType listOfCats = defaultTypeFactory().constructParametricType(List.class, Cat.class);
297297
List<Cat> list = MAPPER.readValue(arrayOfCatsJson, listOfCats);
298298
// When:
299299
String json = MAPPER.writeValueAsString(list);

src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypeNames.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testRoundTrip() throws Exception
9191
};
9292
String json = MAPPER.writeValueAsString(input);
9393
List<Animal> output = MAPPER.readValue(json,
94-
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Animal.class));
94+
defaultTypeFactory().constructCollectionType(ArrayList.class, Animal.class));
9595
assertEquals(input.length, output.size());
9696
for (int i = 0, len = input.length; i < len; ++i) {
9797
assertEquals(input[i], output.get(i), "Entry #"+i+" differs, input = '"+json+"'");

src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedDeserialization.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public void testListAsArray() throws Exception
220220
+asJSONObjectValueString(m, "@classy", Fish.class.getName())
221221
+", null\n]";
222222

223-
JavaType expType = TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Animal.class);
223+
JavaType expType = defaultTypeFactory().constructCollectionType(ArrayList.class, Animal.class);
224224
List<Animal> animals = m.readValue(JSON, expType);
225225
assertNotNull(animals);
226226
assertEquals(4, animals.size());

src/test/java/com/fasterxml/jackson/databind/jsontype/deftyping/TestDefaultForMaps.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected TypeNameIdResolver createTypeNameIdResolver(boolean forSerialization)
125125
subtypes.add(new NamedType(HashMap.class, "HMap"));
126126
ObjectMapper mapper = new ObjectMapper();
127127
return TypeNameIdResolver.construct(mapper.getDeserializationConfig(),
128-
TypeFactory.defaultInstance().constructType(Object.class), subtypes, forSerialization, !forSerialization);
128+
defaultTypeFactory().constructType(Object.class), subtypes, forSerialization, !forSerialization);
129129
}
130130

131131
@Test

src/test/java/com/fasterxml/jackson/databind/jsontype/jdk/TypedArrayDeserTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void testIntList() throws Exception
5656
ObjectMapper m = new ObjectMapper();
5757
// uses WRAPPER_OBJECT inclusion
5858
String JSON = "{\""+TypedListAsWrapper.class.getName()+"\":[4,5, 6]}";
59-
JavaType type = TypeFactory.defaultInstance().constructCollectionType(TypedListAsWrapper.class, Integer.class);
59+
JavaType type = defaultTypeFactory().constructCollectionType(TypedListAsWrapper.class, Integer.class);
6060
TypedListAsWrapper<Integer> result = m.readValue(JSON, type);
6161
assertNotNull(result);
6262
assertEquals(3, result.size());
@@ -76,7 +76,7 @@ public void testBooleanListAsProp() throws Exception
7676
ObjectMapper m = new ObjectMapper();
7777
// tries to use PROPERTY inclusion; but for ARRAYS (and scalars) will become ARRAY_WRAPPER
7878
String JSON = "[\""+TypedListAsProp.class.getName()+"\",[true, false]]";
79-
JavaType type = TypeFactory.defaultInstance().constructCollectionType(TypedListAsProp.class, Boolean.class);
79+
JavaType type = defaultTypeFactory().constructCollectionType(TypedListAsProp.class, Boolean.class);
8080
TypedListAsProp<Object> result = m.readValue(JSON, type);
8181
assertNotNull(result);
8282
assertEquals(2, result.size());
@@ -91,7 +91,7 @@ public void testLongListAsWrapper() throws Exception
9191
// uses OBJECT_ARRAY, works just fine
9292

9393
String JSON = "{\""+TypedListAsWrapper.class.getName()+"\":[1, 3]}";
94-
JavaType type = TypeFactory.defaultInstance().constructCollectionType(TypedListAsWrapper.class, Long.class);
94+
JavaType type = defaultTypeFactory().constructCollectionType(TypedListAsWrapper.class, Long.class);
9595
TypedListAsWrapper<Object> result = m.readValue(JSON, type);
9696
assertNotNull(result);
9797
assertEquals(2, result.size());

src/test/java/com/fasterxml/jackson/databind/module/TestTypeModifiers.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, T
216216
*/
217217

218218
private final ObjectMapper MAPPER_WITH_MODIFIER = JsonMapper.builder()
219-
.typeFactory(TypeFactory.defaultInstance().withModifier(new MyTypeModifier()))
219+
.typeFactory(defaultTypeFactory().withModifier(new MyTypeModifier()))
220220
.build();
221221

222222
/**

src/test/java/com/fasterxml/jackson/databind/testutil/DatabindTestUtil.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ protected static ObjectReader objectReader() {
367367

368368
public static TypeFactory newTypeFactory() {
369369
// this is a work-around; no null modifier added
370-
return TypeFactory.defaultInstance().withModifier(null);
370+
return defaultTypeFactory().withModifier(null);
371371
}
372372

373373
/*
@@ -560,6 +560,11 @@ public static TimeZone getUTCTimeZone() {
560560
return TimeZone.getTimeZone("GMT");
561561
}
562562

563+
// Separated out since "default" TypeFactory instance handling differs
564+
// between 2.x and 3.x
565+
public static TypeFactory defaultTypeFactory() {
566+
return TypeFactory.defaultInstance();
567+
}
563568

564569
protected JsonParser createParserUsingReader(String input)
565570
throws IOException

0 commit comments

Comments
 (0)