Skip to content

Commit 2474c48

Browse files
committed
Polishing
1 parent dc36bb3 commit 2474c48

File tree

8 files changed

+160
-176
lines changed

8 files changed

+160
-176
lines changed

spring-test/src/main/java/org/springframework/test/context/support/AbstractDelegatingSmartContextLoader.java

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.test.context.support;
1818

19-
import java.util.Arrays;
20-
import java.util.List;
21-
2219
import org.apache.commons.logging.Log;
2320
import org.apache.commons.logging.LogFactory;
2421

@@ -88,33 +85,36 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
8885
protected abstract SmartContextLoader getAnnotationConfigLoader();
8986

9087

91-
// SmartContextLoader
88+
// ContextLoader
9289

93-
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) {
94-
if (logger.isDebugEnabled()) {
95-
logger.debug(String.format("Delegating to %s to process context configuration %s.",
96-
name(loader), configAttributes));
97-
}
98-
loader.processContextConfiguration(configAttributes);
90+
/**
91+
* {@code AbstractDelegatingSmartContextLoader} does not support the
92+
* {@link ContextLoader#processLocations(Class, String...)} method. Call
93+
* {@link #processContextConfiguration(ContextConfigurationAttributes)} instead.
94+
* @throws UnsupportedOperationException in this implementation
95+
*/
96+
@Override
97+
public final String[] processLocations(Class<?> clazz, @Nullable String... locations) {
98+
throw new UnsupportedOperationException(
99+
"DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
100+
"Call processContextConfiguration(ContextConfigurationAttributes) instead.");
99101
}
100102

101-
private static ApplicationContext delegateLoading(SmartContextLoader loader, MergedContextConfiguration mergedConfig)
102-
throws Exception {
103-
104-
if (logger.isDebugEnabled()) {
105-
logger.debug(String.format("Delegating to %s to load context from %s.", name(loader), mergedConfig));
106-
}
107-
return loader.loadContext(mergedConfig);
103+
/**
104+
* {@code AbstractDelegatingSmartContextLoader} does not support the
105+
* {@link ContextLoader#loadContext(String...) } method. Call
106+
* {@link #loadContext(MergedContextConfiguration)} instead.
107+
* @throws UnsupportedOperationException in this implementation
108+
*/
109+
@Override
110+
public final ApplicationContext loadContext(String... locations) throws Exception {
111+
throw new UnsupportedOperationException(
112+
"DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
113+
"Call loadContext(MergedContextConfiguration) instead.");
108114
}
109115

110-
private boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig) {
111-
if (loader == getAnnotationConfigLoader()) {
112-
return (mergedConfig.hasClasses() && !mergedConfig.hasLocations());
113-
}
114-
else {
115-
return (mergedConfig.hasLocations() && !mergedConfig.hasClasses());
116-
}
117-
}
116+
117+
// SmartContextLoader
118118

119119
/**
120120
* Delegates to candidate {@code SmartContextLoaders} to process the supplied
@@ -228,15 +228,14 @@ else if (configAttributes.hasClasses()) {
228228
*/
229229
@Override
230230
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
231-
Assert.notNull(mergedConfig, "mergedConfig must not be null");
231+
Assert.notNull(mergedConfig, "MergedContextConfiguration must not be null");
232232

233233
Assert.state(!(mergedConfig.hasLocations() && mergedConfig.hasClasses()), () -> String.format(
234234
"Neither %s nor %s supports loading an ApplicationContext from %s: " +
235235
"declare either 'locations' or 'classes' but not both.", name(getXmlLoader()),
236236
name(getAnnotationConfigLoader()), mergedConfig));
237237

238238
SmartContextLoader[] candidates = {getXmlLoader(), getAnnotationConfigLoader()};
239-
240239
for (SmartContextLoader loader : candidates) {
241240
// Determine if each loader can load a context from the mergedConfig. If it
242241
// can, let it; otherwise, keep iterating.
@@ -254,41 +253,39 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
254253

255254
// else...
256255
throw new IllegalStateException(String.format(
257-
"Neither %s nor %s was able to load an ApplicationContext from %s.", name(getXmlLoader()),
258-
name(getAnnotationConfigLoader()), mergedConfig));
256+
"Neither %s nor %s was able to load an ApplicationContext from %s.",
257+
name(getXmlLoader()), name(getAnnotationConfigLoader()), mergedConfig));
259258
}
260259

