@@ -332,8 +332,8 @@ private static boolean isFirstEntryInArray(Object value, @Nullable Object possib
332
332
}
333
333
334
334
/**
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
337
337
* was declared {@code String...}, then if arguments is {@code [1,"a","b"]} then it must be
338
338
* repackaged as {@code [1,new String[]{"a","b"}]} in order to match the expected types.
339
339
* @param requiredParameterTypes the types of the parameters for the invocation
@@ -350,23 +350,24 @@ public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredPar
350
350
requiredParameterTypes [parameterCount - 1 ] !=
351
351
(args [argumentCount - 1 ] != null ? args [argumentCount - 1 ].getClass () : null )) {
352
352
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.
359
354
Object [] newArgs = new Object [parameterCount ];
355
+ // Copy all leading arguments to the new array, omitting the varargs array argument.
360
356
System .arraycopy (args , 0 , newArgs , 0 , newArgs .length - 1 );
361
357
362
358
// Now sort out the final argument, which is the varargs one. Before entering this method,
363
359
// 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
+ }
364
364
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 ]);
368
368
}
369
- newArgs [newArgs .length - 1 ] = repackagedArgs ;
369
+ // Finally, add the varargs array to the new arguments array.
370
+ newArgs [newArgs .length - 1 ] = varargsArray ;
370
371
return newArgs ;
371
372
}
372
373
return args ;
0 commit comments