Skip to content

Commit 0fc37e0

Browse files
committed
Polish contribution
See spring-projectsgh-32704
1 parent 89d4b6f commit 0fc37e0

File tree

3 files changed

+41
-48
lines changed

3 files changed

+41
-48
lines changed

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

+15-18
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.
@@ -450,28 +448,29 @@ private static boolean isFirstEntryInArray(Object value, @Nullable Object possib
450448
}
451449

452450
/**
453-
* Package up the arguments so that they correctly match what is expected in requiredParameterTypes.
454-
* <p>For example, if requiredParameterTypes is {@code (int, String[])} because the second parameter
455-
* was declared {@code String...}, then if arguments is {@code [1,"a","b"]} then it must be
456-
* repackaged as {@code [1,new String[]{"a","b"}]} in order to match the expected types.
451+
* Package up the supplied {@code args} so that they correctly match what is
452+
* expected in {@code requiredParameterTypes}.
453+
* <p>For example, if {@code requiredParameterTypes} is {@code (int, String[])}
454+
* because the second parameter was declared as {@code String...}, then if
455+
* {@code args} is {@code [1, "a", "b"]} it must be repackaged as
456+
* {@code [1, new String[] {"a", "b"}]} in order to match the expected types.
457457
* @param requiredParameterTypes the types of the parameters for the invocation
458-
* @param args the arguments to be setup ready for the invocation
459-
* @return a repackaged array of arguments where any varargs setup has been done
458+
* @param args the arguments to be set up for the invocation
459+
* @return a repackaged array of arguments where any varargs setup has performed
460460
*/
461461
public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredParameterTypes, Object... args) {
462-
int parameterCount = requiredParameterTypes.length;
463-
Assert.notEmpty(requiredParameterTypes, "Required parameter types must not be empty");
462+
Assert.notEmpty(requiredParameterTypes, "Required parameter types array must not be empty");
464463

464+
int parameterCount = requiredParameterTypes.length;
465465
Class<?> lastRequiredParameterType = requiredParameterTypes[parameterCount - 1];
466-
Assert.isTrue(lastRequiredParameterType.isArray(), "Method must be varargs");
466+
Assert.isTrue(lastRequiredParameterType.isArray(),
467+
"The last required parameter type must be an array to support varargs invocation");
467468

468469
int argumentCount = args.length;
469-
Object lastArgument = argumentCount > 0 ? args[argumentCount - 1] : null;
470+
Object lastArgument = (argumentCount > 0 ? args[argumentCount - 1] : null);
470471

471472
// Check if repackaging is needed...
472-
if (parameterCount != args.length ||
473-
(!isArray(lastArgument) && differentTypes(lastRequiredParameterType, lastArgument))) {
474-
473+
if (parameterCount != argumentCount || !lastRequiredParameterType.isInstance(lastArgument)) {
475474
// Create an array for the leading arguments plus the varargs array argument.
476475
Object[] newArgs = new Object[parameterCount];
477476
// Copy all leading arguments to the new array, omitting the varargs array argument.
@@ -492,12 +491,10 @@ public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredPar
492491
newArgs[newArgs.length - 1] = varargsArray;
493492
return newArgs;
494493
}
494+
495495
return args;
496496
}
497497

498-
private static boolean differentTypes(Class<?> lastRequiredParameterType, @Nullable Object lastArgument) {
499-
return lastArgument == null || lastRequiredParameterType != lastArgument.getClass();
500-
}
501498

502499
/**
503500
* Arguments match kinds.

spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -6631,10 +6631,6 @@ public void concat2(Object... vargs) {
66316631
}
66326632
}
66336633
}
6634-
6635-
public String[] seventeen() {
6636-
return new String[] { "aaa", "bbb", "ccc" };
6637-
}
66386634
}
66396635

66406636

@@ -6863,6 +6859,10 @@ public void sixteen(Object... vargs) {
68636859
}
68646860
}
68656861
}
6862+
6863+
public String[] seventeen() {
6864+
return new String[] { "aaa", "bbb", "ccc" };
6865+
}
68666866
}
68676867

68686868

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)