diff --git a/release-notes/VERSION b/release-notes/VERSION index 7da72be3f5..2bbee0a57d 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -68,6 +68,7 @@ Versions: 3.x (for earlier see VERSION-2.x) (contributed by Joo-Hyuk K) #4589: Remove `MapperFeature.SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER` from 3.0; make logic default +#4659: Remove use of global static `TypeFactory` singleton from 3.0 #4664: Change `EnumNamingStrategy.convertEnumToExternalName()` to take `MapperConfig` argument- (contributed by Joo-Hyuk K) - Remove `MappingJsonFactory` diff --git a/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java b/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java index ac21fd40be..86264f4b2b 100644 --- a/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java +++ b/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java @@ -550,10 +550,10 @@ public TypeFactory typeFactory() { } /** - * Overridable method for changing default {@link SubtypeResolver} instance to use + * Overridable method for changing default {@link TypeFactory} instance to use */ protected TypeFactory _defaultTypeFactory() { - return TypeFactory.defaultInstance(); + return TypeFactory.createDefaultInstance(); } public ClassIntrospector classIntrospector() { diff --git a/src/main/java/tools/jackson/databind/ser/jdk/JDKArraySerializers.java b/src/main/java/tools/jackson/databind/ser/jdk/JDKArraySerializers.java index 05c01522ba..62f30ebcdd 100644 --- a/src/main/java/tools/jackson/databind/ser/jdk/JDKArraySerializers.java +++ b/src/main/java/tools/jackson/databind/ser/jdk/JDKArraySerializers.java @@ -44,10 +44,8 @@ public static ValueSerializer findStandardImpl(Class cls) { return _arraySerializers.get(cls.getName()); } - // @since 2.19 - @SuppressWarnings("deprecation") static JavaType simpleElementType(Class elemClass) { - return TypeFactory.defaultInstance().uncheckedSimpleType(elemClass); + return TypeFactory.unsafeSimpleType(elemClass); } /* diff --git a/src/main/java/tools/jackson/databind/ser/jdk/StringArraySerializer.java b/src/main/java/tools/jackson/databind/ser/jdk/StringArraySerializer.java index e4785b0c79..13cacd3c02 100644 --- a/src/main/java/tools/jackson/databind/ser/jdk/StringArraySerializer.java +++ b/src/main/java/tools/jackson/databind/ser/jdk/StringArraySerializer.java @@ -25,8 +25,7 @@ public class StringArraySerializer /* Note: not clean in general, but we are betting against * anyone re-defining properties of String.class here... */ - @SuppressWarnings("deprecation") - private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(String.class); + private final static JavaType VALUE_TYPE = TypeFactory.unsafeSimpleType(String.class); public final static StringArraySerializer instance = new StringArraySerializer(); diff --git a/src/main/java/tools/jackson/databind/type/TypeFactory.java b/src/main/java/tools/jackson/databind/type/TypeFactory.java index 3e0ea67061..83b176eec0 100644 --- a/src/main/java/tools/jackson/databind/type/TypeFactory.java +++ b/src/main/java/tools/jackson/databind/type/TypeFactory.java @@ -75,13 +75,6 @@ public final class TypeFactory private final static JavaType[] NO_TYPES = new JavaType[0]; - /** - * Globally shared singleton. Not accessed directly; non-core - * code should use per-ObjectMapper instance (via configuration objects). - * Core Jackson code uses {@link #defaultInstance} for accessing it. - */ - protected final static TypeFactory instance = new TypeFactory(); - protected final static TypeBindings EMPTY_BINDINGS = TypeBindings.emptyBindings(); /* @@ -161,7 +154,10 @@ public final class TypeFactory protected final TypeModifier[] _modifiers; /** - * ClassLoader used by this factory [databind#624]. + * ClassLoader used by this factory. + *

