Skip to content

Commit c5c77b9

Browse files
committed
Polishing
1 parent 6b90504 commit c5c77b9

File tree

3 files changed

+54
-44
lines changed

3 files changed

+54
-44
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,13 @@ private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
189189

190190
MergedAnnotations mergedAnnotations = MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY,
191191
RepeatableContainers.none());
192-
List<AnnotationDescriptor<RequestMapping>> requestMappings = mergedAnnotations.stream(RequestMapping.class)
193-
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
194-
.map(AnnotationDescriptor::new)
195-
.distinct()
196-
.toList();
197192

193+
List<AnnotationDescriptor<RequestMapping>> requestMappings = getAnnotationDescriptors(
194+
mergedAnnotations, RequestMapping.class);
198195
if (!requestMappings.isEmpty()) {
199196
if (requestMappings.size() > 1 && logger.isWarnEnabled()) {
200197
logger.warn("Multiple @RequestMapping annotations found on %s, but only the first will be used: %s"
201-
.formatted(element, requestMappings));
198+
.formatted(element, requestMappings));
202199
}
203200
return createRequestMappingInfo(requestMappings.get(0).annotation, customCondition);
204201
}
@@ -430,6 +427,16 @@ private String resolveCorsAnnotationValue(String value) {
430427
}
431428
}
432429

430+
private static <A extends Annotation> List<AnnotationDescriptor<A>> getAnnotationDescriptors(
431+
MergedAnnotations mergedAnnotations, Class<A> annotationType) {
432+
433+
return mergedAnnotations.stream(annotationType)
434+
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
435+
.map(AnnotationDescriptor::new)
436+
.distinct()
437+
.toList();
438+
}
439+
433440
private static class AnnotationDescriptor<A extends Annotation> {
434441

435442
private final A annotation;

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,16 +349,13 @@ private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
349349

350350
MergedAnnotations mergedAnnotations = MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY,
351351
RepeatableContainers.none());
352-
List<AnnotationDescriptor<RequestMapping>> requestMappings = mergedAnnotations.stream(RequestMapping.class)
353-
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
354-
.map(AnnotationDescriptor::new)
355-
.distinct()
356-
.toList();
357352

353+
List<AnnotationDescriptor<RequestMapping>> requestMappings = getAnnotationDescriptors(
354+
mergedAnnotations, RequestMapping.class);
358355
if (!requestMappings.isEmpty()) {
359356
if (requestMappings.size() > 1 && logger.isWarnEnabled()) {
360357
logger.warn("Multiple @RequestMapping annotations found on %s, but only the first will be used: %s"
361-
.formatted(element, requestMappings));
358+
.formatted(element, requestMappings));
362359
}
363360
return createRequestMappingInfo(requestMappings.get(0).annotation, customCondition);
364361
}
@@ -609,6 +606,16 @@ private String resolveCorsAnnotationValue(String value) {
609606
}
610607
}
611608

