Skip to content

Improve static factory method generic type resolution logic #2895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ private List<AnnotatedMethod> _findPotentialFactories(TypeFactory typeFactory,
// 27-Oct-2020, tatu: SIGH. As per [databind#2894] there is widespread use of
// incorrect bindings in the wild -- not supported (no tests) but used
// nonetheless. So, for 2.11.x, put back "Bad Bindings"...
final TypeResolutionContext typeResCtxt = _typeContext;
// final TypeResolutionContext typeResCtxt = _typeContext;

// 03-Nov-2020, ckozak: Implement generic JsonCreator TypeVariable handling [databind#2895]
final TypeResolutionContext emptyTypeResCtxt = new TypeResolutionContext.Empty(typeFactory);

int factoryCount = candidates.size();
List<AnnotatedMethod> result = new ArrayList<>(factoryCount);
Expand All @@ -244,7 +247,7 @@ private List<AnnotatedMethod> _findPotentialFactories(TypeFactory typeFactory,
if (key.equals(methodKeys[i])) {
result.set(i,
constructFactoryCreator(candidates.get(i),
typeResCtxt, mixinFactory));
emptyTypeResCtxt, mixinFactory));
break;
}
}
Expand All @@ -254,8 +257,12 @@ private List<AnnotatedMethod> _findPotentialFactories(TypeFactory typeFactory,
for (int i = 0; i < factoryCount; ++i) {
AnnotatedMethod factory = result.get(i);
if (factory == null) {
Method candidate = candidates.get(i);
// Apply generic type information based on the requested type
TypeResolutionContext typeResCtxt = MethodGenericTypeResolver.narrowMethodTypeParameters(
candidate, type, typeFactory, emptyTypeResCtxt);
result.set(i,
constructFactoryCreator(candidates.get(i),
constructFactoryCreator(candidate,
typeResCtxt, null));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package com.fasterxml.jackson.databind.introspect;

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

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Objects;

/**
* Internal utility functionality to handle type resolution for method type variables based on the requested
* result type.
*/
final class MethodGenericTypeResolver {

/*
* Attempt to narrow types on a generic factory method based on the expected result (requestedType).
* If narrowing was possible, a new TypeResolutionContext is returned with the discovered TypeBindings,
* otherwise the emptyTypeResCtxt argument is returned.
*
* For example:
* Given type Wrapper<T> with
* @JsonCreator static <T> Wrapper<T> fromJson(T value)
* When a Wrapper<Duck> is requested the factory must return a Wrapper<Duck> and we can bind T to Duck
* as though the method was written with defined types:
* @JsonCreator static Wrapper<Duck> fromJson(Duck value)
*/
static TypeResolutionContext narrowMethodTypeParameters(
Method candidate,
JavaType requestedType,
TypeFactory typeFactory,
TypeResolutionContext emptyTypeResCtxt) {
TypeBindings newTypeBindings = bindMethodTypeParameters(candidate, requestedType, emptyTypeResCtxt);
return newTypeBindings == null
? emptyTypeResCtxt
: new TypeResolutionContext.Basic(typeFactory, newTypeBindings);
}

/**
* Returns {@link TypeBindings} with additional type information
* based on {@code requestedType} if possible, otherwise {@code null}.
*/
static TypeBindings bindMethodTypeParameters(
Method candidate,
JavaType requestedType,
TypeResolutionContext emptyTypeResCtxt) {
TypeVariable<Method>[] methodTypeParameters = candidate.getTypeParameters();
if (methodTypeParameters.length == 0
// If the primary type has no type parameters, there's nothing to do
|| requestedType.getBindings().isEmpty()) {
// Method has no type parameters: no need to modify the resolution context.
return null;
}
Type genericReturnType = candidate.getGenericReturnType();
if (!(genericReturnType instanceof ParameterizedType)) {
// Return value is not parameterized, it cannot be used to associate the requestedType expectations
// onto parameters.
return null;
}

ParameterizedType parameterizedGenericReturnType = (ParameterizedType) genericReturnType;
// Primary type and result type must be the same class, otherwise we would need to
// trace generic parameters to a common superclass or interface.
if (!Objects.equals(requestedType.getRawClass(), parameterizedGenericReturnType.getRawType())) {
return null;
}

// Construct TypeBindings based on the requested type, and type variables that occur in the generic return type.
// For example given requestedType: Foo<String, Int>
// and method static <T, U> Foo<T, U> func(Bar<T, U> in)
// Produces TypeBindings{T=String, U=Int}.
Type[] methodReturnTypeArguments = parameterizedGenericReturnType.getActualTypeArguments();
ArrayList<String> names = new ArrayList<>(methodTypeParameters.length);
ArrayList<JavaType> types = new ArrayList<>(methodTypeParameters.length);
for (int i = 0; i < methodReturnTypeArguments.length; i++) {
Type methodReturnTypeArgument = methodReturnTypeArguments[i];
// Note: This strictly supports only TypeVariables of the forms "T" and "? extends T",
// not complex wildcards with nested type variables
TypeVariable<?> typeVar = maybeGetTypeVariable(methodReturnTypeArgument);
if (typeVar != null) {
String typeParameterName = typeVar.getName();
if (typeParameterName == null) {
return null;
}

JavaType bindTarget = requestedType.getBindings().getBoundType(i);
if (bindTarget == null) {
return null;
}
// If the type parameter name is not present in the method type parameters we
// fall back to default type handling.
TypeVariable<?> methodTypeVariable = findByName(methodTypeParameters, typeParameterName);
if (methodTypeVariable == null) {
return null;
}
if (pessimisticallyValidateBounds(emptyTypeResCtxt, bindTarget, methodTypeVariable.getBounds())) {
// Avoid duplicate entries for the same type variable, e.g. '<T> Map<T, T> foo(Class<T> in)'
int existingIndex = names.indexOf(typeParameterName);
if (existingIndex != -1) {
JavaType existingBindTarget = types.get(existingIndex);
if (bindTarget.equals(existingBindTarget)) {
continue;
}
boolean existingIsSubtype = existingBindTarget.isTypeOrSubTypeOf(bindTarget.getRawClass());
boolean newIsSubtype = bindTarget.isTypeOrSubTypeOf(existingBindTarget.getRawClass());
if (!existingIsSubtype && !newIsSubtype) {
// No way to satisfy the requested type.
return null;
}
if (existingIsSubtype ^ newIsSubtype && newIsSubtype) {
// If the new type is more specific than the existing type, the new type replaces the old.
types.set(existingIndex, bindTarget);
}
} else {
names.add(typeParameterName);
types.add(bindTarget);
}
}
}
}
// Fall back to default handling if no specific types from the requestedType are used
if (names.isEmpty()) {
return null;
}
return TypeBindings.create(names, types);
}

/* Returns the TypeVariable if it can be extracted, otherwise null. */
private static TypeVariable<?> maybeGetTypeVariable(Type type) {
if (type instanceof TypeVariable) {
return (TypeVariable<?>) type;
}
// Extract simple type variables from wildcards matching '? extends T'
if (type instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) type;
// Exclude any form of '? super T'
if (wildcardType.getLowerBounds().length != 0) {
return null;
}
Type[] upperBounds = wildcardType.getUpperBounds();
if (upperBounds.length == 1) {
return maybeGetTypeVariable(upperBounds[0]);
}
}
return null;
}

/* Returns the TypeVariable if it can be extracted, otherwise null. */
private static ParameterizedType maybeGetParameterizedType(Type type) {
if (type instanceof ParameterizedType) {
return (ParameterizedType) type;
}
// Extract simple type variables from wildcards matching '? extends T'
if (type instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) type;
// Exclude any form of '? super T'
if (wildcardType.getLowerBounds().length != 0) {
return null;
}
Type[] upperBounds = wildcardType.getUpperBounds();
if (upperBounds.length == 1) {
return maybeGetParameterizedType(upperBounds[0]);
}
}
return null;
}

private static boolean pessimisticallyValidateBounds(
TypeResolutionContext context, JavaType boundType, Type[] upperBound) {
for (Type type : upperBound) {
if (!pessimisticallyValidateBound(context, boundType, type)) {
return false;
}
}
return true;
}

private static boolean pessimisticallyValidateBound(
TypeResolutionContext context, JavaType boundType, Type type) {
if (!boundType.isTypeOrSubTypeOf(context.resolveType(type).getRawClass())) {
return false;
}
ParameterizedType parameterized = maybeGetParameterizedType(type);
if (parameterized != null) {
Type[] typeArguments = parameterized.getActualTypeArguments();
TypeBindings bindings = boundType.getBindings();
if (bindings.size() != typeArguments.length) {
return false;
}
for (int i = 0; i < bindings.size(); i++) {
JavaType boundTypeBound = bindings.getBoundType(i);
Type typeArg = typeArguments[i];
if (!pessimisticallyValidateBound(context, boundTypeBound, typeArg)) {
return false;
}
}
}
return true;
}

private static TypeVariable<?> findByName(TypeVariable<?>[] typeVariables, String name) {
if (typeVariables == null || name == null) {
return null;
}
for (TypeVariable<?> typeVariable : typeVariables) {
if (name.equals(typeVariable.getName())) {
return typeVariable;
}
}
return null;
}

private MethodGenericTypeResolver() {
// Utility class
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,18 @@ public static TypeBindings create(Class<?> erasedType, JavaType typeArg1, JavaTy
return new TypeBindings(new String[] { vars[0].getName(), vars[1].getName() },
new JavaType[] { typeArg1, typeArg2 }, null);
}


/**
* Factory method for constructing bindings given names and associated types.
*/
public static TypeBindings create(List<String> names, List<JavaType> types)
{
if (names == null || names.isEmpty() || types == null || types.isEmpty()) {
return EMPTY;
}
return new TypeBindings(names.toArray(NO_STRINGS), types.toArray(NO_TYPES), null);
}

/**
* Alternate factory method that may be called if it is possible that type
* does or does not require type parameters; this is mostly useful for
Expand Down
Loading