Skip to content

Commit 610e779

Browse files
committed
GenericTypeResolver properly handles bound parameterized type
Issue: SPR-10819 (cherry picked from commit ea6525f)
1 parent 8efac21 commit 610e779

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ else if (ifc != null && genericIfc.isAssignableFrom((Class) ifc)) {
305305
}
306306

307307
/**
308-
* Extract a class instance from given Type.
308+
* Extract a Class from the given Type.
309309
*/
310310
private static Class<?> extractClass(Class<?> ownerClass, Type arg) {
311311
if (arg instanceof ParameterizedType) {
@@ -322,9 +322,12 @@ else if (arg instanceof TypeVariable) {
322322
arg = getTypeVariableMap(ownerClass).get(tv);
323323
if (arg == null) {
324324
arg = extractBoundForTypeVariable(tv);
325+
if (arg instanceof ParameterizedType) {
326+
return extractClass(ownerClass, ((ParameterizedType) arg).getRawType());
327+
}
325328
}
326329
else {
327-
arg = extractClass(ownerClass, arg);
330+
return extractClass(ownerClass, arg);
328331
}
329332
}
330333
return (arg instanceof Class ? (Class) arg : Object.class);

spring-core/src/test/java/org/springframework/core/GenericTypeResolverTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919
import java.lang.reflect.Method;
2020
import java.lang.reflect.Type;
2121
import java.lang.reflect.TypeVariable;
22-
2322
import java.util.Collection;
2423
import java.util.HashMap;
2524
import java.util.Map;
2625

2726
import org.junit.Test;
2827

2928
import static org.junit.Assert.*;
30-
3129
import static org.springframework.core.GenericTypeResolver.*;
3230
import static org.springframework.util.ReflectionUtils.*;
3331

@@ -76,7 +74,6 @@ public void methodReturnTypes() {
7674
*/
7775
@Test
7876
public void genericMethodReturnTypes() {
79-
8077
Method notParameterized = findMethod(MyTypeWithMethods.class, "notParameterized", new Class[] {});
8178
assertEquals(String.class, resolveReturnTypeForGenericMethod(notParameterized, new Object[] {}));
8279

@@ -151,6 +148,11 @@ public void testResolveType() {
151148
assertEquals(Integer[].class, resolveType(genericArrMessageMethodParam.getGenericParameterType(), varMap));
152149
}
153150

151+
@Test
152+
public void testBoundParameterizedType() {
153+
assertEquals(B.class, resolveTypeArgument(TestImpl.class, ITest.class));
154+
}
155+
154156

155157
public interface MyInterfaceType<T> {
156158
}
@@ -250,4 +252,13 @@ public static class MySimpleTypeWithMethods extends MyTypeWithMethods<Integer> {
250252
static class GenericClass<T> {
251253
}
252254

255+
class A{}
256+
257+
class B<T>{}
258+
259+
class ITest<T>{}
260+
261+
class TestImpl<I extends A, T extends B<I>> extends ITest<T>{
262+
}
263+
253264
}

0 commit comments

Comments
 (0)