Improve GenericTypeResolver to resolve type variable recursively

Fix GH-24963
This commit is contained in:
Yanming Zhou
2023-03-08 12:55:49 +08:00
committed by Juergen Hoeller
parent 4b2e40153a
commit e788aeb25b
5 changed files with 45 additions and 8 deletions

View File

@@ -39,6 +39,7 @@ import static org.springframework.util.ReflectionUtils.findMethod;
* @author Sam Brannen
* @author Sebastien Deleuze
* @author Stephane Nicoll
* @author Yanming Zhou
*/
@SuppressWarnings({"unchecked", "rawtypes"})
class GenericTypeResolverTests {
@@ -203,6 +204,13 @@ class GenericTypeResolverTests {
assertThat(resolvedType).isEqualTo(reference.getType());
}
@Test
void resolveNestedTypeVariable() throws Exception {
Type resolved = resolveType(ListOfListSupplier.class.getMethod("get").getGenericReturnType(),
StringListOfListSupplier.class);
assertThat(ResolvableType.forType(resolved).getGeneric(0).getGeneric(0).resolve()).isEqualTo(String.class);
}
private static Method method(Class<?> target, String methodName, Class<?>... parameterTypes) {
Method method = findMethod(target, methodName, parameterTypes);
assertThat(method).describedAs(target.getName() + "#" + methodName).isNotNull();
@@ -382,5 +390,14 @@ class GenericTypeResolverTests {
}
}
public interface ListOfListSupplier<T> {
List<List<T>> get();
}
public interface StringListOfListSupplier extends ListOfListSupplier<String> {
}
}

View File

@@ -66,6 +66,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @author Yanming Zhou
*/
@SuppressWarnings("rawtypes")
@ExtendWith(MockitoExtension.class)
@@ -1314,6 +1315,12 @@ class ResolvableTypeTests {
assertThat(type.hasUnresolvableGenerics()).isTrue();
}
@Test
void hasUnresolvableGenericsWhenNested() throws Exception {
ResolvableType type = ResolvableType.forMethodReturnType(ListOfListSupplier.class.getMethod("get"));
assertThat(type.hasUnresolvableGenerics()).isTrue();
}
@Test
void spr11219() throws Exception {
ResolvableType type = ResolvableType.forField(BaseProvider.class.getField("stuff"), BaseProvider.class);
@@ -1617,6 +1624,12 @@ class ResolvableTypeTests {
}
public interface ListOfListSupplier<T> {
List<List<T>> get();
}
static class EnclosedInParameterizedType<T> {
static class InnerRaw {