Skip to content

Commit

Permalink
Prep work for #4659 (reduce use of TypeFactory.defaultInstance()) (#4745
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cowtowncoder authored Oct 11, 2024
1 parent f8e2930 commit 61ef2cb
Show file tree
Hide file tree
Showing 23 changed files with 93 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void testObjectMapper() throws Exception
@Test
public void testTypeFactory() throws Exception
{
TypeFactory orig = TypeFactory.defaultInstance();
TypeFactory orig = defaultTypeFactory();
JavaType t = orig.constructType(JavaType.class);
assertNotNull(t);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fasterxml.jackson.databind.deser.ContextualKeyDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -20,7 +20,7 @@
* Tests to ensure that we can do contextual key serializers and
* deserializers as well as value ser/deser.
*/
public class ContextualKeyTypesTest
public class ContextualKeyTypesTest extends DatabindTestUtil
{
/*
/**********************************************************
Expand Down Expand Up @@ -99,7 +99,7 @@ public void testSimpleKeySer() throws Exception
mapper.registerModule(module);
Map<String,Object> input = new HashMap<String,Object>();
input.put("a", Integer.valueOf(3));
String json = mapper.writerFor(TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, Object.class))
String json = mapper.writerFor(defaultTypeFactory().constructMapType(HashMap.class, String.class, Object.class))
.writeValueAsString(input);
assertEquals("{\"prefix:a\":3}", json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ static class MyDelegateMapInstantiator extends ValueInstantiator.Base

@Override
public JavaType getDelegateType(DeserializationConfig config) {
return TypeFactory.defaultInstance().constructType(Object.class);
return defaultTypeFactory().constructType(Object.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.type.TypeFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SuppressWarnings("serial")
public class MapWithGenericValuesDeserTest
public class MapWithGenericValuesDeserTest extends DatabindTestUtil
{
/*
/**********************************************************
Expand Down Expand Up @@ -154,7 +155,7 @@ public void testKeyViaCtor() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Map<KeyTypeCtor,Integer> map = mapper.readValue("{\"a\":123}",
TypeFactory.defaultInstance().constructMapType(HashMap.class, KeyTypeCtor.class, Integer.class));
defaultTypeFactory().constructMapType(HashMap.class, KeyTypeCtor.class, Integer.class));
assertEquals(1, map.size());
Map.Entry<?,?> entry = map.entrySet().iterator().next();
assertEquals(Integer.valueOf(123), entry.getValue());
Expand All @@ -168,7 +169,7 @@ public void testKeyViaFactory() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Map<KeyTypeCtor,Integer> map = mapper.readValue("{\"a\":123}",
TypeFactory.defaultInstance().constructMapType(HashMap.class, KeyTypeFactory.class, Integer.class));
defaultTypeFactory().constructMapType(HashMap.class, KeyTypeFactory.class, Integer.class));
assertEquals(1, map.size());
Map.Entry<?,?> entry = map.entrySet().iterator().next();
assertEquals(Integer.valueOf(123), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.type.TypeFactory;

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

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

public class BasicExceptionTest
public class BasicExceptionTest extends DatabindTestUtil
{
static class User {
public String user;
Expand All @@ -32,7 +33,7 @@ static class Users {
@Test
public void testBadDefinition() throws Exception
{
JavaType t = TypeFactory.defaultInstance().constructType(String.class);
JavaType t = defaultTypeFactory().constructType(String.class);
JsonParser p = JSON_F.createParser("[]");
InvalidDefinitionException e = new InvalidDefinitionException(p,
"Testing", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private String removeZ(String dateStr) {
public void testDeserializerLoading()
{
CoreXMLDeserializers sers = new CoreXMLDeserializers();
TypeFactory f = TypeFactory.defaultInstance();
TypeFactory f = defaultTypeFactory();
sers.findBeanDeserializer(f.constructType(Duration.class), null, null);
sers.findBeanDeserializer(f.constructType(XMLGregorianCalendar.class), null, null);
sers.findBeanDeserializer(f.constructType(QName.class), null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.annotation.JsonCreator;

import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.type.TypeBindings;
import com.fasterxml.jackson.databind.type.TypeFactory;

import org.junit.jupiter.api.Test;

Expand All @@ -18,7 +19,7 @@
public class MethodGenericTypeResolverTest extends DatabindTestUtil {

private static final TypeResolutionContext EMPTY_CONTEXT =
new TypeResolutionContext.Empty(TypeFactory.defaultInstance());
new TypeResolutionContext.Empty(defaultTypeFactory());

public static <T> AtomicReference<T> simple(T input) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testSubTypesFor356() throws Exception
.configure(MapperFeature.USE_STATIC_TYPING, true)
.build();

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

JSONResponse<List<Parent>> out = mapper.readValue(json, 0, json.length, rootType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ public void testContainedInferenceOfEmptySubtype() throws Exception {

@Test
public void testListInference() throws Exception {
JavaType listOfCats = TypeFactory.defaultInstance().constructParametricType(List.class, Cat.class);
JavaType listOfCats = defaultTypeFactory().constructParametricType(List.class, Cat.class);
List<Cat> boxes = MAPPER.readValue(arrayOfCatsJson, listOfCats);
assertTrue(boxes.get(0) instanceof LiveCat);
assertTrue(boxes.get(1) instanceof DeadCat);
}

@Test
public void testMapInference() throws Exception {
JavaType mapOfCats = TypeFactory.defaultInstance().constructParametricType(Map.class, String.class, Cat.class);
JavaType mapOfCats = defaultTypeFactory().constructParametricType(Map.class, String.class, Cat.class);
Map<String, Cat> map = MAPPER.readValue(mapOfCatsJson, mapOfCats);
assertEquals(1, map.size());
assertTrue(map.entrySet().iterator().next().getValue() instanceof LiveCat);
Expand Down Expand Up @@ -281,7 +281,7 @@ public void testDefaultImpl() throws Exception {
@Test
public void testSimpleSerialization() throws Exception {
// Given:
JavaType listOfCats = TypeFactory.defaultInstance().constructParametricType(List.class, Cat.class);
JavaType listOfCats = defaultTypeFactory().constructParametricType(List.class, Cat.class);
List<Cat> list = MAPPER.readValue(arrayOfCatsJson, listOfCats);
Cat cat = list.get(0);
// When:
Expand All @@ -293,7 +293,7 @@ public void testSimpleSerialization() throws Exception {
@Test
public void testListSerialization() throws Exception {
// Given:
JavaType listOfCats = TypeFactory.defaultInstance().constructParametricType(List.class, Cat.class);
JavaType listOfCats = defaultTypeFactory().constructParametricType(List.class, Cat.class);
List<Cat> list = MAPPER.readValue(arrayOfCatsJson, listOfCats);
// When:
String json = MAPPER.writeValueAsString(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testRoundTrip() throws Exception
};
String json = MAPPER.writeValueAsString(input);
List<Animal> output = MAPPER.readValue(json,
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Animal.class));
defaultTypeFactory().constructCollectionType(ArrayList.class, Animal.class));
assertEquals(input.length, output.size());
for (int i = 0, len = input.length; i < len; ++i) {
assertEquals(input[i], output.get(i), "Entry #"+i+" differs, input = '"+json+"'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void testListAsArray() throws Exception
+asJSONObjectValueString(m, "@classy", Fish.class.getName())
+", null\n]";

JavaType expType = TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Animal.class);
JavaType expType = defaultTypeFactory().constructCollectionType(ArrayList.class, Animal.class);
List<Animal> animals = m.readValue(JSON, expType);
assertNotNull(animals);
assertEquals(4, animals.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected TypeNameIdResolver createTypeNameIdResolver(boolean forSerialization)
subtypes.add(new NamedType(HashMap.class, "HMap"));
ObjectMapper mapper = new ObjectMapper();
return TypeNameIdResolver.construct(mapper.getDeserializationConfig(),
TypeFactory.defaultInstance().constructType(Object.class), subtypes, forSerialization, !forSerialization);
defaultTypeFactory().constructType(Object.class), subtypes, forSerialization, !forSerialization);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void testIntList() throws Exception
ObjectMapper m = new ObjectMapper();
// uses WRAPPER_OBJECT inclusion
String JSON = "{\""+TypedListAsWrapper.class.getName()+"\":[4,5, 6]}";
JavaType type = TypeFactory.defaultInstance().constructCollectionType(TypedListAsWrapper.class, Integer.class);
JavaType type = defaultTypeFactory().constructCollectionType(TypedListAsWrapper.class, Integer.class);
TypedListAsWrapper<Integer> result = m.readValue(JSON, type);
assertNotNull(result);
assertEquals(3, result.size());
Expand All @@ -76,7 +76,7 @@ public void testBooleanListAsProp() throws Exception
ObjectMapper m = new ObjectMapper();
// tries to use PROPERTY inclusion; but for ARRAYS (and scalars) will become ARRAY_WRAPPER
String JSON = "[\""+TypedListAsProp.class.getName()+"\",[true, false]]";
JavaType type = TypeFactory.defaultInstance().constructCollectionType(TypedListAsProp.class, Boolean.class);
JavaType type = defaultTypeFactory().constructCollectionType(TypedListAsProp.class, Boolean.class);
TypedListAsProp<Object> result = m.readValue(JSON, type);
assertNotNull(result);
assertEquals(2, result.size());
Expand All @@ -91,7 +91,7 @@ public void testLongListAsWrapper() throws Exception
// uses OBJECT_ARRAY, works just fine

String JSON = "{\""+TypedListAsWrapper.class.getName()+"\":[1, 3]}";
JavaType type = TypeFactory.defaultInstance().constructCollectionType(TypedListAsWrapper.class, Long.class);
JavaType type = defaultTypeFactory().constructCollectionType(TypedListAsWrapper.class, Long.class);
TypedListAsWrapper<Object> result = m.readValue(JSON, type);
assertNotNull(result);
assertEquals(2, result.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, T
*/

private final ObjectMapper MAPPER_WITH_MODIFIER = JsonMapper.builder()
.typeFactory(TypeFactory.defaultInstance().withModifier(new MyTypeModifier()))
.typeFactory(defaultTypeFactory().withModifier(new MyTypeModifier()))
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ protected static ObjectReader objectReader() {

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

/*
Expand Down Expand Up @@ -560,6 +560,11 @@ public static TimeZone getUTCTimeZone() {
return TimeZone.getTimeZone("GMT");
}

// Separated out since "default" TypeFactory instance handling differs
// between 2.x and 3.x
public static TypeFactory defaultTypeFactory() {
return TypeFactory.defaultInstance();
}

protected JsonParser createParserUsingReader(String input)
throws IOException
Expand Down
Loading

0 comments on commit 61ef2cb

Please sign in to comment.