Skip to content

Commit df4e7d1

Browse files
committed
Polishing
1 parent 2a5eab4 commit df4e7d1

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public String toString() {
522522
/**
523523
* Create a new type descriptor for an object.
524524
* <p>Use this factory method to introspect a source object before asking the
525-
* conversion system to convert it to some another type.
525+
* conversion system to convert it to some other type.
526526
* <p>If the provided object is {@code null}, returns {@code null}, else calls
527527
* {@link #valueOf(Class)} to build a TypeDescriptor from the object's class.
528528
* @param source the source object

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

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Method;
2222
import java.lang.reflect.Proxy;
2323
import java.util.ArrayList;
24+
import java.util.Arrays;
2425
import java.util.List;
2526

2627
import org.junit.jupiter.api.Test;
@@ -44,11 +45,12 @@
4445
* Tests for reflection helper code.
4546
*
4647
* @author Andy Clement
48+
* @author Sam Brannen
4749
*/
48-
public class ReflectionHelperTests extends AbstractExpressionTests {
50+
class ReflectionHelperTests extends AbstractExpressionTests {
4951

5052
@Test
51-
public void testUtilities() throws ParseException {
53+
void utilities() throws ParseException {
5254
SpelExpression expr = (SpelExpression)parser.parseExpression("3+4+5+6+7-2");
5355
ByteArrayOutputStream baos = new ByteArrayOutputStream();
5456
PrintStream ps = new PrintStream(baos);
@@ -74,12 +76,12 @@ public void testUtilities() throws ParseException {
7476
// CompoundExpression value:2
7577
// IntLiteral value:2
7678
// ===> Expression '3+4+5+6+7-2' - AST end
77-
assertThat(s.contains("===> Expression '3+4+5+6+7-2' - AST start")).isTrue();
78-
assertThat(s.contains(" OpPlus value:((((3 + 4) + 5) + 6) + 7) #children:2")).isTrue();
79+
assertThat(s).contains("===> Expression '3+4+5+6+7-2' - AST start");
80+
assertThat(s).contains(" OpPlus value:((((3 + 4) + 5) + 6) + 7) #children:2");
7981
}
8082

8183
@Test
82-
public void testTypedValue() {
84+
void typedValue() {
8385
TypedValue tv1 = new TypedValue("hello");
8486
TypedValue tv2 = new TypedValue("hello");
8587
TypedValue tv3 = new TypedValue("bye");
@@ -91,13 +93,13 @@ public void testTypedValue() {
9193
assertThat(tv3).isNotEqualTo(tv2);
9294
assertThat(tv1).isNotEqualTo(tv3);
9395
assertThat(tv2).isNotEqualTo(tv3);
94-
assertThat(tv2.hashCode()).isEqualTo(tv1.hashCode());
95-
assertThat(tv3.hashCode()).isNotEqualTo(tv1.hashCode());
96-
assertThat(tv3.hashCode()).isNotEqualTo(tv2.hashCode());
96+
assertThat(tv2).hasSameHashCodeAs(tv1);
97+
assertThat(tv3).doesNotHaveSameHashCodeAs(tv1);
98+
assertThat(tv3).doesNotHaveSameHashCodeAs(tv2);
9799
}
98100

99101
@Test
100-
public void testReflectionHelperCompareArguments_ExactMatching() {
102+
void reflectionHelperCompareArguments_ExactMatching() {
101103
StandardTypeConverter tc = new StandardTypeConverter();
102104

103105
// Calling foo(String) with (String) is exact match
@@ -108,7 +110,7 @@ public void testReflectionHelperCompareArguments_ExactMatching() {
108110
}
109111

110112
@Test
111-
public void testReflectionHelperCompareArguments_CloseMatching() {
113+
void reflectionHelperCompareArguments_CloseMatching() {
112114
StandardTypeConverter tc = new StandardTypeConverter();
113115

114116
// Calling foo(List) with (ArrayList) is close match (no conversion required)
@@ -122,7 +124,7 @@ public void testReflectionHelperCompareArguments_CloseMatching() {
122124
}
123125

124126
@Test
125-
public void testReflectionHelperCompareArguments_RequiresConversionMatching() {
127+
void reflectionHelperCompareArguments_RequiresConversionMatching() {
126128
StandardTypeConverter tc = new StandardTypeConverter();
127129

128130
// Calling foo(String,int) with (String,Integer) requires boxing conversion of argument one
@@ -139,15 +141,15 @@ public void testReflectionHelperCompareArguments_RequiresConversionMatching() {
139141
}
140142

141143
@Test
142-
public void testReflectionHelperCompareArguments_NotAMatch() {
144+
void reflectionHelperCompareArguments_NotAMatch() {
143145
StandardTypeConverter typeConverter = new StandardTypeConverter();
144146

145147
// Passing (Super,String) on call to foo(Sub,String) is not a match
146148
checkMatch(new Class<?>[] {Super.class,String.class}, new Class<?>[] {Sub.class,String.class}, typeConverter, null);
147149
}
148150

149151
@Test
150-
public void testReflectionHelperCompareArguments_Varargs_ExactMatching() {
152+
void reflectionHelperCompareArguments_Varargs_ExactMatching() {
151153
StandardTypeConverter tc = new StandardTypeConverter();
152154

153155
// Passing (String[]) on call to (String[]) is exact match
@@ -198,7 +200,7 @@ public void testReflectionHelperCompareArguments_Varargs_ExactMatching() {
198200
}
199201

200202
@Test
201-
public void testConvertArguments() throws Exception {
203+
void convertArguments() throws Exception {
202204
StandardTypeConverter tc = new StandardTypeConverter();
203205
Method oneArg = TestInterface.class.getMethod("oneArg", String.class);
204206
Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class);
@@ -225,7 +227,7 @@ public void testConvertArguments() throws Exception {
225227
}
226228

227229
@Test
228-
public void testConvertArguments2() throws Exception {
230+
void convertArguments2() throws Exception {
229231
StandardTypeConverter tc = new StandardTypeConverter();
230232
Method oneArg = TestInterface.class.getMethod("oneArg", String.class);
231233
Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class);
@@ -252,22 +254,19 @@ public void testConvertArguments2() throws Exception {
252254
}
253255

254256
@Test
255-
public void testSetupArguments() {
257+
void setupArguments() {
256258
Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
257259
new Class<?>[] {String[].class}, "a", "b", "c");
258260

259261
assertThat(newArray).hasSize(1);
260262
Object firstParam = newArray[0];
261263
assertThat(firstParam.getClass().getComponentType()).isEqualTo(String.class);
262264
Object[] firstParamArray = (Object[]) firstParam;
263-
assertThat(firstParamArray).hasSize(3);
264-
assertThat(firstParamArray[0]).isEqualTo("a");
265-
assertThat(firstParamArray[1]).isEqualTo("b");
266-
assertThat(firstParamArray[2]).isEqualTo("c");
265+
assertThat(firstParamArray).containsExactly("a", "b", "c");
267266
}
268267

269268
@Test
270-
public void testReflectivePropertyAccessor() throws Exception {
269+
void reflectivePropertyAccessor() throws Exception {
271270
ReflectivePropertyAccessor rpa = new ReflectivePropertyAccessor();
272271
Tester t = new Tester();
273272
t.setProperty("hello");
@@ -328,7 +327,7 @@ public void testReflectivePropertyAccessor() throws Exception {
328327
}
329328

330329
@Test
331-
public void testOptimalReflectivePropertyAccessor() throws Exception {
330+
void optimalReflectivePropertyAccessor() throws Exception {
332331
ReflectivePropertyAccessor reflective = new ReflectivePropertyAccessor();
333332
Tester tester = new Tester();
334333
tester.setProperty("hello");
@@ -374,19 +373,28 @@ void reflectiveMethodResolverForJdkProxies() throws Exception {
374373
MethodResolver resolver = new ReflectiveMethodResolver();
375374
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
376375

376+
// Nonexistent method
377377
MethodExecutor bogus = resolver.resolve(evaluationContext, proxy, "bogus", List.of());
378378
assertThat(bogus).as("MethodExecutor for bogus()").isNull();
379+
380+
// Method in interface
381+
MethodExecutor run = resolver.resolve(evaluationContext, proxy, "run", List.of());
382+
assertThat(run).as("MethodExecutor for run()").isNotNull();
383+
384+
// Methods in Object
379385
MethodExecutor toString = resolver.resolve(evaluationContext, proxy, "toString", List.of());
380386
assertThat(toString).as("MethodExecutor for toString()").isNotNull();
381387
MethodExecutor hashCode = resolver.resolve(evaluationContext, proxy, "hashCode", List.of());
382388
assertThat(hashCode).as("MethodExecutor for hashCode()").isNotNull();
389+
MethodExecutor equals = resolver.resolve(evaluationContext, proxy, "equals", typeDescriptors(Object.class));
390+
assertThat(equals).as("MethodExecutor for equals()").isNotNull();
383391
}
384392

385393
/**
386394
* Used to validate the match returned from a compareArguments call.
387395
*/
388396
private void checkMatch(Class<?>[] inputTypes, Class<?>[] expectedTypes, StandardTypeConverter typeConverter, ArgumentsMatchKind expectedMatchKind) {
389-
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArguments(getTypeDescriptors(expectedTypes), getTypeDescriptors(inputTypes), typeConverter);
397+
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArguments(typeDescriptors(expectedTypes), typeDescriptors(inputTypes), typeConverter);
390398
if (expectedMatchKind == null) {
391399
assertThat(matchInfo).as("Did not expect them to match in any way").isNull();
392400
}
@@ -408,47 +416,42 @@ else if (expectedMatchKind == ArgumentsMatchKind.REQUIRES_CONVERSION) {
408416
/**
409417
* Used to validate the match returned from a compareArguments call.
410418
*/
411-
private void checkMatch2(Class<?>[] inputTypes, Class<?>[] expectedTypes, StandardTypeConverter typeConverter, ArgumentsMatchKind expectedMatchKind) {
412-
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArgumentsVarargs(getTypeDescriptors(expectedTypes), getTypeDescriptors(inputTypes), typeConverter);
419+
private static void checkMatch2(Class<?>[] inputTypes, Class<?>[] expectedTypes,
420+
StandardTypeConverter typeConverter, ArgumentsMatchKind expectedMatchKind) {
421+
422+
ReflectionHelper.ArgumentsMatchInfo matchInfo =
423+
ReflectionHelper.compareArgumentsVarargs(typeDescriptors(expectedTypes), typeDescriptors(inputTypes), typeConverter);
413424
if (expectedMatchKind == null) {
414425
assertThat(matchInfo).as("Did not expect them to match in any way: " + matchInfo).isNull();
415426
}
416427
else {
417428
assertThat(matchInfo).as("Should not be a null match").isNotNull();
418-
}
419-
420-
if (expectedMatchKind == ArgumentsMatchKind.EXACT) {
421-
assertThat(matchInfo.isExactMatch()).isTrue();
422-
}
423-
else if (expectedMatchKind == ArgumentsMatchKind.CLOSE) {
424-
assertThat(matchInfo.isCloseMatch()).isTrue();
425-
}
426-
else if (expectedMatchKind == ArgumentsMatchKind.REQUIRES_CONVERSION) {
427-
assertThat(matchInfo.isMatchRequiringConversion()).as("expected to be a match requiring conversion, but was " + matchInfo).isTrue();
429+
switch (expectedMatchKind) {
430+
case EXACT -> assertThat(matchInfo.isExactMatch()).isTrue();
431+
case CLOSE -> assertThat(matchInfo.isCloseMatch()).isTrue();
432+
case REQUIRES_CONVERSION -> assertThat(matchInfo.isMatchRequiringConversion())
433+
.as("expected to be a match requiring conversion, but was " + matchInfo).isTrue();
434+
}
428435
}
429436
}
430437

431-
private void checkArguments(Object[] args, Object... expected) {
438+
private static void checkArguments(Object[] args, Object... expected) {
432439
assertThat(args).hasSize(expected.length);
433440
for (int i = 0; i < expected.length; i++) {
434-
checkArgument(expected[i],args[i]);
441+
checkArgument(expected[i], args[i]);
435442
}
436443
}
437444

438-
private void checkArgument(Object expected, Object actual) {
445+
private static void checkArgument(Object expected, Object actual) {
439446
assertThat(actual).isEqualTo(expected);
440447
}
441448

442-
private List<TypeDescriptor> getTypeDescriptors(Class<?>... types) {
443-
List<TypeDescriptor> typeDescriptors = new ArrayList<>(types.length);
444-
for (Class<?> type : types) {
445-
typeDescriptors.add(TypeDescriptor.valueOf(type));
446-
}
447-
return typeDescriptors;
449+
private static List<TypeDescriptor> typeDescriptors(Class<?>... types) {
450+
return Arrays.stream(types).map(TypeDescriptor::valueOf).toList();
448451
}
449452

450453

451-
public interface TestInterface {
454+
interface TestInterface {
452455

453456
void oneArg(String arg1);
454457

spring-webflux/src/main/java/org/springframework/web/reactive/accept/RequestedContentTypeResolverBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)