Browse Source

Revise use of ResolvableType in MethodParameter

Includes consistent use of getContainingClass()
pull/23401/head
Juergen Hoeller 6 years ago
parent
commit
b67dbe66ef
  1. 15
      spring-core/src/main/java/org/springframework/core/MethodParameter.java
  2. 13
      spring-core/src/main/java/org/springframework/core/ResolvableType.java

15
spring-core/src/main/java/org/springframework/core/MethodParameter.java

@ -160,9 +160,7 @@ public class MethodParameter {
* @param containingClass the containing class * @param containingClass the containing class
* @since 5.2 * @since 5.2
*/ */
MethodParameter(Executable executable, int parameterIndex, MethodParameter(Executable executable, int parameterIndex, @Nullable Class<?> containingClass) {
@Nullable Class<?> containingClass) {
Assert.notNull(executable, "Executable must not be null"); Assert.notNull(executable, "Executable must not be null");
this.executable = executable; this.executable = executable;
this.parameterIndex = validateIndex(executable, parameterIndex); this.parameterIndex = validateIndex(executable, parameterIndex);
@ -488,9 +486,7 @@ public class MethodParameter {
if (paramType != null) { if (paramType != null) {
return paramType; return paramType;
} }
if (this.containingClass != null) { paramType = ResolvableType.forMethodParameter(this, null, 1).resolve();
paramType = ResolvableType.forMethodParameter(this, null, 1, null).resolve();
}
if (paramType == null) { if (paramType == null) {
paramType = computeParameterType(); paramType = computeParameterType();
} }
@ -760,7 +756,7 @@ public class MethodParameter {
return false; return false;
} }
MethodParameter otherParam = (MethodParameter) other; MethodParameter otherParam = (MethodParameter) other;
return (this.containingClass == otherParam.containingClass && return (getContainingClass() == otherParam.getContainingClass() &&
ObjectUtils.nullSafeEquals(this.typeIndexesPerLevel, otherParam.typeIndexesPerLevel) && ObjectUtils.nullSafeEquals(this.typeIndexesPerLevel, otherParam.typeIndexesPerLevel) &&
this.nestingLevel == otherParam.nestingLevel && this.nestingLevel == otherParam.nestingLevel &&
this.parameterIndex == otherParam.parameterIndex && this.parameterIndex == otherParam.parameterIndex &&
@ -925,11 +921,10 @@ public class MethodParameter {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
if (function != null && function.isSuspend()) { if (function != null && function.isSuspend()) {
Type paramType = ReflectJvmMapping.getJavaType(function.getReturnType()); Type paramType = ReflectJvmMapping.getJavaType(function.getReturnType());
Class<?> paramClass = ResolvableType.forType(paramType).resolve(); return ResolvableType.forType(paramType).resolve(method.getReturnType());
Assert.notNull(paramClass, "Type " + paramType + "can't be resolved to a class");
return paramClass;
} }
return method.getReturnType(); return method.getReturnType();
} }
} }
} }

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

@ -1300,10 +1300,7 @@ public class ResolvableType implements Serializable {
*/ */
public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable Type targetType) { public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable Type targetType) {
Assert.notNull(methodParameter, "MethodParameter must not be null"); Assert.notNull(methodParameter, "MethodParameter must not be null");
int nestingLevel = methodParameter.getNestingLevel(); return forMethodParameter(methodParameter, targetType, methodParameter.getNestingLevel());
Map<Integer, Integer> typeIndexesPerLevel = methodParameter.typeIndexesPerLevel;
return forMethodParameter(methodParameter, targetType, nestingLevel,
typeIndexesPerLevel);
} }
/** /**
@ -1313,16 +1310,16 @@ public class ResolvableType implements Serializable {
* @param methodParameter the source method parameter (must not be {@code null}) * @param methodParameter the source method parameter (must not be {@code null})
* @param targetType the type to resolve (a part of the method parameter's type) * @param targetType the type to resolve (a part of the method parameter's type)
* @param nestingLevel the nesting level to use * @param nestingLevel the nesting level to use
* @param typeIndexesPerLevel the type indexes per nesting level
* @return a {@link ResolvableType} for the specified method parameter * @return a {@link ResolvableType} for the specified method parameter
* @since 5.2 * @since 5.2
* @see #forMethodParameter(Method, int) * @see #forMethodParameter(Method, int)
*/ */
static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable Type targetType, static ResolvableType forMethodParameter(
int nestingLevel, @Nullable Map<Integer, Integer> typeIndexesPerLevel) { MethodParameter methodParameter, @Nullable Type targetType, int nestingLevel) {
ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass()); ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass());
return forType(targetType, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()). return forType(targetType, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()).
getNested(nestingLevel, typeIndexesPerLevel); getNested(nestingLevel, methodParameter.typeIndexesPerLevel);
} }
/** /**

Loading…
Cancel
Save