+ * See [databind#624] + * for details. */ protected final ClassLoader _classLoader; @@ -171,8 +167,9 @@ public final class TypeFactory /********************************************************************** */ - private TypeFactory() { - this(new SimpleLookupCache(16, DEFAULT_MAX_CACHE_SIZE)); + // NOTE: was private in Jackson 2.x + public TypeFactory() { + this(new SimpleLookupCache<>(16, DEFAULT_MAX_CACHE_SIZE)); } protected TypeFactory(LookupCache typeCache) { @@ -187,6 +184,16 @@ protected TypeFactory(LookupCache typeCache, _classLoader = classLoader; } + /** + * Method used to construct a new default/standard {@code TypeFactory} + * instance; an instance which has + * no custom configuration. Used by {@code ObjectMapper} to + * get the default factory when constructed. + * + * @since 3.0 + */ + public static TypeFactory createDefaultInstance() { return new TypeFactory(); } + /** * Need to make a copy on snapshot() to avoid accidental leakage via cache. * In theory only needed if there are modifiers, but since these are lightweight @@ -242,13 +249,6 @@ public TypeFactory withCache(LookupCache cache) { return new TypeFactory(cache, _modifiers, _classLoader); } - /** - * Method used to access the globally shared instance, which has - * no custom configuration. Used by ObjectMapper to - * get the default factory when constructed. - */ - public static TypeFactory defaultInstance() { return instance; } - /** * Method that will clear up any cached type definitions that may * be cached by this {@link TypeFactory} instance. @@ -306,6 +306,27 @@ public static Class rawClass(Type t) { +ClassUtil.classNameOf(t)); } + // 12-Oct-2024, tatu: not ideal but has to do for now -- maybe can re-factor somehow + // before 3.0 release? + /** + * Method by core databind for constructing "simple" {@link JavaType}s in cases + * where there is no access to {@link TypeFactory} AND it is known that full + * resolution of the type is not needed (generally when type is just a placeholder). + * NOT TO BE USED by application code! + * + * @since 3.0 + */ + public static JavaType unsafeSimpleType(Class cls) { + // 18-Oct-2015, tatu: Not sure how much problem missing super-type info is here + // Copied from `_constructSimple()`: + JavaType t = _findWellKnownSimple(cls); + if (t == null) { + // copied from `_newSimpleType()` + return new SimpleType(cls, EMPTY_BINDINGS, null, null); + } + return t; + } + /* /********************************************************************** /* Low-level helper methods @@ -748,8 +769,8 @@ public JavaType constructType(TypeReference typeRef) * * @param type Type of a {@link java.lang.reflect.Member} to resolve * @param contextBindings Type bindings from the context, often class in which - * member declared but may be subtype of that type (to bind actual bound - * type parametrers). Not used if {@code type} is of type {@code Class}. + * member declared but may be sub-type of that type (to bind actual bound + * type parameters). Not used if {@code type} is of type {@code Class}. * * @return Fully resolved type */ @@ -757,13 +778,6 @@ public JavaType resolveMemberType(Type type, TypeBindings contextBindings) { return _fromAny(null, type, contextBindings); } - // 20-Apr-2018, tatu: Really should get rid of this... - @Deprecated // since 2.8 - public JavaType uncheckedSimpleType(Class cls) { - // 18-Oct-2015, tatu: Not sure how much problem missing super-type info is here - return _constructSimple(cls, EMPTY_BINDINGS, null, null); - } - /* /********************************************************************** /* Direct factory methods @@ -1221,7 +1235,7 @@ protected JavaType _unknownType() { * type is one of common, "well-known" types, instances of which are * pre-constructed and do not need dynamic caching. */ - protected JavaType _findWellKnownSimple(Class clz) { + protected static JavaType _findWellKnownSimple(Class clz) { if (clz.isPrimitive()) { if (clz == CLS_BOOL) return CORE_TYPE_BOOL; if (clz == CLS_INT) return CORE_TYPE_INT; diff --git a/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java b/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java index 75d315b1cb..16ba6c40ab 100644 --- a/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java +++ b/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java @@ -45,6 +45,8 @@ public String findImplicitPropertyName(MapperConfig config, private final static Object SINGLETON_OBJECT = new Object(); + private final static TypeFactory DEFAULT_TYPE_FACTORY = TypeFactory.createDefaultInstance(); + /* /********************************************************************** /* A sample documents @@ -561,7 +563,7 @@ public static TimeZone getUTCTimeZone() { // Separated out since "default" TypeFactory instance handling differs // between 2.x and 3.x public static TypeFactory defaultTypeFactory() { - return TypeFactory.defaultInstance(); + return DEFAULT_TYPE_FACTORY; } protected JsonParser createParserUsingReader(String input)