Polishing

This commit is contained in:
Juergen Hoeller
2018-01-12 18:24:00 +01:00
parent 06e6386dc9
commit 4c9ed0d87e
4 changed files with 26 additions and 35 deletions

View File

@@ -806,7 +806,7 @@ public class BridgeMethodResolverTests {
}
@SuppressWarnings("unchecked")
@SuppressWarnings({"serial", "unchecked"})
public static class MessageBroadcasterImpl extends GenericEventBroadcasterImpl<MessageEvent>
implements Serializable, // implement an unrelated interface first (SPR-16288)
MessageBroadcaster {

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.
@@ -106,8 +106,8 @@ public class MethodJmsListenerEndpoint extends AbstractJmsListenerEndpoint imple
}
Method method = getMethod();
if (method != null && AopUtils.isAopProxy(this.bean)) {
Class<?> target = AopProxyUtils.ultimateTargetClass(this.bean);
return AopUtils.getMostSpecificMethod(method, target);
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(this.bean);
return AopUtils.getMostSpecificMethod(method, targetClass);
}
else {
return method;

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.
@@ -51,13 +51,11 @@ public class ErrorsMethodArgumentResolver extends HandlerMethodArgumentResolverS
return checkParameterType(parameter, Errors.class::isAssignableFrom);
}
@Override
public Mono<Object> resolveArgument(
MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
Object errors = getErrors(parameter, context);
if (Mono.class.isAssignableFrom(errors.getClass())) {
return ((Mono<?>) errors).cast(Object.class);
}
@@ -65,35 +63,32 @@ public class ErrorsMethodArgumentResolver extends HandlerMethodArgumentResolverS
return Mono.just(errors);
}
else {
throw new IllegalStateException(
"Unexpected Errors/BindingResult type: " + errors.getClass().getName());
throw new IllegalStateException("Unexpected Errors/BindingResult type: " + errors.getClass().getName());
}
}
private Object getErrors(MethodParameter parameter, BindingContext context) {
Assert.isTrue(parameter.getParameterIndex() > 0,
"Errors argument must be immediately after a model attribute argument");
"Errors argument must be declared immediately after a model attribute argument");
int index = parameter.getParameterIndex() - 1;
MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());
Assert.isNull(adapter, "An @ModelAttribute and an Errors/BindingResult) arguments " +
Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
"cannot both be declared with an async type wrapper. " +
"Either declare the @ModelAttribute without an async wrapper type or " +
"handle a WebExchangeBindException error signal through the async type.");
ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class);
String name = (annot != null && StringUtils.hasText(annot.value()) ?
annot.value() : Conventions.getVariableNameForParameter(attributeParam));
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
String name = (ann != null && StringUtils.hasText(ann.value()) ?
ann.value() : Conventions.getVariableNameForParameter(attributeParam));
Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);
Assert.notNull(errors, "An Errors/BindingResult argument is expected " +
Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +
"immediately after the @ModelAttribute argument to which it applies. " +
"For @RequestBody and @RequestPart arguments, please declare them with a reactive type wrapper " +
"and use its onError operators to handle WebExchangeBindException: " +
"For @RequestBody and @RequestPart arguments, please declare them with a reactive " +
"type wrapper and use its onError operators to handle WebExchangeBindException: " +
parameter.getMethod());
return errors;

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.
@@ -36,12 +36,11 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.method.ResolvableMethod;
import org.springframework.web.reactive.BindingContext;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* Unit tests for {@link ErrorsMethodArgumentResolver}.
*
* @author Rossen Stoyanchev
*/
public class ErrorsMethodArgumentResolverTests {
@@ -61,7 +60,7 @@ public class ErrorsMethodArgumentResolverTests {
@Test
public void supports() throws Exception {
public void supports() {
MethodParameter parameter = this.testMethod.arg(Errors.class);
assertTrue(this.resolver.supportsParameter(parameter));
@@ -76,8 +75,7 @@ public class ErrorsMethodArgumentResolverTests {
}
@Test
public void resolve() throws Exception {
public void resolve() {
BindingResult bindingResult = createBindingResult(new Foo(), "foo");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "foo", bindingResult);
@@ -94,8 +92,7 @@ public class ErrorsMethodArgumentResolverTests {
}
@Test
public void resolveWithMono() throws Exception {
public void resolveWithMono() {
BindingResult bindingResult = createBindingResult(new Foo(), "foo");
MonoProcessor<BindingResult> monoProcessor = MonoProcessor.create();
monoProcessor.onNext(bindingResult);
@@ -109,9 +106,8 @@ public class ErrorsMethodArgumentResolverTests {
}
@Test
public void resolveWithMonoOnBindingResultAndModelAttribute() throws Exception {
this.expectedException.expectMessage("An @ModelAttribute and an Errors/BindingResult) arguments " +
public void resolveWithMonoOnBindingResultAndModelAttribute() {
this.expectedException.expectMessage("An @ModelAttribute and an Errors/BindingResult argument " +
"cannot both be declared with an async type wrapper.");
MethodParameter parameter = this.testMethod.arg(BindingResult.class);
@@ -119,9 +115,8 @@ public class ErrorsMethodArgumentResolverTests {
.block(Duration.ofMillis(5000));
}
@Test // SPR-16187
public void resolveWithBindingResultNotFound() throws Exception {
@Test // SPR-16187
public void resolveWithBindingResultNotFound() {
this.expectedException.expectMessage("An Errors/BindingResult argument is expected " +
"immediately after the @ModelAttribute argument");
@@ -159,6 +154,7 @@ public class ErrorsMethodArgumentResolverTests {
@ModelAttribute Mono<Foo> fooMono,
BindingResult bindingResult,
Mono<Errors> errorsMono,
String string) {}
String string) {
}
}