Resolve variable bounds at outermost recursion level only

Closes gh-34504
This commit is contained in:
Juergen Hoeller 2025-02-27 22:51:41 +01:00
parent aae2952a32
commit 3bb4795d43
2 changed files with 30 additions and 1 deletions

View File

@ -159,6 +159,9 @@ public final class GenericTypeResolver {
if (genericType instanceof TypeVariable<?> typeVariable) {
ResolvableType resolvedTypeVariable = resolveVariable(
typeVariable, ResolvableType.forClass(contextClass));
if (resolvedTypeVariable == ResolvableType.NONE) {
resolvedTypeVariable = ResolvableType.forVariableBounds(typeVariable);
}
if (resolvedTypeVariable != ResolvableType.NONE) {
Class<?> resolved = resolvedTypeVariable.resolve();
if (resolved != null) {
@ -176,6 +179,9 @@ public final class GenericTypeResolver {
Type typeArgument = typeArguments[i];
if (typeArgument instanceof TypeVariable<?> typeVariable) {
ResolvableType resolvedTypeArgument = resolveVariable(typeVariable, contextType);
if (resolvedTypeArgument == ResolvableType.NONE) {
resolvedTypeArgument = ResolvableType.forVariableBounds(typeVariable);
}
if (resolvedTypeArgument != ResolvableType.NONE) {
generics[i] = resolvedTypeArgument;
}
@ -229,7 +235,7 @@ public final class GenericTypeResolver {
return resolvedType;
}
}
return ResolvableType.forVariableBounds(typeVariable);
return ResolvableType.NONE;
}
/**

View File

@ -243,6 +243,13 @@ class GenericTypeResolverTests {
assertThat(resolvedType.toString()).isEqualTo("java.util.List<E>");
}
@Test
void resolveTypeFromGenericDefaultMethod() {
Type type = method(InterfaceWithDefaultMethod.class, "get", InheritsDefaultMethod.AbstractType.class).getGenericParameterTypes()[0];
Type resolvedType = resolveType(type, InheritsDefaultMethod.class);
assertThat(resolvedType).isEqualTo(InheritsDefaultMethod.ConcreteType.class);
}
private static Method method(Class<?> target, String methodName, Class<?>... parameterTypes) {
Method method = findMethod(target, methodName, parameterTypes);
assertThat(method).describedAs(target.getName() + "#" + methodName).isNotNull();
@ -454,4 +461,20 @@ class GenericTypeResolverTests {
}
}
public interface InterfaceWithDefaultMethod<T extends InheritsDefaultMethod.AbstractType> {
default String get(T input) {
throw new UnsupportedOperationException();
}
interface AbstractType {
}
}
public static class InheritsDefaultMethod implements InterfaceWithDefaultMethod<InheritsDefaultMethod.ConcreteType> {
static class ConcreteType implements AbstractType {
}
}
}