Support GenericArrayType in GenericTypeResolver method
Before this change GenericTypeResolver.resolveType supported TypeVariable's and ParameterizedType's only. Now it also supports GenericArrayType.
This commit is contained in:
		
							parent
							
								
									0e2904b71a
								
							
						
					
					
						commit
						b36ab83ab3
					
				|  | @ -371,8 +371,13 @@ public abstract class GenericTypeResolver { | |||
| 	 * @return the type if it resolves to a Class, or <code>Object.class</code> otherwise | ||||
| 	 */ | ||||
| 	public static Class<?> resolveType(Type genericType, Map<TypeVariable, Type> typeVariableMap) { | ||||
| 		Type rawType = getRawType(genericType, typeVariableMap); | ||||
| 		return (rawType instanceof Class ? (Class) rawType : Object.class); | ||||
| 		Type resolvedType = getRawType(genericType, typeVariableMap); | ||||
| 		if (resolvedType instanceof GenericArrayType) { | ||||
| 			Type componentType = ((GenericArrayType) resolvedType).getGenericComponentType(); | ||||
| 			Class<?> componentClass = resolveType(componentType, typeVariableMap); | ||||
| 			resolvedType = Array.newInstance(componentClass, 0).getClass(); | ||||
| 		} | ||||
| 		return (resolvedType instanceof Class ? (Class) resolvedType : Object.class); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
|  |  | |||
|  | @ -17,6 +17,8 @@ | |||
| package org.springframework.core; | ||||
| 
 | ||||
| import java.lang.reflect.Method; | ||||
| import java.lang.reflect.Type; | ||||
| import java.lang.reflect.TypeVariable; | ||||
| 
 | ||||
| import java.util.Collection; | ||||
| import java.util.HashMap; | ||||
|  | @ -128,6 +130,27 @@ public class GenericTypeResolverTests { | |||
| 		assertEquals(Object.class, resolveReturnTypeForGenericMethod(extractMagicValue, new Object[] { map })); | ||||
| 	} | ||||
| 
 | ||||
|     /** | ||||
|      * @since 3.2 | ||||
|      */ | ||||
|     @Test | ||||
|     public void testResolveType() { | ||||
|             Method intMessageMethod = findMethod(MyTypeWithMethods.class, "readIntegerInputMessage", MyInterfaceType.class); | ||||
|             MethodParameter intMessageMethodParam = new MethodParameter(intMessageMethod, 0); | ||||
|             assertEquals(MyInterfaceType.class, | ||||
|                             resolveType(intMessageMethodParam.getGenericParameterType(), new HashMap<TypeVariable, Type>())); | ||||
| 
 | ||||
|             Method intArrMessageMethod = findMethod(MyTypeWithMethods.class, "readIntegerArrayInputMessage", MyInterfaceType[].class); | ||||
|             MethodParameter intArrMessageMethodParam = new MethodParameter(intArrMessageMethod, 0); | ||||
|             assertEquals(MyInterfaceType[].class, | ||||
|                             resolveType(intArrMessageMethodParam.getGenericParameterType(), new HashMap<TypeVariable, Type>())); | ||||
| 
 | ||||
|             Method genericArrMessageMethod = findMethod(MySimpleTypeWithMethods.class, "readGenericArrayInputMessage", Object[].class); | ||||
|             MethodParameter genericArrMessageMethodParam = new MethodParameter(genericArrMessageMethod, 0); | ||||
|             Map<TypeVariable, Type> varMap = getTypeVariableMap(MySimpleTypeWithMethods.class); | ||||
|             assertEquals(Integer[].class, resolveType(genericArrMessageMethodParam.getGenericParameterType(), varMap)); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 	public interface MyInterfaceType<T> { | ||||
| 	} | ||||
|  | @ -147,7 +170,7 @@ public class GenericTypeResolverTests { | |||
| 	public class MyCollectionSuperclassType extends MySuperclassType<Collection<String>> { | ||||
| 	} | ||||
| 
 | ||||
| 	public static class MyTypeWithMethods { | ||||
| 	public static class MyTypeWithMethods<T> { | ||||
| 		public MyInterfaceType<Integer> integer() { return null; } | ||||
| 		public MySimpleInterfaceType string() { return null; } | ||||
| 		public Object object() { return null; } | ||||
|  | @ -211,6 +234,17 @@ public class GenericTypeResolverTests { | |||
| 			return null; | ||||
| 		} | ||||
| 
 | ||||
|         public void readIntegerInputMessage(MyInterfaceType<Integer> message) { | ||||
|         } | ||||
| 
 | ||||
|         public void readIntegerArrayInputMessage(MyInterfaceType<Integer>[] message) { | ||||
|         } | ||||
| 
 | ||||
|         public void readGenericArrayInputMessage(T[] message) { | ||||
|         } | ||||
| 	} | ||||
| 
 | ||||
| 	public static class MySimpleTypeWithMethods extends MyTypeWithMethods<Integer> { | ||||
| 	} | ||||
| 
 | ||||
| 	static class GenericClass<T> { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue