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 d70e8a18fb..1cc1056ea8 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -733,11 +733,19 @@ public class MethodParameter { protected static int findParameterIndex(Parameter parameter) { Executable executable = parameter.getDeclaringExecutable(); Parameter[] allParams = executable.getParameters(); + // Try first with identity checks for greater performance. for (int i = 0; i < allParams.length; i++) { if (parameter == allParams[i]) { return i; } } + // Potentially try again with object equality checks in order to avoid race + // conditions while invoking java.lang.reflect.Executable.getParameters(). + for (int i = 0; i < allParams.length; i++) { + if (parameter.equals(allParams[i])) { + return i; + } + } throw new IllegalArgumentException("Given parameter [" + parameter + "] does not match any parameter in the declaring executable"); }