261-
private static String name(SmartContextLoader loader) {
262-
return loader.getClass().getSimpleName();
260+
261+
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) {
262+
if (logger.isDebugEnabled()) {
263+
logger.debug(String.format("Delegating to %s to process context configuration %s.",
264+
name(loader), configAttributes));
265+
}
266+
loader.processContextConfiguration(configAttributes);
263267
}
264268

269+
private static ApplicationContext delegateLoading(SmartContextLoader loader, MergedContextConfiguration mergedConfig)
270+
throws Exception {
265271

266-
// ContextLoader
272+
if (logger.isDebugEnabled()) {
273+
logger.debug(String.format("Delegating to %s to load context from %s.", name(loader), mergedConfig));
274+
}
275+
return loader.loadContext(mergedConfig);
276+
}
267277

268-
/**
269-
* {@code AbstractDelegatingSmartContextLoader} does not support the
270-
* {@link ContextLoader#processLocations(Class, String...)} method. Call
271-
* {@link #processContextConfiguration(ContextConfigurationAttributes)} instead.
272-
* @throws UnsupportedOperationException in this implementation
273-
*/
274-
@Override
275-
public final String[] processLocations(Class<?> clazz, @Nullable String... locations) {
276-
throw new UnsupportedOperationException(
277-
"DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
278-
"Call processContextConfiguration(ContextConfigurationAttributes) instead.");
278+
private boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig) {
279+
if (loader == getAnnotationConfigLoader()) {
280+
return (mergedConfig.hasClasses() && !mergedConfig.hasLocations());
281+
}
282+
else {
283+
return (mergedConfig.hasLocations() && !mergedConfig.hasClasses());
284+
}
279285
}
280286