609+
private static <A extends Annotation> List<AnnotationDescriptor<A>> getAnnotationDescriptors(
610+
MergedAnnotations mergedAnnotations, Class<A> annotationType) {
611+
612+
return mergedAnnotations.stream(annotationType)
613+
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
614+
.map(AnnotationDescriptor::new)
615+
.distinct()
616+
.toList();
617+
}
618+
612619
private static class AnnotationDescriptor<A extends Annotation> {
613620

614621
private final A annotation;

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ class RequestMappingHandlerMappingTests {
7171

7272
@SuppressWarnings("unused")
7373
static Stream<Arguments> pathPatternsArguments() {
74-
RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping();
7574
StaticWebApplicationContext wac1 = new StaticWebApplicationContext();
76-
mapping1.setApplicationContext(wac1);
77-
7875
StaticWebApplicationContext wac2 = new StaticWebApplicationContext();
7976

77+
RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping();
78+
mapping1.setApplicationContext(wac1);
79+
8080
RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
8181
mapping2.setPatternParser(null);
8282
mapping2.setApplicationContext(wac2);
@@ -86,8 +86,7 @@ static Stream<Arguments> pathPatternsArguments() {
8686

8787
@Test
8888
void builderConfiguration() {
89-
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
90-
mapping.setApplicationContext(new StaticWebApplicationContext());
89+
RequestMappingHandlerMapping mapping = createMapping();
9190

9291
RequestMappingInfo.BuilderConfiguration config = mapping.getBuilderConfiguration();
9392
assertThat(config).isNotNull();
@@ -99,22 +98,20 @@ void builderConfiguration() {
9998
@Test
10099
@SuppressWarnings("deprecation")
101100
void useRegisteredSuffixPatternMatch() {
102-
103-
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
104-
handlerMapping.setApplicationContext(new StaticWebApplicationContext());
101+
RequestMappingHandlerMapping mapping = createMapping();
105102

106103
Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
107104
org.springframework.web.accept.PathExtensionContentNegotiationStrategy strategy =
108105
new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(fileExtensions);
109106
ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
110107

111-
handlerMapping.setContentNegotiationManager(manager);
112-
handlerMapping.setUseRegisteredSuffixPatternMatch(true);
113-
handlerMapping.afterPropertiesSet();
108+
mapping.setContentNegotiationManager(manager);
109+
mapping.setUseRegisteredSuffixPatternMatch(true);
110+
mapping.afterPropertiesSet();
114111

115-
assertThat(handlerMapping.useSuffixPatternMatch()).isTrue();
116-
assertThat(handlerMapping.useRegisteredSuffixPatternMatch()).isTrue();
117-
assertThat(handlerMapping.getFileExtensions()).isEqualTo(Collections.singletonList("json"));
112+
assertThat(mapping.useSuffixPatternMatch()).isTrue();
113+
assertThat(mapping.useRegisteredSuffixPatternMatch()).isTrue();
114+
assertThat(mapping.getFileExtensions()).isEqualTo(Collections.singletonList("json"));
118115
}
119116

120117
@Test
@@ -150,25 +147,24 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
150147
@Test
151148
@SuppressWarnings("deprecation")
152149
void suffixPatternMatchSettings() {
153-
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
150+
RequestMappingHandlerMapping mapping = createMapping();
154151

155-
assertThat(handlerMapping.useSuffixPatternMatch()).isFalse();
156-
assertThat(handlerMapping.useRegisteredSuffixPatternMatch()).isFalse();
152+
assertThat(mapping.useSuffixPatternMatch()).isFalse();
153+
assertThat(mapping.useRegisteredSuffixPatternMatch()).isFalse();
157154

158-
handlerMapping.setUseRegisteredSuffixPatternMatch(false);
159-
assertThat(handlerMapping.useSuffixPatternMatch())
155+
mapping.setUseRegisteredSuffixPatternMatch(false);
156+
assertThat(mapping.useSuffixPatternMatch())
160157
.as("'false' registeredSuffixPatternMatch shouldn't impact suffixPatternMatch")
161158
.isFalse();
162159

163-
handlerMapping.setUseRegisteredSuffixPatternMatch(true);
164-
assertThat(handlerMapping.useSuffixPatternMatch())
160+
mapping.setUseRegisteredSuffixPatternMatch(true);
161+
assertThat(mapping.useSuffixPatternMatch())
165162
.as("'true' registeredSuffixPatternMatch should enable suffixPatternMatch")
166163
.isTrue();
167164
}
168165

169166
@PathPatternsParameterizedTest
170167
void resolveEmbeddedValuesInPatterns(RequestMappingHandlerMapping mapping) {
171-
172168
mapping.setEmbeddedValueResolver(
173169
value -> "/${pattern}/bar".equals(value) ? "/foo/bar" : value
174170
);
@@ -181,7 +177,6 @@ void resolveEmbeddedValuesInPatterns(RequestMappingHandlerMapping mapping) {
181177

182178
@PathPatternsParameterizedTest
183179
void pathPrefix(RequestMappingHandlerMapping mapping) throws Exception {
184-
185180
mapping.setEmbeddedValueResolver(value -> "/${prefix}".equals(value) ? "/api" : value);
186181
mapping.setPathPrefixes(Collections.singletonMap(
187182
"/${prefix}", HandlerTypePredicate.forAnnotation(RestController.class)));
@@ -226,7 +221,6 @@ private void initRequestPath(RequestMappingHandlerMapping mapping, MockHttpServl
226221

227222
@PathPatternsParameterizedTest
228223
void resolveRequestMappingViaComposedAnnotation(RequestMappingHandlerMapping mapping) {
229-
230224
RequestMappingInfo info = assertComposedAnnotationMapping(
231225
mapping, "postJson", "/postJson", RequestMethod.POST);
232226

@@ -285,9 +279,7 @@ void patchMapping() {
285279
@SuppressWarnings("DataFlowIssue")
286280
@Test
287281
void httpExchangeWithDefaultValues() throws NoSuchMethodException {
288-
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
289-
mapping.setApplicationContext(new StaticWebApplicationContext());
290-
mapping.afterPropertiesSet();
282+
RequestMappingHandlerMapping mapping = createMapping();
291283

292284
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(
293285
HttpExchangeController.class.getMethod("defaultValuesExchange"),
@@ -307,9 +299,7 @@ void httpExchangeWithDefaultValues() throws NoSuchMethodException {
307299
@SuppressWarnings("DataFlowIssue")
308300
@Test
309301
void httpExchangeWithCustomValues() throws Exception {
310-
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
311-
mapping.setApplicationContext(new StaticWebApplicationContext());
312-
mapping.afterPropertiesSet();
302+
RequestMappingHandlerMapping mapping = createMapping();
313303

314304
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(
315305
HttpExchangeController.class.getMethod("customValuesExchange"),
@@ -332,17 +322,23 @@ void httpExchangeWithCustomValues() throws Exception {
332322
.containsOnly(MediaType.valueOf("text/plain;charset=UTF-8"));
333323
}
334324

335-
private RequestMappingInfo assertComposedAnnotationMapping(RequestMethod requestMethod) {
325+
private static RequestMappingHandlerMapping createMapping() {
336326
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
337327
mapping.setApplicationContext(new StaticWebApplicationContext());
328+
mapping.afterPropertiesSet();
329+
return mapping;
330+
}
331+
332+
private static RequestMappingInfo assertComposedAnnotationMapping(RequestMethod requestMethod) {
333+
RequestMappingHandlerMapping mapping = createMapping();
338334

339335
String methodName = requestMethod.name().toLowerCase();
340336
String path = "/" + methodName;
341337

342338
return assertComposedAnnotationMapping(mapping, methodName, path, requestMethod);
343339
}
344340

345-
private RequestMappingInfo assertComposedAnnotationMapping(
341+
private static RequestMappingInfo assertComposedAnnotationMapping(
346342
RequestMappingHandlerMapping mapping, String methodName, String path, RequestMethod requestMethod) {
347343

348344
Class<?> clazz = ComposedAnnotationController.class;

0 commit comments

Comments
 (0)