Merge branch '6.2.x'

This commit is contained in:
Juergen Hoeller 2025-02-27 22:54:07 +01:00
commit 03cce13937
2 changed files with 30 additions and 1 deletions

View File

@ -156,6 +156,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) {
@ -173,6 +176,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;
}
@ -226,7 +232,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 {
}
}
}