From 840ae0594250c7946c7050c438bcf797a162d61e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 6 Jun 2020 16:57:28 +0200 Subject: [PATCH] Polishing --- .../annotation/ReflectiveAspectJAdvisorFactory.java | 13 +++++++------ .../autoproxy/AspectJPrecedenceComparator.java | 12 +++--------- .../http/server/reactive/MockServerHttpRequest.java | 11 +++-------- .../http/codec/json/Jackson2CodecSupport.java | 3 +-- .../reactive/DefaultServerHttpRequestBuilder.java | 8 ++++---- .../server/reactive/ReactorServerHttpRequest.java | 3 ++- .../server/reactive/ServerHttpRequestDecorator.java | 5 +++-- .../server/reactive/ServletServerHttpRequest.java | 5 ++++- .../server/reactive/UndertowServerHttpRequest.java | 3 ++- .../server/reactive/test/MockServerHttpRequest.java | 13 ++++--------- .../config/DelegatingWebFluxConfiguration.java | 5 ++++- .../function/client/DefaultWebClientBuilder.java | 4 +--- 12 files changed, 38 insertions(+), 47 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java index 760c6dc976..db6898f310 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -76,9 +76,8 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto new InstanceComparator<>( Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class), (Converter) method -> { - AspectJAnnotation annotation = - AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method); - return (annotation != null ? annotation.getAnnotation() : null); + AspectJAnnotation ann = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method); + return (ann != null ? ann.getAnnotation() : null); }); Comparator methodNameComparator = new ConvertingComparator<>(Method::getName); METHOD_COMPARATOR = adviceKindComparator.thenComparing(methodNameComparator); @@ -153,8 +152,10 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) { methods.add(method); } - }); - methods.sort(METHOD_COMPARATOR); + }, ReflectionUtils.USER_DECLARED_METHODS); + if (methods.size() > 1) { + methods.sort(METHOD_COMPARATOR); + } return methods; } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java index 64066f7c04..d013cda2c9 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -138,14 +138,8 @@ class AspectJPrecedenceComparator implements Comparator { } private int getAspectDeclarationOrder(Advisor anAdvisor) { - AspectJPrecedenceInformation precedenceInfo = - AspectJAopUtils.getAspectJPrecedenceInformationFor(anAdvisor); - if (precedenceInfo != null) { - return precedenceInfo.getDeclarationOrder(); - } - else { - return 0; - } + AspectJPrecedenceInformation precedenceInfo = AspectJAopUtils.getAspectJPrecedenceInformationFor(anAdvisor); + return (precedenceInfo != null ? precedenceInfo.getDeclarationOrder() : 0); } } diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java index f94bacde93..eb84a91368 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -97,8 +97,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { return this.remoteAddress; } - @Nullable @Override + @Nullable protected SslInfo initSslInfo() { return this.sslInfo; } @@ -342,9 +342,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { * @see BodyBuilder#body(String) */ MockServerHttpRequest build(); - } + /** * A builder that adds a body to the request. */ @@ -383,7 +383,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { * @return the built request entity */ MockServerHttpRequest body(String body); - } @@ -391,7 +390,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory(); - private final HttpMethod method; private final URI url; @@ -411,7 +409,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { @Nullable private SslInfo sslInfo; - public DefaultBodyBuilder(HttpMethod method, URI url) { this.method = method; this.url = url; @@ -558,11 +555,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private URI getUrlToUse() { MultiValueMap params = this.queryParamsBuilder.buildAndExpand().encode().getQueryParams(); - if (!params.isEmpty()) { return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri(); } - return this.url; } } diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java index fc256206f0..5e89310a25 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java @@ -18,12 +18,11 @@ package org.springframework.http.codec.json; import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.nio.charset.StandardCharsets; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java index 45edd3e0b6..8ffce59844 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -111,7 +111,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { } @Override - @SuppressWarnings("deprecation") + @Deprecated public ServerHttpRequest.Builder header(String key, String value) { this.httpHeaders.add(key, value); return this; @@ -215,14 +215,14 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { return this.cookies; } - @Nullable @Override + @Nullable public InetSocketAddress getRemoteAddress() { return this.remoteAddress; } - @Nullable @Override + @Nullable protected SslInfo initSslInfo() { return this.sslInfo; } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java index c6d60c01ab..606c668200 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -92,6 +92,7 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest { } else { InetSocketAddress localAddress = request.hostAddress(); + Assert.state(localAddress != null, "No host address available"); return new URI(scheme, null, localAddress.getHostString(), localAddress.getPort(), null, null, null); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java index c24f79ef4f..f8c26b4271 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -97,12 +97,13 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest { } @Override + @Nullable public InetSocketAddress getRemoteAddress() { return getDelegate().getRemoteAddress(); } - @Nullable @Override + @Nullable public SslInfo getSslInfo() { return getDelegate().getSslInfo(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java index 1d7c55ada0..e29a79a3b3 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -42,6 +42,7 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; @@ -174,10 +175,12 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest { } @Override + @NonNull public InetSocketAddress getRemoteAddress() { return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort()); } + @Override @Nullable protected SslInfo initSslInfo() { X509Certificate[] certificates = getX509Certificates(); diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java index c534e0cbba..d01600eabb 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -101,6 +101,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { } @Override + @Nullable public InetSocketAddress getRemoteAddress() { return this.exchange.getSourceAddress(); } diff --git a/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java b/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java index 1615258e46..6b09e8830a 100644 --- a/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -52,7 +52,7 @@ import org.springframework.web.util.UriComponentsBuilder; * @author Rossen Stoyanchev * @since 5.0 */ -public class MockServerHttpRequest extends AbstractServerHttpRequest { +public final class MockServerHttpRequest extends AbstractServerHttpRequest { private final HttpMethod httpMethod; @@ -97,8 +97,8 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest { return this.remoteAddress; } - @Nullable @Override + @Nullable protected SslInfo initSslInfo() { return this.sslInfo; } @@ -342,9 +342,9 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest { * @see BodyBuilder#body(String) */ MockServerHttpRequest build(); - } + /** * A builder that adds a body to the request. */ @@ -383,7 +383,6 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest { * @return the built request entity */ MockServerHttpRequest body(String body); - } @@ -391,7 +390,6 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest { private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory(); - private final HttpMethod method; private final URI url; @@ -411,7 +409,6 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest { @Nullable private SslInfo sslInfo; - public DefaultBodyBuilder(HttpMethod method, URI url) { this.method = method; this.url = url; @@ -558,11 +555,9 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest { private URI getUrlToUse() { MultiValueMap params = this.queryParamsBuilder.buildAndExpand().encode().getQueryParams(); - if (!params.isEmpty()) { return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri(); } - return this.url; } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.java index 65601ad6b4..f754040fa7 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -42,6 +42,7 @@ public class DelegatingWebFluxConfiguration extends WebFluxConfigurationSupport private final WebFluxConfigurerComposite configurers = new WebFluxConfigurerComposite(); + @Autowired(required = false) public void setConfigurers(List configurers) { if (!CollectionUtils.isEmpty(configurers)) { @@ -49,6 +50,7 @@ public class DelegatingWebFluxConfiguration extends WebFluxConfigurationSupport } } + @Override protected void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) { this.configurers.configureContentTypeResolver(builder); @@ -100,4 +102,5 @@ public class DelegatingWebFluxConfiguration extends WebFluxConfigurationSupport protected void configureViewResolvers(ViewResolverRegistry registry) { this.configurers.configureViewResolvers(registry); } + } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java index dfd1087598..98733a3e78 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java @@ -223,8 +223,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder { return this; } - @SuppressWarnings("deprecation") @Override + @Deprecated public WebClient.Builder exchangeStrategies(Consumer configurer) { if (this.strategiesConfigurers == null) { this.strategiesConfigurers = new ArrayList<>(4); @@ -282,10 +282,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder { if (CollectionUtils.isEmpty(this.strategiesConfigurers)) { return this.strategies != null ? this.strategies : ExchangeStrategies.withDefaults(); } - ExchangeStrategies.Builder builder = this.strategies != null ? this.strategies.mutate() : ExchangeStrategies.builder(); - this.strategiesConfigurers.forEach(configurer -> configurer.accept(builder)); return builder.build(); }