From c2438cb932fd4948a065254854519667e443d396 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 16 Oct 2017 19:59:07 +0200 Subject: [PATCH] Defensive resolution of getParameterType (actually never returning null) Includes defensive access to volatile field for resolved parameter type. Issue: SPR-16072 --- .../springframework/core/MethodParameter.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) 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 51738dc593..5bc50d9731 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -358,17 +358,20 @@ public class MethodParameter { * @return the parameter type (never {@code null}) */ public Class getParameterType() { - if (this.parameterType == null) { + Class paramType = this.parameterType; + if (paramType == null) { if (this.parameterIndex < 0) { - this.parameterType = (this.method != null ? this.method.getReturnType() : null); + Method method = getMethod(); + paramType = (method != null ? method.getReturnType() : void.class); } else { - this.parameterType = (this.method != null ? - this.method.getParameterTypes()[this.parameterIndex] : - this.constructor.getParameterTypes()[this.parameterIndex]); + paramType = (this.method != null ? + this.method.getParameterTypes()[this.parameterIndex] : + this.constructor.getParameterTypes()[this.parameterIndex]); } + this.parameterType = paramType; } - return this.parameterType; + return paramType; } /** @@ -377,17 +380,20 @@ public class MethodParameter { * @since 3.0 */ public Type getGenericParameterType() { - if (this.genericParameterType == null) { + Type paramType = this.genericParameterType; + if (paramType == null) { if (this.parameterIndex < 0) { - this.genericParameterType = (this.method != null ? this.method.getGenericReturnType() : null); + Method method = getMethod(); + paramType = (method != null ? method.getGenericReturnType() : void.class); } else { - this.genericParameterType = (this.method != null ? - this.method.getGenericParameterTypes()[this.parameterIndex] : - this.constructor.getGenericParameterTypes()[this.parameterIndex]); + paramType = (this.method != null ? + this.method.getGenericParameterTypes()[this.parameterIndex] : + this.constructor.getGenericParameterTypes()[this.parameterIndex]); } + this.genericParameterType = paramType; } - return this.genericParameterType; + return paramType; } /**