From 4c9ed0d87edc23a49dcc84499246c80a2070345d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 12 Jan 2018 18:24:00 +0100 Subject: [PATCH] Polishing --- .../core/BridgeMethodResolverTests.java | 2 +- .../jms/config/MethodJmsListenerEndpoint.java | 6 ++-- .../ErrorsMethodArgumentResolver.java | 25 +++++++---------- .../ErrorsMethodArgumentResolverTests.java | 28 ++++++++----------- 4 files changed, 26 insertions(+), 35 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/core/BridgeMethodResolverTests.java b/spring-core/src/test/java/org/springframework/core/BridgeMethodResolverTests.java index a918d764f2..1838bf2d21 100644 --- a/spring-core/src/test/java/org/springframework/core/BridgeMethodResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/BridgeMethodResolverTests.java @@ -806,7 +806,7 @@ public class BridgeMethodResolverTests { } - @SuppressWarnings("unchecked") + @SuppressWarnings({"serial", "unchecked"}) public static class MessageBroadcasterImpl extends GenericEventBroadcasterImpl implements Serializable, // implement an unrelated interface first (SPR-16288) MessageBroadcaster { diff --git a/spring-jms/src/main/java/org/springframework/jms/config/MethodJmsListenerEndpoint.java b/spring-jms/src/main/java/org/springframework/jms/config/MethodJmsListenerEndpoint.java index d814faeb8b..bc35694d3c 100644 --- a/spring-jms/src/main/java/org/springframework/jms/config/MethodJmsListenerEndpoint.java +++ b/spring-jms/src/main/java/org/springframework/jms/config/MethodJmsListenerEndpoint.java @@ -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; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolver.java index 9cd88c005a..8fd64bc796 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolver.java @@ -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 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; diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolverTests.java index 5bfcd7719a..445353933e 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolverTests.java @@ -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 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 fooMono, BindingResult bindingResult, Mono errorsMono, - String string) {} + String string) { + } }