From c38cb43527bdfe3a523413e2112f1793244d7d11 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 14 Jun 2018 00:42:19 +0200 Subject: [PATCH] MethodParameter strictly asserts parameter index -1 for return type Issue: SPR-16889 --- .../java/org/springframework/core/MethodParameter.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/core/MethodParameter.java b/spring-core/src/main/java/org/springframework/core/MethodParameter.java index ad3b190f62..2437fde4a4 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -227,6 +227,9 @@ public class MethodParameter { * @since 5.0 */ public Parameter getParameter() { + if (this.parameterIndex < 0) { + throw new IllegalStateException("Cannot retrieve Parameter descriptor for method return type"); + } Parameter parameter = this.parameter; if (parameter == null) { parameter = getExecutable().getParameters()[this.parameterIndex]; @@ -597,6 +600,9 @@ public class MethodParameter { */ @Nullable public String getParameterName() { + if (this.parameterIndex < 0) { + return null; + } ParameterNameDiscoverer discoverer = this.parameterNameDiscoverer; if (discoverer != null) { String[] parameterNames = null; @@ -735,7 +741,8 @@ public class MethodParameter { private static int validateIndex(Executable executable, int parameterIndex) { int count = executable.getParameterCount(); - Assert.isTrue(parameterIndex < count, () -> "Parameter index needs to be between -1 and " + (count - 1)); + Assert.isTrue(parameterIndex >= -1 && parameterIndex < count, + () -> "Parameter index needs to be between -1 and " + (count - 1)); return parameterIndex; }