281-
/**
282-
* {@code AbstractDelegatingSmartContextLoader} does not support the
283-
* {@link ContextLoader#loadContext(String...) } method. Call
284-
* {@link #loadContext(MergedContextConfiguration)} instead.
285-
* @throws UnsupportedOperationException in this implementation
286-
*/
287-
@Override
288-
public final ApplicationContext loadContext(String... locations) throws Exception {
289-
throw new UnsupportedOperationException(
290-
"DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
291-
"Call loadContext(MergedContextConfiguration) instead.");
287+
private static String name(SmartContextLoader loader) {
288+
return loader.getClass().getSimpleName();
292289
}
293290

294291
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.web.method.annotation;
1818

19-
import java.util.Arrays;
20-
import java.util.Collection;
2119
import java.util.Collections;
2220
import java.util.List;
2321

@@ -57,15 +55,15 @@ public InitBinderDataBinderFactory(@Nullable List<InvocableHandlerMethod> binder
5755

5856
/**
5957
* Initialize a WebDataBinder with {@code @InitBinder} methods.
60-
* If the {@code @InitBinder} annotation specifies attributes names, it is
61-
* invoked only if the names include the target object name.
58+
* <p>If the {@code @InitBinder} annotation specifies attributes names,
59+
* it is invoked only if the names include the target object name.
6260
* @throws Exception if one of the invoked @{@link InitBinder} methods fail.
6361
*/
6462
@Override
65-
public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {
63+
public void initBinder(WebDataBinder dataBinder, NativeWebRequest request) throws Exception {
6664
for (InvocableHandlerMethod binderMethod : this.binderMethods) {
67-
if (isBinderMethodApplicable(binderMethod, binder)) {
68-
Object returnValue = binderMethod.invokeForRequest(request, null, binder);
65+
if (isBinderMethodApplicable(binderMethod, dataBinder)) {
66+
Object returnValue = binderMethod.invokeForRequest(request, null, dataBinder);
6967
if (returnValue != null) {
7068
throw new IllegalStateException(
7169
"@InitBinder methods should return void: " + binderMethod);
@@ -79,11 +77,11 @@ public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Ex
7977
* the given WebDataBinder instance. By default we check the attributes
8078
* names of the annotation, if present.
8179
*/
82-
protected boolean isBinderMethodApplicable(HandlerMethod binderMethod, WebDataBinder binder) {
80+
protected boolean isBinderMethodApplicable(HandlerMethod binderMethod, WebDataBinder dataBinder) {
8381
InitBinder ann = binderMethod.getMethodAnnotation(InitBinder.class);
8482
Assert.state(ann != null, "No InitBinder annotation");
8583
String[] names = ann.value();
86-
return ObjectUtils.isEmpty(names) || Arrays.asList(names).contains(binder.getObjectName());
84+
return (ObjectUtils.isEmpty(names) || ObjectUtils.containsElement(names, dataBinder.getObjectName()));
8785
}
8886

8987
}

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.web.reactive.result.method.annotation;
1818

19-
import java.util.Arrays;
20-
import java.util.Collection;
2119
import java.util.List;
2220

2321
import org.springframework.lang.Nullable;
@@ -44,7 +42,6 @@ class InitBinderBindingContext extends BindingContext {
4442

4543
private final List<SyncInvocableHandlerMethod> binderMethods;
4644

47-
/* Simple BindingContext to help with the invoking @InitBinder methods */
4845
private final BindingContext binderMethodContext;
4946

5047
private final SessionStatus sessionStatus = new SimpleSessionStatus();
@@ -72,32 +69,28 @@ public SessionStatus getSessionStatus() {
7269

7370

7471
@Override
75-
protected WebExchangeDataBinder initDataBinder(WebExchangeDataBinder dataBinder,
76-
ServerWebExchange exchange) {
77-
72+
protected WebExchangeDataBinder initDataBinder(WebExchangeDataBinder dataBinder, ServerWebExchange exchange) {
7873
this.binderMethods.stream()
7974
.filter(binderMethod -> {
8075
InitBinder ann = binderMethod.getMethodAnnotation(InitBinder.class);
8176
Assert.state(ann != null, "No InitBinder annotation");
8277
String[] names = ann.value();
83-
return ObjectUtils.isEmpty(names) || Arrays.asList(names).contains(dataBinder.getObjectName());
78+
return (ObjectUtils.isEmpty(names) ||
79+
ObjectUtils.containsElement(names, dataBinder.getObjectName()));
8480
})
8581
.forEach(method -> invokeBinderMethod(dataBinder, exchange, method));
8682

8783
return dataBinder;
8884
}
8985

90-
private void invokeBinderMethod(WebExchangeDataBinder dataBinder,
91-
ServerWebExchange exchange, SyncInvocableHandlerMethod binderMethod) {
92-
93-
HandlerResult result = binderMethod.invokeForHandlerResult(
94-
exchange, this.binderMethodContext, dataBinder);
86+
private void invokeBinderMethod(WebExchangeDataBinder dataBinder, ServerWebExchange exchange,
87+
SyncInvocableHandlerMethod binderMethod) {
9588

89+
HandlerResult result = binderMethod.invokeForHandlerResult(exchange, this.binderMethodContext, dataBinder);
9690
if (result != null && result.getReturnValue() != null) {
9791
throw new IllegalStateException(
9892
"@InitBinder methods should return void: " + binderMethod);
9993
}
100-
10194
// Should not happen (no Model argument resolution) ...
10295
if (!this.binderMethodContext.getModel().asMap().isEmpty()) {
10396
throw new IllegalStateException(

0 commit comments

Comments
 (0)