Skip to content

Commit 75b323e

Browse files
committed
Polishing
1 parent 13637ec commit 75b323e

File tree

7 files changed

+169
-176
lines changed

7 files changed

+169
-176
lines changed

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

Lines changed: 52 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

@@ -87,33 +84,36 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
8784
protected abstract SmartContextLoader getAnnotationConfigLoader();
8885

8986

90-
// SmartContextLoader
87+
// ContextLoader
9188

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

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

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

118118
/**
119119
* Delegates to candidate {@code SmartContextLoaders} to process the supplied
@@ -232,8 +232,7 @@ else if (configAttributes.hasClasses()) {
232232
*/
233233
@Override
234234
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
235-
Assert.notNull(mergedConfig, "mergedConfig must not be null");
236-
List<SmartContextLoader> candidates = Arrays.asList(getXmlLoader(), getAnnotationConfigLoader());
235+
Assert.notNull(mergedConfig, "MergedContextConfiguration must not be null");
237236

238237
if (mergedConfig.hasLocations() && mergedConfig.hasClasses()) {
239238
throw new IllegalStateException(String.format(
@@ -242,6 +241,7 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
242241
name(getAnnotationConfigLoader()), mergedConfig));
243242
}
244243

244+
SmartContextLoader[] candidates = {getXmlLoader(), getAnnotationConfigLoader()};
245245
for (SmartContextLoader loader : candidates) {
246246
// Determine if each loader can load a context from the mergedConfig. If it
247247
// can, let it; otherwise, keep iterating.
@@ -259,41 +259,39 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
259259

260260
// else...
261261
throw new IllegalStateException(String.format(
262-
"Neither %s nor %s was able to load an ApplicationContext from %s.", name(getXmlLoader()),
263-
name(getAnnotationConfigLoader()), mergedConfig));
262+
"Neither %s nor %s was able to load an ApplicationContext from %s.",
263+
name(getXmlLoader()), name(getAnnotationConfigLoader()), mergedConfig));
264264
}
265265

266-
private static String name(SmartContextLoader loader) {
267-
return loader.getClass().getSimpleName();
266+
267+
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) {
268+
if (logger.isDebugEnabled()) {
269+
logger.debug(String.format("Delegating to %s to process context configuration %s.",
270+
name(loader), configAttributes));
271+
}
272+
loader.processContextConfiguration(configAttributes);
268273
}
269274

275+
private static ApplicationContext delegateLoading(SmartContextLoader loader, MergedContextConfiguration mergedConfig)
276+
throws Exception {
270277

271-
// ContextLoader
278+
if (logger.isDebugEnabled()) {
279+
logger.debug(String.format("Delegating to %s to load context from %s.", name(loader), mergedConfig));
280+
}
281+
return loader.loadContext(mergedConfig);
282+
}
272283

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

286-
/**
287-
* {@code AbstractDelegatingSmartContextLoader} does not support the
288-
* {@link ContextLoader#loadContext(String...) } method. Call
289-
* {@link #loadContext(MergedContextConfiguration)} instead.
290-
* @throws UnsupportedOperationException
291-
*/
292-
@Override
293-
public final ApplicationContext loadContext(String... locations) throws Exception {
294-
throw new UnsupportedOperationException(
295-
"DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
296-
"Call loadContext(MergedContextConfiguration) instead.");
293+
private static String name(SmartContextLoader loader) {
294+
return loader.getClass().getSimpleName();
297295
}
298296

299297
}

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

Lines changed: 16 additions & 16 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,11 +16,10 @@
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

22+
import org.springframework.util.ObjectUtils;
2423
import org.springframework.web.bind.WebDataBinder;
2524
import org.springframework.web.bind.annotation.InitBinder;
2625
import org.springframework.web.bind.support.DefaultDataBinderFactory;
@@ -53,32 +52,33 @@ public InitBinderDataBinderFactory(List<InvocableHandlerMethod> binderMethods, W
5352

5453
/**
5554
* Initialize a WebDataBinder with {@code @InitBinder} methods.
56-
* If the {@code @InitBinder} annotation specifies attributes names, it is
57-
* invoked only if the names include the target object name.
58-
* @throws Exception if one of the invoked @{@link InitBinder} methods fail.
55+
* <p>If the {@code @InitBinder} annotation specifies attributes names,
56+
* it is invoked only if the names include the target object name.
57+
* @throws Exception if one of the invoked @{@link InitBinder} methods fails
58+
* @see #isBinderMethodApplicable
5959
*/
6060
@Override
61-
public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {
61+
public void initBinder(WebDataBinder dataBinder, NativeWebRequest request) throws Exception {
6262
for (InvocableHandlerMethod binderMethod : this.binderMethods) {
63-
if (isBinderMethodApplicable(binderMethod, binder)) {
64-
Object returnValue = binderMethod.invokeForRequest(request, null, binder);
63+
if (isBinderMethodApplicable(binderMethod, dataBinder)) {
64+
Object returnValue = binderMethod.invokeForRequest(request, null, dataBinder);
6565
if (returnValue != null) {
6666
throw new IllegalStateException(
67-
"@InitBinder methods should return void: " + binderMethod);
67+
"@InitBinder methods must not return a value (should be void): " + binderMethod);
6868
}
6969
}
7070
}
7171
}
7272

7373
/**
74-
* Whether the given {@code @InitBinder} method should be used to initialize
75-
* the given WebDataBinder instance. By default we check the attributes
76-
* names of the annotation, if present.
74+
* Determine whether the given {@code @InitBinder} method should be used
75+
* to initialize the given {@link WebDataBinder} instance. By default we
76+
* check the specified attribute names in the annotation value, if any.
7777
*/
78-
protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder binder) {
78+
protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder dataBinder) {
7979
InitBinder ann = initBinderMethod.getMethodAnnotation(InitBinder.class);
80-
Collection<String> names = Arrays.asList(ann.value());
81-
return (names.isEmpty() || names.contains(binder.getObjectName()));
80+
String[] names = ann.value();
81+
return (ObjectUtils.isEmpty(names) || ObjectUtils.containsElement(names, dataBinder.getObjectName()));
8282
}
8383

8484
}

0 commit comments

Comments
 (0)