GenericTypeResolver properly handles bound parameterized type

Issue: SPR-10819
This commit is contained in:
Juergen Hoeller 2013-08-20 19:36:57 +02:00
parent da0128f120
commit ea6525f15f
2 changed files with 19 additions and 5 deletions

View File

@ -305,7 +305,7 @@ public abstract class GenericTypeResolver {
} }
/** /**
* Extract a class instance from given Type. * Extract a Class from the given Type.
*/ */
private static Class<?> extractClass(Class<?> ownerClass, Type arg) { private static Class<?> extractClass(Class<?> ownerClass, Type arg) {
if (arg instanceof ParameterizedType) { if (arg instanceof ParameterizedType) {
@ -322,9 +322,12 @@ public abstract class GenericTypeResolver {
arg = getTypeVariableMap(ownerClass).get(tv); arg = getTypeVariableMap(ownerClass).get(tv);
if (arg == null) { if (arg == null) {
arg = extractBoundForTypeVariable(tv); arg = extractBoundForTypeVariable(tv);
if (arg instanceof ParameterizedType) {
return extractClass(ownerClass, ((ParameterizedType) arg).getRawType());
}
} }
else { else {
arg = extractClass(ownerClass, arg); return extractClass(ownerClass, arg);
} }
} }
return (arg instanceof Class ? (Class) arg : Object.class); return (arg instanceof Class ? (Class) arg : Object.class);

View File

@ -19,7 +19,6 @@ package org.springframework.core;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable; import java.lang.reflect.TypeVariable;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -27,7 +26,6 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.core.GenericTypeResolver.*; import static org.springframework.core.GenericTypeResolver.*;
import static org.springframework.util.ReflectionUtils.*; import static org.springframework.util.ReflectionUtils.*;
@ -79,7 +77,6 @@ public class GenericTypeResolverTests {
*/ */
@Test @Test
public void genericMethodReturnTypes() { public void genericMethodReturnTypes() {
Method notParameterized = findMethod(MyTypeWithMethods.class, "notParameterized", new Class[] {}); Method notParameterized = findMethod(MyTypeWithMethods.class, "notParameterized", new Class[] {});
assertEquals(String.class, resolveReturnTypeForGenericMethod(notParameterized, new Object[] {})); assertEquals(String.class, resolveReturnTypeForGenericMethod(notParameterized, new Object[] {}));
@ -156,6 +153,11 @@ public class GenericTypeResolverTests {
assertEquals(Integer[].class, resolveType(genericArrMessageMethodParam.getGenericParameterType(), varMap)); assertEquals(Integer[].class, resolveType(genericArrMessageMethodParam.getGenericParameterType(), varMap));
} }
@Test
public void testBoundParameterizedType() {
assertEquals(B.class, resolveTypeArgument(TestImpl.class, ITest.class));
}
public interface MyInterfaceType<T> { public interface MyInterfaceType<T> {
} }
@ -273,4 +275,13 @@ public class GenericTypeResolverTests {
static class GenericClass<T> { static class GenericClass<T> {
} }
class A{}
class B<T>{}
class ITest<T>{}
class TestImpl<I extends A, T extends B<I>> extends ITest<T>{
}
} }