GenericTypeResolver works for nested parameterized types and for generic superclasses as well (SPR-7389)
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3499 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
63962b1ff1
commit
1ba330c276
|
|
@ -107,7 +107,7 @@ public abstract class GenericTypeResolver {
|
||||||
* the given target class which is assumed to implement the generic interface
|
* the given target class which is assumed to implement the generic interface
|
||||||
* and possibly declare a concrete type for its type variable.
|
* and possibly declare a concrete type for its type variable.
|
||||||
* @param clazz the target class to check against
|
* @param clazz the target class to check against
|
||||||
* @param genericIfc the generic interface to resolve the type argument from
|
* @param genericIfc the generic interface or superclass to resolve the type argument from
|
||||||
* @return the resolved type of the argument, or <code>null</code> if not resolvable
|
* @return the resolved type of the argument, or <code>null</code> if not resolvable
|
||||||
*/
|
*/
|
||||||
public static Class<?> resolveTypeArgument(Class clazz, Class genericIfc) {
|
public static Class<?> resolveTypeArgument(Class clazz, Class genericIfc) {
|
||||||
|
|
@ -127,7 +127,7 @@ public abstract class GenericTypeResolver {
|
||||||
* target class which is assumed to implement the generic interface and possibly
|
* target class which is assumed to implement the generic interface and possibly
|
||||||
* declare concrete types for its type variables.
|
* declare concrete types for its type variables.
|
||||||
* @param clazz the target class to check against
|
* @param clazz the target class to check against
|
||||||
* @param genericIfc the generic interface to resolve the type argument from
|
* @param genericIfc the generic interface or superclass to resolve the type argument from
|
||||||
* @return the resolved type of each argument, with the array size matching the
|
* @return the resolved type of each argument, with the array size matching the
|
||||||
* number of actual type arguments, or <code>null</code> if not resolvable
|
* number of actual type arguments, or <code>null</code> if not resolvable
|
||||||
*/
|
*/
|
||||||
|
|
@ -137,8 +137,28 @@ public abstract class GenericTypeResolver {
|
||||||
|
|
||||||
private static Class[] doResolveTypeArguments(Class ownerClass, Class classToIntrospect, Class genericIfc) {
|
private static Class[] doResolveTypeArguments(Class ownerClass, Class classToIntrospect, Class genericIfc) {
|
||||||
while (classToIntrospect != null) {
|
while (classToIntrospect != null) {
|
||||||
|
if (genericIfc.isInterface()) {
|
||||||
Type[] ifcs = classToIntrospect.getGenericInterfaces();
|
Type[] ifcs = classToIntrospect.getGenericInterfaces();
|
||||||
for (Type ifc : ifcs) {
|
for (Type ifc : ifcs) {
|
||||||
|
Class[] result = doResolveTypeArguments(ownerClass, ifc, genericIfc);
|
||||||
|
if (result != null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Class[] result = doResolveTypeArguments(
|
||||||
|
ownerClass, classToIntrospect.getGenericSuperclass(), genericIfc);
|
||||||
|
if (result != null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
classToIntrospect = classToIntrospect.getSuperclass();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Class[] doResolveTypeArguments(Class ownerClass, Type ifc, Class genericIfc) {
|
||||||
if (ifc instanceof ParameterizedType) {
|
if (ifc instanceof ParameterizedType) {
|
||||||
ParameterizedType paramIfc = (ParameterizedType) ifc;
|
ParameterizedType paramIfc = (ParameterizedType) ifc;
|
||||||
Type rawType = paramIfc.getRawType();
|
Type rawType = paramIfc.getRawType();
|
||||||
|
|
@ -158,9 +178,6 @@ public abstract class GenericTypeResolver {
|
||||||
else if (genericIfc.isAssignableFrom((Class) ifc)) {
|
else if (genericIfc.isAssignableFrom((Class) ifc)) {
|
||||||
return doResolveTypeArguments(ownerClass, (Class) ifc, genericIfc);
|
return doResolveTypeArguments(ownerClass, (Class) ifc, genericIfc);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
classToIntrospect = classToIntrospect.getSuperclass();
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -168,7 +185,16 @@ public abstract class GenericTypeResolver {
|
||||||
* Extract a class instance from given Type.
|
* Extract a class instance from given Type.
|
||||||
*/
|
*/
|
||||||
private static Class extractClass(Class ownerClass, Type arg) {
|
private static Class extractClass(Class ownerClass, Type arg) {
|
||||||
if (arg instanceof TypeVariable) {
|
if (arg instanceof ParameterizedType) {
|
||||||
|
return extractClass(ownerClass, ((ParameterizedType) arg).getRawType());
|
||||||
|
}
|
||||||
|
else if (arg instanceof GenericArrayType) {
|
||||||
|
GenericArrayType gat = (GenericArrayType) arg;
|
||||||
|
Type gt = gat.getGenericComponentType();
|
||||||
|
Class<?> componentClass = extractClass(ownerClass, gt);
|
||||||
|
return Array.newInstance(componentClass, 0).getClass();
|
||||||
|
}
|
||||||
|
else if (arg instanceof TypeVariable) {
|
||||||
TypeVariable tv = (TypeVariable) arg;
|
TypeVariable tv = (TypeVariable) arg;
|
||||||
arg = getTypeVariableMap(ownerClass).get(tv);
|
arg = getTypeVariableMap(ownerClass).get(tv);
|
||||||
if (arg == null) {
|
if (arg == null) {
|
||||||
|
|
@ -178,12 +204,6 @@ public abstract class GenericTypeResolver {
|
||||||
arg = extractClass(ownerClass, arg);
|
arg = extractClass(ownerClass, arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (arg instanceof GenericArrayType) {
|
|
||||||
GenericArrayType gat = (GenericArrayType) arg;
|
|
||||||
Type gt = gat.getGenericComponentType();
|
|
||||||
Class<?> componentClass = extractClass(ownerClass, gt);
|
|
||||||
arg = Array.newInstance(componentClass, 0).getClass();
|
|
||||||
}
|
|
||||||
return (arg instanceof Class ? (Class) arg : Object.class);
|
return (arg instanceof Class ? (Class) arg : Object.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2010 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.core;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
*/
|
||||||
|
public class GenericTypeResolverTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleInterfaceType() {
|
||||||
|
assertEquals(String.class, GenericTypeResolver.resolveTypeArgument(MySimpleInterfaceType.class, MyInterfaceType.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleCollectionInterfaceType() {
|
||||||
|
assertEquals(Collection.class, GenericTypeResolver.resolveTypeArgument(MyCollectionInterfaceType.class, MyInterfaceType.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleSuperclassType() {
|
||||||
|
assertEquals(String.class, GenericTypeResolver.resolveTypeArgument(MySimpleSuperclassType.class, MySuperclassType.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleCollectionSuperclassType() {
|
||||||
|
assertEquals(Collection.class, GenericTypeResolver.resolveTypeArgument(MyCollectionSuperclassType.class, MySuperclassType.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public interface MyInterfaceType<T> {
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MySimpleInterfaceType implements MyInterfaceType<String> {
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MyCollectionInterfaceType implements MyInterfaceType<Collection<String>> {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class MySuperclassType<T> {
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MySimpleSuperclassType extends MySuperclassType<String> {
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MyCollectionSuperclassType extends MySuperclassType<Collection<String>> {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue