Skip to content

Commit 9b96777

Browse files
committed
Polish Spel's ReflectionHelper.setupArgumentsForVarargsInvocation()
1 parent 9af11ad commit 9b96777

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ private static boolean isFirstEntryInArray(Object value, @Nullable Object possib
332332
}
333333

334334
/**
335-
* Package up the arguments so that they correctly match what is expected in parameterTypes.
336-
* For example, if parameterTypes is {@code (int, String[])} because the second parameter
335+
* Package up the arguments so that they correctly match what is expected in requiredParameterTypes.
336+
* <p>For example, if requiredParameterTypes is {@code (int, String[])} because the second parameter
337337
* was declared {@code String...}, then if arguments is {@code [1,"a","b"]} then it must be
338338
* repackaged as {@code [1,new String[]{"a","b"}]} in order to match the expected types.
339339
* @param requiredParameterTypes the types of the parameters for the invocation
@@ -350,23 +350,24 @@ public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredPar
350350
requiredParameterTypes[parameterCount - 1] !=
351351
(args[argumentCount - 1] != null ? args[argumentCount - 1].getClass() : null)) {
352352

353-
int arraySize = 0; // zero size array if nothing to pass as the varargs parameter
354-
if (argumentCount >= parameterCount) {
355-
arraySize = argumentCount - (parameterCount - 1);
356-
}
357-
358-
// Create an array for the varargs arguments
353+
// Create an array for the leading arguments plus the varargs array argument.
359354
Object[] newArgs = new Object[parameterCount];
355+
// Copy all leading arguments to the new array, omitting the varargs array argument.
360356
System.arraycopy(args, 0, newArgs, 0, newArgs.length - 1);
361357

362358
// Now sort out the final argument, which is the varargs one. Before entering this method,
363359
// the arguments should have been converted to the box form of the required type.
360+
int varargsArraySize = 0; // zero size array if nothing to pass as the varargs parameter
361+
if (argumentCount >= parameterCount) {
362+
varargsArraySize = argumentCount - (parameterCount - 1);
363+
}
364364
Class<?> componentType = requiredParameterTypes[parameterCount - 1].getComponentType();
365-
Object repackagedArgs = Array.newInstance(componentType, arraySize);
366-
for (int i = 0; i < arraySize; i++) {
367-
Array.set(repackagedArgs, i, args[parameterCount - 1 + i]);
365+
Object varargsArray = Array.newInstance(componentType, varargsArraySize);
366+
for (int i = 0; i < varargsArraySize; i++) {
367+
Array.set(varargsArray, i, args[parameterCount - 1 + i]);
368368
}
369-
newArgs[newArgs.length - 1] = repackagedArgs;
369+
// Finally, add the varargs array to the new arguments array.
370+
newArgs[newArgs.length - 1] = varargsArray;
370371
return newArgs;
371372
}
372373
return args;

0 commit comments

Comments
 (0)