Polishing

This commit is contained in:
Juergen Hoeller
2018-08-02 17:39:39 +02:00
parent 13637ec3b5
commit 75b323e6c2
7 changed files with 177 additions and 184 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,10 @@
package org.springframework.web.method.annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.support.DefaultDataBinderFactory;
@@ -53,32 +52,33 @@ public class InitBinderDataBinderFactory extends DefaultDataBinderFactory {
/**
* Initialize a WebDataBinder with {@code @InitBinder} methods.
* If the {@code @InitBinder} annotation specifies attributes names, it is
* invoked only if the names include the target object name.
* @throws Exception if one of the invoked @{@link InitBinder} methods fail.
* <p>If the {@code @InitBinder} annotation specifies attributes names,
* it is invoked only if the names include the target object name.
* @throws Exception if one of the invoked @{@link InitBinder} methods fails
* @see #isBinderMethodApplicable
*/
@Override
public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {
public void initBinder(WebDataBinder dataBinder, NativeWebRequest request) throws Exception {
for (InvocableHandlerMethod binderMethod : this.binderMethods) {
if (isBinderMethodApplicable(binderMethod, binder)) {
Object returnValue = binderMethod.invokeForRequest(request, null, binder);
if (isBinderMethodApplicable(binderMethod, dataBinder)) {
Object returnValue = binderMethod.invokeForRequest(request, null, dataBinder);
if (returnValue != null) {
throw new IllegalStateException(
"@InitBinder methods should return void: " + binderMethod);
"@InitBinder methods must not return a value (should be void): " + binderMethod);
}
}
}
}
/**
* Whether the given {@code @InitBinder} method should be used to initialize
* the given WebDataBinder instance. By default we check the attributes
* names of the annotation, if present.
* Determine whether the given {@code @InitBinder} method should be used
* to initialize the given {@link WebDataBinder} instance. By default we
* check the specified attribute names in the annotation value, if any.
*/
protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder binder) {
protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder dataBinder) {
InitBinder ann = initBinderMethod.getMethodAnnotation(InitBinder.class);
Collection<String> names = Arrays.asList(ann.value());
return (names.isEmpty() || names.contains(binder.getObjectName()));
String[] names = ann.value();
return (ObjectUtils.isEmpty(names) || ObjectUtils.containsElement(names, dataBinder.getObjectName()));
}
}