Skip to content

Commit 6ca8fbc

Browse files
committed
Merge ArgumentsMatchInfo into ArgumentsMatchKind in SpEL
The internal ArgumentsMatchInfo type seems to have once had additional methods beyond the functionality provided by the ArgumentsMatchKind enum; however, that is no longer the case. Consequently, there is no need to maintain both types. This commit therefore merges the convenience methods from ArgumentsMatchInfo into the ArgumentsMatchKind enum and removes the unnecessary ArgumentsMatchInfo type.
1 parent 007a347 commit 6ca8fbc

File tree

4 files changed

+50
-56
lines changed

4 files changed

+50
-56
lines changed

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

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public abstract class ReflectionHelper {
5858
* or {@code null} if it was not a match
5959
*/
6060
@Nullable
61-
static ArgumentsMatchInfo compareArguments(
61+
static ArgumentsMatchKind compareArguments(
6262
List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {
6363

6464
Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
@@ -88,7 +88,7 @@ else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
8888
}
8989
}
9090
}
91-
return (match != null ? new ArgumentsMatchInfo(match) : null);
91+
return match;
9292
}
9393

9494
/**
@@ -143,11 +143,11 @@ else if (ClassUtils.isAssignable(paramTypeClazz, superClass)) {
143143
* @param expectedArgTypes the types the method/constructor is expecting
144144
* @param suppliedArgTypes the types that are being supplied at the point of invocation
145145
* @param typeConverter a registered type converter
146-
* @return an {@code ArgumentsMatchInfo} object indicating what kind of match it was,
146+
* @return an {@code ArgumentsMatchKind} object indicating what kind of match it was,
147147
* or {@code null} if it was not a match
148148
*/
149149
@Nullable
150-
static ArgumentsMatchInfo compareArgumentsVarargs(
150+
static ArgumentsMatchKind compareArgumentsVarargs(
151151
List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {
152152

153153
Assert.isTrue(!CollectionUtils.isEmpty(expectedArgTypes),
@@ -231,7 +231,7 @@ else if (typeConverter.canConvert(suppliedArg, TypeDescriptor.valueOf(varargsCom
231231
}
232232
}
233233

234-
return (match != null ? new ArgumentsMatchInfo(match) : null);
234+
return match;
235235
}
236236

237237
/**
@@ -507,45 +507,37 @@ public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredPar
507507

508508

509509
/**
510-
* Arguments match kinds.
510+
* {@code ArgumentsMatchKind} describes what kind of match was achieved between two sets
511+
* of arguments: the set that a method/constructor is expecting and the set that is being
512+
* supplied at the point of invocation.
511513
*/
512514
enum ArgumentsMatchKind {
513515

514-
/** An exact match is where the parameter types exactly match what the method/constructor is expecting. */
516+
/**
517+
* An exact match is where the parameter types exactly match what the method/constructor is expecting.
518+
*/
515519
EXACT,
516520

517-
/** A close match is where the parameter types either exactly match or are assignment-compatible. */
521+
/**
522+
* A close match is where the parameter types either exactly match or are assignment-compatible.
523+
*/
518524
CLOSE,
519525

520-
/** A conversion match is where the type converter must be used to transform some of the parameter types. */
521-
REQUIRES_CONVERSION
522-
}
523-
524-
525-
/**
526-
* An instance of {@code ArgumentsMatchInfo} describes what kind of match was achieved
527-
* between two sets of arguments - the set that a method/constructor is expecting
528-
* and the set that is being supplied at the point of invocation.
529-
*
530-
* @param kind the kind of match that was achieved
531-
*/
532-
record ArgumentsMatchInfo(ArgumentsMatchKind kind) {
526+
/**
527+
* A conversion match is where the type converter must be used to transform some of the parameter types.
528+
*/
529+
REQUIRES_CONVERSION;
533530

534531
public boolean isExactMatch() {
535-
return (this.kind == ArgumentsMatchKind.EXACT);
532+
return (this == EXACT);
536533
}
537534

538535
public boolean isCloseMatch() {
539-
return (this.kind == ArgumentsMatchKind.CLOSE);
536+
return (this == CLOSE);
540537
}
541538

542539
public boolean isMatchRequiringConversion() {
543-
return (this.kind == ArgumentsMatchKind.REQUIRES_CONVERSION);
544-
}
545-
546-
@Override
547-
public String toString() {
548-
return "ArgumentsMatchInfo: " + this.kind;
540+
return (this == REQUIRES_CONVERSION);
549541
}
550542
}
551543

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.expression.EvaluationContext;
3131
import org.springframework.expression.EvaluationException;
3232
import org.springframework.expression.TypeConverter;
33+
import org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind;
3334
import org.springframework.lang.Nullable;
3435

3536
/**
@@ -75,28 +76,28 @@ public ConstructorExecutor resolve(EvaluationContext context, String typeName, L
7576
for (int i = 0; i < paramCount; i++) {
7677
paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
7778
}
78-
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
79+
ArgumentsMatchKind matchKind = null;
7980
if (ctor.isVarArgs() && argumentTypes.size() >= paramCount - 1) {
8081
// *sigh* complicated
8182
// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
8283
// being provided should be
8384
// the same type whilst the final argument to the method must be an array of that (oh, how easy...not) -
8485
// or the final parameter
8586
// we are supplied does match exactly (it is an array already).
86-
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
87+
matchKind = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
8788
}
8889
else if (paramCount == argumentTypes.size()) {
8990
// worth a closer look
90-
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
91+
matchKind = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
9192
}
92-
if (matchInfo != null) {
93-
if (matchInfo.isExactMatch()) {
93+
if (matchKind != null) {
94+
if (matchKind.isExactMatch()) {
9495
return new ReflectiveConstructorExecutor(ctor);
9596
}
96-
else if (matchInfo.isCloseMatch()) {
97+
else if (matchKind.isCloseMatch()) {
9798
closeMatch = ctor;
9899
}
99-
else if (matchInfo.isMatchRequiringConversion()) {
100+
else if (matchKind.isMatchRequiringConversion()) {
100101
matchRequiringConversion = ctor;
101102
}
102103
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.expression.TypeConverter;
4040
import org.springframework.expression.spel.SpelEvaluationException;
4141
import org.springframework.expression.spel.SpelMessage;
42+
import org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind;
4243
import org.springframework.lang.Nullable;
4344

4445
/**
@@ -169,20 +170,20 @@ else if (m1.isVarArgs() && !m2.isVarArgs()) {
169170
for (int i = 0; i < paramCount; i++) {
170171
paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, i)));
171172
}
172-
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
173+
ArgumentsMatchKind matchKind = null;
173174
if (method.isVarArgs() && argumentTypes.size() >= (paramCount - 1)) {
174175
// *sigh* complicated
175-
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
176+
matchKind = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
176177
}
177178
else if (paramCount == argumentTypes.size()) {
178179
// Name and parameter number match, check the arguments
179-
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
180+
matchKind = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
180181
}
181-
if (matchInfo != null) {
182-
if (matchInfo.isExactMatch()) {
182+
if (matchKind != null) {
183+
if (matchKind.isExactMatch()) {
183184
return new ReflectiveMethodExecutor(method, type);
184185
}
185-
else if (matchInfo.isCloseMatch()) {
186+
else if (matchKind.isCloseMatch()) {
186187
if (this.useDistance) {
187188
int matchDistance = ReflectionHelper.getTypeDifferenceWeight(paramDescriptors, argumentTypes);
188189
if (closeMatch == null || matchDistance < closeMatchDistance) {
@@ -198,7 +199,7 @@ else if (matchInfo.isCloseMatch()) {
198199
}
199200
}
200201
}
201-
else if (matchInfo.isMatchRequiringConversion()) {
202+
else if (matchKind.isMatchRequiringConversion()) {
202203
if (matchRequiringConversion != null) {
203204
multipleOptions = true;
204205
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,22 +450,22 @@ void reflectiveMethodResolverForJdkProxies() throws Exception {
450450
* Used to validate the match returned from a compareArguments call.
451451
*/
452452
private void checkMatch(Class<?>[] inputTypes, Class<?>[] expectedTypes, StandardTypeConverter typeConverter, ArgumentsMatchKind expectedMatchKind) {
453-
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArguments(typeDescriptors(expectedTypes), typeDescriptors(inputTypes), typeConverter);
453+
ArgumentsMatchKind matchKind = ReflectionHelper.compareArguments(typeDescriptors(expectedTypes), typeDescriptors(inputTypes), typeConverter);
454454
if (expectedMatchKind == null) {
455-
assertThat(matchInfo).as("Did not expect them to match in any way").isNull();
455+
assertThat(matchKind).as("Did not expect them to match in any way").isNull();
456456
}
457457
else {
458-
assertThat(matchInfo).as("Should not be a null match").isNotNull();
458+
assertThat(matchKind).as("Should not be a null match").isNotNull();
459459
}
460460

461461
if (expectedMatchKind == EXACT) {
462-
assertThat(matchInfo.isExactMatch()).isTrue();
462+
assertThat(matchKind.isExactMatch()).isTrue();
463463
}
464464
else if (expectedMatchKind == CLOSE) {
465-
assertThat(matchInfo.isCloseMatch()).isTrue();
465+
assertThat(matchKind.isCloseMatch()).isTrue();
466466
}
467467
else if (expectedMatchKind == REQUIRES_CONVERSION) {
468-
assertThat(matchInfo.isMatchRequiringConversion()).as("expected to be a match requiring conversion, but was " + matchInfo).isTrue();
468+
assertThat(matchKind.isMatchRequiringConversion()).as("expected to be a match requiring conversion, but was " + matchKind).isTrue();
469469
}
470470
}
471471

@@ -475,18 +475,18 @@ else if (expectedMatchKind == REQUIRES_CONVERSION) {
475475
private static void checkMatchVarargs(Class<?>[] inputTypes, Class<?>[] expectedTypes,
476476
StandardTypeConverter typeConverter, ArgumentsMatchKind expectedMatchKind) {
477477

478-
ReflectionHelper.ArgumentsMatchInfo matchInfo =
478+
ArgumentsMatchKind matchKind =
479479
ReflectionHelper.compareArgumentsVarargs(typeDescriptors(expectedTypes), typeDescriptors(inputTypes), typeConverter);
480480
if (expectedMatchKind == null) {
481-
assertThat(matchInfo).as("Did not expect them to match in any way: " + matchInfo).isNull();
481+
assertThat(matchKind).as("Did not expect them to match in any way: " + matchKind).isNull();
482482
}
483483
else {
484-
assertThat(matchInfo).as("Should not be a null match").isNotNull();
484+
assertThat(matchKind).as("Should not be a null match").isNotNull();
485485
switch (expectedMatchKind) {
486-
case EXACT -> assertThat(matchInfo.isExactMatch()).isTrue();
487-
case CLOSE -> assertThat(matchInfo.isCloseMatch()).isTrue();
488-
case REQUIRES_CONVERSION -> assertThat(matchInfo.isMatchRequiringConversion())
489-
.as("expected to be a match requiring conversion, but was " + matchInfo).isTrue();
486+
case EXACT -> assertThat(matchKind.isExactMatch()).isTrue();
487+
case CLOSE -> assertThat(matchKind.isCloseMatch()).isTrue();
488+
case REQUIRES_CONVERSION -> assertThat(matchKind.isMatchRequiringConversion())
489+
.as("expected to be a match requiring conversion, but was " + matchKind).isTrue();
490490
}
491491
}
492492
}

0 commit comments

Comments
 (0)