Skip to content

Commit b522ba3

Browse files
authored
Fix #4659: remove TypeFactory.defaultInstance() (#4747)
1 parent 6314acb commit b522ba3

File tree

6 files changed

+49
-35
lines changed

6 files changed

+49
-35
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Versions: 3.x (for earlier see VERSION-2.x)
6868
(contributed by Joo-Hyuk K)
6969
#4589: Remove `MapperFeature.SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER` from 3.0;
7070
make logic default
71+
#4659: Remove use of global static `TypeFactory` singleton from 3.0
7172
#4664: Change `EnumNamingStrategy.convertEnumToExternalName()` to take `MapperConfig` argument-
7273
(contributed by Joo-Hyuk K)
7374
- Remove `MappingJsonFactory`

src/main/java/tools/jackson/databind/cfg/MapperBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,10 @@ public TypeFactory typeFactory() {
550550
}
551551

552552
/**
553-
* Overridable method for changing default {@link SubtypeResolver} instance to use
553+
* Overridable method for changing default {@link TypeFactory} instance to use
554554
*/
555555
protected TypeFactory _defaultTypeFactory() {
556-
return TypeFactory.defaultInstance();
556+
return TypeFactory.createDefaultInstance();
557557
}
558558

559559
public ClassIntrospector classIntrospector() {

src/main/java/tools/jackson/databind/ser/jdk/JDKArraySerializers.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ public static ValueSerializer<?> findStandardImpl(Class<?> cls) {
4444
return _arraySerializers.get(cls.getName());
4545
}
4646

47-
// @since 2.19
48-
@SuppressWarnings("deprecation")
4947
static JavaType simpleElementType(Class<?> elemClass) {
50-
return TypeFactory.defaultInstance().uncheckedSimpleType(elemClass);
48+
return TypeFactory.unsafeSimpleType(elemClass);
5149
}
5250

5351
/*

src/main/java/tools/jackson/databind/ser/jdk/StringArraySerializer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public class StringArraySerializer
2525
/* Note: not clean in general, but we are betting against
2626
* anyone re-defining properties of String.class here...
2727
*/
28-
@SuppressWarnings("deprecation")
29-
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(String.class);
28+
private final static JavaType VALUE_TYPE = TypeFactory.unsafeSimpleType(String.class);
3029

3130
public final static StringArraySerializer instance = new StringArraySerializer();
3231

src/main/java/tools/jackson/databind/type/TypeFactory.java

+41-27
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ public final class TypeFactory
7575

7676
private final static JavaType[] NO_TYPES = new JavaType[0];
7777

78-
/**
79-
* Globally shared singleton. Not accessed directly; non-core
80-
* code should use per-ObjectMapper instance (via configuration objects).
81-
* Core Jackson code uses {@link #defaultInstance} for accessing it.
82-
*/
83-
protected final static TypeFactory instance = new TypeFactory();
84-
8578
protected final static TypeBindings EMPTY_BINDINGS = TypeBindings.emptyBindings();
8679

8780
/*
@@ -161,7 +154,10 @@ public final class TypeFactory
161154
protected final TypeModifier[] _modifiers;
162155

163156
/**
164-
* ClassLoader used by this factory [databind#624].
157+
* ClassLoader used by this factory.
158+
*<p>
159+
* See <a href="https://github.com/FasterXML/jackson-databind/issues/624">[databind#624]</a>
160+
* for details.
165161
*/
166162
protected final ClassLoader _classLoader;
167163

@@ -171,8 +167,9 @@ public final class TypeFactory
171167
/**********************************************************************
172168
*/
173169

174-
private TypeFactory() {
175-
this(new SimpleLookupCache<Object,JavaType>(16, DEFAULT_MAX_CACHE_SIZE));
170+
// NOTE: was private in Jackson 2.x
171+
public TypeFactory() {
172+
this(new SimpleLookupCache<>(16, DEFAULT_MAX_CACHE_SIZE));
176173
}
177174

178175
protected TypeFactory(LookupCache<Object,JavaType> typeCache) {
@@ -187,6 +184,16 @@ protected TypeFactory(LookupCache<Object,JavaType> typeCache,
187184
_classLoader = classLoader;
188185
}
189186

187+
/**
188+
* Method used to construct a new default/standard {@code TypeFactory}
189+
* instance; an instance which has
190+
* no custom configuration. Used by {@code ObjectMapper} to
191+
* get the default factory when constructed.
192+
*
193+
* @since 3.0
194+
*/
195+
public static TypeFactory createDefaultInstance() { return new TypeFactory(); }
196+
190197
/**
191198
* Need to make a copy on snapshot() to avoid accidental leakage via cache.
192199
* In theory only needed if there are modifiers, but since these are lightweight
@@ -242,13 +249,6 @@ public TypeFactory withCache(LookupCache<Object,JavaType> cache) {
242249
return new TypeFactory(cache, _modifiers, _classLoader);
243250
}
244251

245-
/**
246-
* Method used to access the globally shared instance, which has
247-
* no custom configuration. Used by <code>ObjectMapper</code> to
248-
* get the default factory when constructed.
249-
*/
250-
public static TypeFactory defaultInstance() { return instance; }
251-
252252
/**
253253
* Method that will clear up any cached type definitions that may
254254
* be cached by this {@link TypeFactory} instance.
@@ -306,6 +306,27 @@ public static Class<?> rawClass(Type t) {
306306
+ClassUtil.classNameOf(t));
307307
}
308308

309+
// 12-Oct-2024, tatu: not ideal but has to do for now -- maybe can re-factor somehow
310+
// before 3.0 release?
311+
/**
312+
* Method by core databind for constructing "simple" {@link JavaType}s in cases
313+
* where there is no access to {@link TypeFactory} AND it is known that full
314+
* resolution of the type is not needed (generally when type is just a placeholder).
315+
* NOT TO BE USED by application code!
316+
*
317+
* @since 3.0
318+
*/
319+
public static JavaType unsafeSimpleType(Class<?> cls) {
320+
// 18-Oct-2015, tatu: Not sure how much problem missing super-type info is here
321+
// Copied from `_constructSimple()`:
322+
JavaType t = _findWellKnownSimple(cls);
323+
if (t == null) {
324+
// copied from `_newSimpleType()`
325+
return new SimpleType(cls, EMPTY_BINDINGS, null, null);
326+
}
327+
return t;
328+
}
329+
309330
/*
310331
/**********************************************************************
311332
/* Low-level helper methods
@@ -748,22 +769,15 @@ public JavaType constructType(TypeReference<?> typeRef)
748769
*
749770
* @param type Type of a {@link java.lang.reflect.Member} to resolve
750771
* @param contextBindings Type bindings from the context, often class in which
751-
* member declared but may be subtype of that type (to bind actual bound
752-
* type parametrers). Not used if {@code type} is of type {@code Class<?>}.
772+
* member declared but may be sub-type of that type (to bind actual bound
773+
* type parameters). Not used if {@code type} is of type {@code Class<?>}.
753774
*
754775
* @return Fully resolved type
755776
*/
756777
public JavaType resolveMemberType(Type type, TypeBindings contextBindings) {
757778
return _fromAny(null, type, contextBindings);
758779
}
759780

760-
// 20-Apr-2018, tatu: Really should get rid of this...
761-
@Deprecated // since 2.8
762-
public JavaType uncheckedSimpleType(Class<?> cls) {
763-
// 18-Oct-2015, tatu: Not sure how much problem missing super-type info is here
764-
return _constructSimple(cls, EMPTY_BINDINGS, null, null);
765-
}
766-
767781
/*
768782
/**********************************************************************
769783
/* Direct factory methods
@@ -1221,7 +1235,7 @@ protected JavaType _unknownType() {
12211235
* type is one of common, "well-known" types, instances of which are
12221236
* pre-constructed and do not need dynamic caching.
12231237
*/
1224-
protected JavaType _findWellKnownSimple(Class<?> clz) {
1238+
protected static JavaType _findWellKnownSimple(Class<?> clz) {
12251239
if (clz.isPrimitive()) {
12261240
if (clz == CLS_BOOL) return CORE_TYPE_BOOL;
12271241
if (clz == CLS_INT) return CORE_TYPE_INT;

src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public String findImplicitPropertyName(MapperConfig<?> config,
4545

4646
private final static Object SINGLETON_OBJECT = new Object();
4747

48+
private final static TypeFactory DEFAULT_TYPE_FACTORY = TypeFactory.createDefaultInstance();
49+
4850
/*
4951
/**********************************************************************
5052
/* A sample documents
@@ -561,7 +563,7 @@ public static TimeZone getUTCTimeZone() {
561563
// Separated out since "default" TypeFactory instance handling differs
562564
// between 2.x and 3.x
563565
public static TypeFactory defaultTypeFactory() {
564-
return TypeFactory.defaultInstance();
566+
return DEFAULT_TYPE_FACTORY;
565567
}
566568

567569
protected JsonParser createParserUsingReader(String input)

0 commit comments

Comments
 (0)