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 a44ce5898a..d709fba357 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -725,11 +725,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"); }