Skip to content

Commit aebc48e

Browse files
committed
Revise contribution
See gh-32704
1 parent f51be0a commit aebc48e

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

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

+7-12
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import org.springframework.util.CollectionUtils;
3737
import org.springframework.util.MethodInvoker;
3838

39-
import static org.springframework.util.ObjectUtils.isArray;
40-
4139
/**
4240
* Utility methods used by the reflection resolver code to discover the appropriate
4341
* methods/constructors and fields that should be used in expressions.
@@ -453,19 +451,18 @@ private static boolean isFirstEntryInArray(Object value, @Nullable Object possib
453451
* @return a repackaged array of arguments where any varargs setup has performed
454452
*/
455453
public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredParameterTypes, Object... args) {
456-
int parameterCount = requiredParameterTypes.length;
457-
Assert.notEmpty(requiredParameterTypes, "Required parameter types must not be empty");
454+
Assert.notEmpty(requiredParameterTypes, "Required parameter types array must not be empty");
458455

456+
int parameterCount = requiredParameterTypes.length;
459457
Class<?> lastRequiredParameterType = requiredParameterTypes[parameterCount - 1];
460-
Assert.isTrue(lastRequiredParameterType.isArray(), "Method must be varargs");
458+
Assert.isTrue(lastRequiredParameterType.isArray(),
459+
"The last required parameter type must be an array to support varargs invocation");
461460

462461
int argumentCount = args.length;
463-
Object lastArgument = argumentCount > 0 ? args[argumentCount - 1] : null;
462+
Object lastArgument = (argumentCount > 0 ? args[argumentCount - 1] : null);
464463

465464
// Check if repackaging is needed...
466-
if (parameterCount != args.length ||
467-
(!isArray(lastArgument) && differentTypes(lastRequiredParameterType, lastArgument))) {
468-
465+
if (parameterCount != argumentCount || !lastRequiredParameterType.isInstance(lastArgument)) {
469466
// Create an array for the leading arguments plus the varargs array argument.
470467
Object[] newArgs = new Object[parameterCount];
471468
// Copy all leading arguments to the new array, omitting the varargs array argument.
@@ -486,12 +483,10 @@ public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredPar
486483
newArgs[newArgs.length - 1] = varargsArray;
487484
return newArgs;
488485
}
486+
489487
return args;
490488
}
491489

492-
private static boolean differentTypes(Class<?> lastRequiredParameterType, @Nullable Object lastArgument) {
493-
return lastArgument == null || lastRequiredParameterType != lastArgument.getClass();
494-
}
495490

496491
/**
497492
* Arguments match kinds.

Diff for: spring-expression/src/test/java/org/springframework/expression/spel/support/ReflectionHelperTests.java

+22-26
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
4242
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
43-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
43+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
4444
import static org.assertj.core.api.InstanceOfAssertFactories.array;
4545
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.CLOSE;
4646
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.EXACT;
@@ -252,34 +252,42 @@ void convertAllArguments() throws Exception {
252252
checkArguments(args, "3", null, "3.0");
253253
}
254254

255+
@Test
256+
void setupArgumentsForVarargsInvocationPreconditions() {
257+
assertThatIllegalArgumentException()
258+
.isThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation(new Class[] {}, "a"))
259+
.withMessage("Required parameter types array must not be empty");
260+
261+
assertThatIllegalArgumentException()
262+
.isThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation(
263+
new Class<?>[] { Integer.class, Integer.class }, 123))
264+
.withMessage("The last required parameter type must be an array to support varargs invocation");
265+
}
266+
255267
@Test
256268
void setupArgumentsForVarargsInvocation() {
257269
Object[] newArray;
258270

259-
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
260-
new Class<?>[] {String[].class}, "a", "b", "c");
271+
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class }, "a", "b", "c");
261272
assertThat(newArray)
262273
.singleElement()
263274
.asInstanceOf(array(String[].class))
264275
.containsExactly("a", "b", "c");
265276

266-
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
267-
new Class<?>[] { Object[].class }, "a", "b", "c");
277+
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { Object[].class }, "a", "b", "c");
268278
assertThat(newArray)
269279
.singleElement()
270280
.asInstanceOf(array(Object[].class))
271281
.containsExactly("a", "b", "c");
272282

273283
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
274284
new Class<?>[] { Integer.class, Integer.class, String[].class }, 123, 456, "a", "b", "c");
275-
assertThat(newArray)
276-
.satisfiesExactly(
277-
i -> assertThat(i).isEqualTo(123),
278-
i -> assertThat(i).isEqualTo(456),
279-
i -> assertThat(i).asInstanceOf(array(String[].class)).containsExactly("a", "b", "c"));
285+
assertThat(newArray).satisfiesExactly(
286+
one -> assertThat(one).isEqualTo(123),
287+
two -> assertThat(two).isEqualTo(456),
288+
three -> assertThat(three).asInstanceOf(array(String[].class)).containsExactly("a", "b", "c"));
280289

281-
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
282-
new Class<?>[] { String[].class });
290+
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class });
283291
assertThat(newArray)
284292
.singleElement()
285293
.asInstanceOf(array(String[].class))
@@ -299,30 +307,18 @@ void setupArgumentsForVarargsInvocation() {
299307
.asInstanceOf(array(Object[].class))
300308
.containsExactly("a", "b", "c");
301309

302-
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
303-
new Class<?>[] { String[].class }, "a");
310+
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class }, "a");
304311
assertThat(newArray)
305312
.singleElement()
306313
.asInstanceOf(array(String[].class))
307314
.containsExactly("a");
308315

309-
310-
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
311-
new Class<?>[] { String[].class }, new Object[]{null});
316+
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class }, new Object[] { null });
312317
assertThat(newArray)
313318
.singleElement()
314319
.asInstanceOf(array(String[].class))
315320
.singleElement()
316321
.isNull();
317-
318-
assertThatThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation(
319-
new Class<?>[] { Integer.class, Integer.class }, 123, 456))
320-
.isInstanceOf(IllegalArgumentException.class)
321-
.hasMessage("Method must be varargs");
322-
323-
assertThatThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation(new Class[] {}, "a", "b", "c"))
324-
.isInstanceOf(IllegalArgumentException.class)
325-
.hasMessage("Required parameter types must not be empty");
326322
}
327323

328324
@Test

0 commit comments

Comments
 (0)