Skip to content

Commit bc470fc

Browse files
committed
Polish
1 parent 8f78c77 commit bc470fc

File tree

6 files changed

+48
-39
lines changed

6 files changed

+48
-39
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,12 @@ private boolean isBindingCandidate(String attributeName, Object value) {
243243

244244

245245
/**
246-
* Derive the model attribute name for a method parameter based on:
247-
* <ol>
248-
* <li>the parameter {@code @ModelAttribute} annotation value
249-
* <li>the parameter type
250-
* </ol>
246+
* Derive the model attribute name for the given method parameter based on
247+
* a {@code @ModelAttribute} parameter annotation (if present) or falling
248+
* back on parameter type based conventions.
251249
* @param parameter a descriptor for the method parameter
252-
* @return the derived name (never {@code null} or empty String)
250+
* @return the derived name
251+
* @see Conventions#getVariableNameForParameter(MethodParameter)
253252
*/
254253
public static String getNameForParameter(MethodParameter parameter) {
255254
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);

spring-web/src/main/java/org/springframework/web/method/annotation/SessionAttributesHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ public SessionAttributesHandler(Class<?> handlerType, SessionAttributeStore sess
7474
this.attributeNames.addAll(Arrays.asList(annotation.names()));
7575
this.attributeTypes.addAll(Arrays.asList(annotation.types()));
7676
}
77-
78-
for (String attributeName : this.attributeNames) {
79-
this.knownAttributeNames.add(attributeName);
80-
}
77+
this.knownAttributeNames.addAll(this.attributeNames);
8178
}
8279

8380
/**
@@ -90,7 +87,7 @@ public boolean hasSessionAttributes() {
9087

9188
/**
9289
* Whether the attribute name or type match the names and types specified
93-
* via {@code @SessionAttributes} in underlying controller.
90+
* via {@code @SessionAttributes} on the underlying controller.
9491
* <p>Attributes successfully resolved through this method are "remembered"
9592
* and subsequently used in {@link #retrieveAttributes(WebRequest)} and
9693
* {@link #cleanupAttributes(WebRequest)}.

spring-web/src/test/java/org/springframework/web/method/annotation/SessionAttributesHandlerTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -30,12 +30,15 @@
3030
import org.springframework.web.context.request.NativeWebRequest;
3131
import org.springframework.web.context.request.ServletWebRequest;
3232

33-
import static java.util.Arrays.*;
34-
import static org.junit.Assert.*;
33+
import static java.util.Arrays.asList;
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertNotNull;
37+
import static org.junit.Assert.assertNull;
38+
import static org.junit.Assert.assertTrue;
3539

3640
/**
3741
* Test fixture with {@link SessionAttributesHandler}.
38-
*
3942
* @author Rossen Stoyanchev
4043
*/
4144
public class SessionAttributesHandlerTests {
@@ -50,10 +53,10 @@ public class SessionAttributesHandlerTests {
5053

5154
@Test
5255
public void isSessionAttribute() throws Exception {
53-
assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("attr1", null));
54-
assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("attr2", null));
56+
assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("attr1", String.class));
57+
assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("attr2", String.class));
5558
assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("simple", TestBean.class));
56-
assertFalse(sessionAttributesHandler.isHandlerSessionAttribute("simple", null));
59+
assertFalse(sessionAttributesHandler.isHandlerSessionAttribute("simple", String.class));
5760
}
5861

5962
@Test

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
import org.springframework.web.server.ServerWebExchange;
2525

