Skip to content

Commit c909789

Browse files
committed
Avoid mismatch between cached top-level versus nested parameter type
Issue: SPR-13755
1 parent f5e681e commit c909789

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,28 +51,28 @@ public abstract class GenericTypeResolver {
5151

5252
/**
5353
* Determine the target type for the given parameter specification.
54-
* @param methodParam the method parameter specification
54+
* @param methodParameter the method parameter specification
5555
* @return the corresponding generic parameter type
5656
* @deprecated as of Spring 4.0, use {@link MethodParameter#getGenericParameterType()}
5757
*/
5858
@Deprecated
59-
public static Type getTargetType(MethodParameter methodParam) {
60-
Assert.notNull(methodParam, "MethodParameter must not be null");
61-
return methodParam.getGenericParameterType();
59+
public static Type getTargetType(MethodParameter methodParameter) {
60+
Assert.notNull(methodParameter, "MethodParameter must not be null");
61+
return methodParameter.getGenericParameterType();
6262
}
6363

6464
/**
6565
* Determine the target type for the given generic parameter type.
66-
* @param methodParam the method parameter specification
67-
* @param clazz the class to resolve type variables against
66+
* @param methodParameter the method parameter specification
67+
* @param implementationClass the class to resolve type variables against
6868
* @return the corresponding generic parameter or return type
6969
*/
70-
public static Class<?> resolveParameterType(MethodParameter methodParam, Class<?> clazz) {
71-
Assert.notNull(methodParam, "MethodParameter must not be null");
72-
Assert.notNull(clazz, "Class must not be null");
73-
methodParam.setContainingClass(clazz);
74-
methodParam.setParameterType(ResolvableType.forMethodParameter(methodParam).resolve());
75-
return methodParam.getParameterType();
70+
public static Class<?> resolveParameterType(MethodParameter methodParameter, Class<?> implementationClass) {
71+
Assert.notNull(methodParameter, "MethodParameter must not be null");
72+
Assert.notNull(implementationClass, "Class must not be null");
73+
methodParameter.setContainingClass(implementationClass);
74+
ResolvableType.resolveMethodParameter(methodParameter);
75+
return methodParameter.getParameterType();
7676
}
7777

7878
/**

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,11 @@ public static ResolvableType forMethodParameter(MethodParameter methodParameter)
12191219
*/
12201220
public static ResolvableType forMethodParameter(MethodParameter methodParameter, ResolvableType implementationType) {
12211221
Assert.notNull(methodParameter, "MethodParameter must not be null");
1222-
implementationType = (implementationType == null ? forType(methodParameter.getContainingClass()) : implementationType);
1222+
implementationType = (implementationType != null ? implementationType :
1223+
forType(methodParameter.getContainingClass()));
12231224
ResolvableType owner = implementationType.as(methodParameter.getDeclaringClass());
1224-
return forType(null, new MethodParameterTypeProvider(methodParameter),
1225-
owner.asVariableResolver()).getNested(methodParameter.getNestingLevel(),
1226-
methodParameter.typeIndexesPerLevel);
1225+
return forType(null, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()).
1226+
getNested(methodParameter.getNestingLevel(), methodParameter.typeIndexesPerLevel);
12271227
}
12281228

12291229
/**
@@ -1241,13 +1241,26 @@ public static ResolvableType forMethodParameter(MethodParameter methodParameter,
12411241
getNested(methodParameter.getNestingLevel(), methodParameter.typeIndexesPerLevel);
12421242
}
12431243

1244+
/**
1245+
* Resolve the top-level parameter type of the given {@code MethodParameter}.
1246+
* @param methodParameter the method parameter to resolve
1247+
* @since 4.1.9
1248+
* @see MethodParameter#setParameterType
1249+
*/
1250+
static void resolveMethodParameter(MethodParameter methodParameter) {
1251+
Assert.notNull(methodParameter, "MethodParameter must not be null");
1252+
ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass());
1253+
methodParameter.setParameterType(
1254+
forType(null, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()).resolve());
1255+
}
1256+
12441257
/**
12451258
* Return a {@link ResolvableType} as a array of the specified {@code componentType}.
12461259
* @param componentType the component type
12471260
* @return a {@link ResolvableType} as an array of the specified component type
12481261
*/
12491262
public static ResolvableType forArrayComponent(ResolvableType componentType) {
1250-
Assert.notNull(componentType, "componentType must not be null");
1263+
Assert.notNull(componentType, "Component type must not be null");
12511264
Class<?> arrayClass = Array.newInstance(componentType.resolve(), 0).getClass();
12521265
return new ResolvableType(arrayClass, null, null, componentType);
12531266
}

0 commit comments

Comments
 (0)