Skip to content

Commit

Permalink
Fix #4659: remove TypeFactory.defaultInstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 13, 2024
1 parent 6314acb commit d510083
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 35 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/tools/jackson/databind/cfg/MapperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
68 changes: 41 additions & 27 deletions src/main/java/tools/jackson/databind/type/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/*
Expand Down Expand Up @@ -161,7 +154,10 @@ public final class TypeFactory
protected final TypeModifier[] _modifiers;

/**
* ClassLoader used by this factory [databind#624].
* ClassLoader used by this factory.
*<p>
* See <a href="https://github.com/FasterXML/jackson-databind/issues/624">[databind#624]</a>
* for details.
*/
protected final ClassLoader _classLoader;

Expand All @@ -171,8 +167,9 @@ public final class TypeFactory
/**********************************************************************
*/

private TypeFactory() {
this(new SimpleLookupCache<Object,JavaType>(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<Object,JavaType> typeCache) {
Expand All @@ -187,6 +184,16 @@ protected TypeFactory(LookupCache<Object,JavaType> 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
Expand Down Expand Up @@ -242,13 +249,6 @@ public TypeFactory withCache(LookupCache<Object,JavaType> cache) {
return new TypeFactory(cache, _modifiers, _classLoader);
}

/**
* Method used to access the globally shared instance, which has
* no custom configuration. Used by <code>ObjectMapper</code> 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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -748,22 +769,15 @@ 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
*/
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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d510083

Please sign in to comment.