2626
/**
27-
* Context to assist with processing a request and binding it onto Objects.
27+
* Context to assist with binding request data onto Objects and provide access
28+
* to a shared {@link Model} with controller-specific attributes.
2829
*
2930
* <p>Provides methods to create a {@link WebExchangeDataBinder} for a specific
3031
* target, command Object to apply data binding and validation to, or without a

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.core.ReactiveAdapterRegistry;
3131
import org.springframework.core.ResolvableType;
3232
import org.springframework.core.annotation.AnnotatedElementUtils;
33+
import org.springframework.lang.Nullable;
3334
import org.springframework.util.StringUtils;
3435
import org.springframework.web.bind.annotation.ModelAttribute;
3536
import org.springframework.web.reactive.BindingContext;
@@ -73,31 +74,33 @@ public Mono<Void> initModel(BindingContext bindingContext,
7374
List<Mono<HandlerResult>> resultList = new ArrayList<>();
7475
attributeMethods.forEach(invocable -> resultList.add(invocable.invoke(exchange, bindingContext)));
7576

76-
return Mono.zip(resultList, objectArray -> {
77-
return Arrays.stream(objectArray)
78-
.map(object -> (HandlerResult) object)
79-
.map(handlerResult -> handleResult(handlerResult, bindingContext))
80-
.collect(Collectors.toList());
81-
}).flatMap(completionList -> Mono.when(completionList));
77+
return Mono
78+
.zip(resultList, objectArray -> {
79+
return Arrays.stream(objectArray)
80+
.map(object -> handleResult(((HandlerResult) object), bindingContext))
81+
.collect(Collectors.toList());
82+
})
83+
.flatMap(completionList -> Mono.when(completionList));
8284
}
8385

8486
private Mono<Void> handleResult(HandlerResult handlerResult, BindingContext bindingContext) {
8587
Object value = handlerResult.getReturnValue();
8688
if (value != null) {
8789
ResolvableType type = handlerResult.getReturnType();
8890
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(type.getRawClass(), value);
89-
if (adapter != null) {
90-
Class<?> attributeType = (adapter.isNoValue() ? Void.class : type.resolveGeneric());
91-
if (attributeType == Void.class) {
92-
return Mono.from(adapter.toPublisher(value));
93-
}
91+
if (isAsyncVoidType(type, adapter)) {
92+
return Mono.from(adapter.toPublisher(value));
9493
}
9594
String name = getAttributeName(handlerResult.getReturnTypeSource());
9695
bindingContext.getModel().asMap().putIfAbsent(name, value);
9796
}
9897
return Mono.empty();
9998
}
10099

100+
private boolean isAsyncVoidType(ResolvableType type, @Nullable ReactiveAdapter adapter) {
101+
return adapter != null && (adapter.isNoValue() || type.resolveGeneric() == Void.class);
102+
}
103+
101104
private String getAttributeName(MethodParameter param) {
102105
return Optional
103106
.ofNullable(AnnotatedElementUtils.findMergedAnnotation(param.getAnnotatedElement(), ModelAttribute.class))

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ public ApplicationContext getApplicationContext() {
147147
* Obtain the ApplicationContext for actual use.
148148
* @return the ApplicationContext (never {@code null})
149149
* @throws IllegalStateException in case of no ApplicationContext set
150-
* @since 5.0
151150
*/
152151
protected final ApplicationContext obtainApplicationContext() {
153152
ApplicationContext applicationContext = getApplicationContext();
@@ -191,7 +190,9 @@ public Mono<Void> render(@Nullable Map<String, ?> model, @Nullable MediaType con
191190
* <p>The default implementation creates a combined output Map that includes
192191
* model as well as static attributes with the former taking precedence.
193192
*/
194-
protected Mono<Map<String, Object>> getModelAttributes(@Nullable Map<String, ?> model, ServerWebExchange exchange) {
193+
protected Mono<Map<String, Object>> getModelAttributes(@Nullable Map<String, ?> model,
194+
ServerWebExchange exchange) {
195+
195196
int size = (model != null ? model.size() : 0);
196197

197198
Map<String, Object> attributes = new LinkedHashMap<>(size);
@@ -203,9 +204,11 @@ protected Mono<Map<String, Object>> getModelAttributes(@Nullable Map<String, ?>
203204
}
204205

205206
/**
206-
* By default, resolve async attributes supported by the {@link ReactiveAdapterRegistry} to their blocking counterparts.
207-
* <p>View implementations capable of taking advantage of reactive types can override this method if needed.
208-
* @return {@code Mono} to represent when the async attributes have been resolved
207+
* By default, resolve async attributes supported by the
208+
* {@link ReactiveAdapterRegistry} to their blocking counterparts.
209+
* <p>View implementations capable of taking advantage of reactive types
210+
* can override this method if needed.
211+
* @return {@code Mono} for the completion of async attributes resolution
209212
*/
210213
protected Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {
211214

@@ -252,8 +255,9 @@ protected Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {
252255

253256
/**
254257
* Create a RequestContext to expose under the specified attribute name.
255-
* <p>The default implementation creates a standard RequestContext instance for the
256-
* given request and model. Can be overridden in subclasses for custom instances.
258+
* <p>The default implementation creates a standard RequestContext instance
259+
* for the given request and model. Can be overridden in subclasses for
260+
* custom instances.
257261
* @param exchange current exchange
258262
* @param model combined output Map (never {@code null}),
259263
* with dynamic values taking precedence over static attributes
@@ -269,7 +273,8 @@ protected RequestContext createRequestContext(ServerWebExchange exchange, Map<St
269273
* <p>The default implementation looks in the {@link #getApplicationContext()
270274
* Spring configuration} for a {@code RequestDataValueProcessor} bean with
271275
* the name {@link #REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME}.
272-
* @return the RequestDataValueProcessor, or null if there is none at the application context.
276+
* @return the RequestDataValueProcessor, or null if there is none at the
277+
* application context.
273278
*/
274279
@Nullable
275280
protected RequestDataValueProcessor getRequestDataValueProcessor() {
@@ -286,7 +291,8 @@ protected RequestDataValueProcessor getRequestDataValueProcessor() {
286291
* with dynamic values taking precedence over static attributes
287292
* @param contentType the content type selected to render with which should
288293
* match one of the {@link #getSupportedMediaTypes() supported media types}.
289-
*@param exchange current exchange @return {@code Mono} to represent when and if rendering succeeds
294+
*@param exchange current exchange @return {@code Mono} to represent when
295+
* and if rendering succeeds
290296
*/
291297
protected abstract Mono<Void> renderInternal(Map<String, Object> renderAttributes,
292298
@Nullable MediaType contentType, ServerWebExchange exchange);

0 commit comments

Comments
 (0)