From 8135ae6d38b19f0e15d792709331cc17ce5a6701 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 17 Oct 2021 13:30:05 +0200 Subject: [PATCH] Apply "instanceof pattern matching" in spring-webflux This commit also applies additional clean-up tasks such as the following. - final fields - diamond operator (<>) for anonymous inner classes - multi-catch This has only been applied to `src/main/java`. --- .../client/DefaultClientResponseBuilder.java | 8 +++---- .../client/ExchangeFilterFunctions.java | 8 +++---- .../ChangePathPatternParserVisitor.java | 5 ++--- .../server/DefaultEntityResponseBuilder.java | 2 +- .../server/DefaultServerRequestBuilder.java | 4 ++-- .../server/DefaultServerResponseBuilder.java | 5 ++--- .../handler/AbstractUrlHandlerMapping.java | 6 ++---- .../resource/CssLinkResourceTransformer.java | 5 ++--- .../resource/PathResourceResolver.java | 21 +++++++++---------- .../resource/ResourceUrlProvider.java | 3 +-- .../reactive/resource/ResourceWebHandler.java | 13 ++++-------- .../resource/VersionResourceResolver.java | 8 +++---- .../method/AbstractHandlerMethodMapping.java | 13 ++++++------ .../result/method/InvocableHandlerMethod.java | 3 +-- .../result/method/RequestMappingInfo.java | 3 +-- .../reactive/result/view/RequestContext.java | 16 +++++++------- .../result/view/ViewResolverSupport.java | 4 ++-- .../web/reactive/socket/CloseStatus.java | 5 ++--- .../web/reactive/socket/WebSocketMessage.java | 5 ++--- .../StandardWebSocketHandlerAdapter.java | 17 ++++++++------- 20 files changed, 68 insertions(+), 86 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java index 055a1d9ed5c..a63ba76bf7e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -66,7 +66,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { }; - private ExchangeStrategies strategies; + private final ExchangeStrategies strategies; private int statusCode = 200; @@ -104,8 +104,8 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { this.headers.addAll(other.headers().asHttpHeaders()); } this.originalResponse = other; - this.request = (other instanceof DefaultClientResponse ? - ((DefaultClientResponse) other).request() : EMPTY_REQUEST); + this.request = (other instanceof DefaultClientResponse defaultClientResponse ? + defaultClientResponse.request() : EMPTY_REQUEST); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java index b7d6c3d7200..42ad43e22c4 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -116,8 +116,7 @@ public abstract class ExchangeFilterFunctions { public static ExchangeFilterFunction basicAuthentication() { return (request, next) -> { Object attr = request.attributes().get(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE); - if (attr instanceof Credentials) { - Credentials cred = (Credentials) attr; + if (attr instanceof Credentials cred) { return next.exchange(ClientRequest.from(request) .headers(headers -> headers.setBasicAuth(cred.username, cred.password)) .build()); @@ -174,10 +173,9 @@ public abstract class ExchangeFilterFunctions { if (this == other) { return true; } - if (!(other instanceof Credentials)) { + if (!(other instanceof Credentials otherCred)) { return false; } - Credentials otherCred = (Credentials) other; return (this.username.equals(otherCred.username) && this.password.equals(otherCred.password)); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ChangePathPatternParserVisitor.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ChangePathPatternParserVisitor.java index 10f34a3e765..dc971a467ac 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ChangePathPatternParserVisitor.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ChangePathPatternParserVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -70,8 +70,7 @@ class ChangePathPatternParserVisitor implements RouterFunctions.Visitor { } private void changeParser(RequestPredicate predicate) { - if (predicate instanceof Target) { - Target target = (Target) predicate; + if (predicate instanceof Target target) { target.changeParser(this.parser); } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java index d43a4e71f1a..461a82efd28 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java @@ -195,7 +195,7 @@ class DefaultEntityResponseBuilder implements EntityResponse.Builder { @Override public Mono> build() { - return Mono.just(new DefaultEntityResponse( + return Mono.just(new DefaultEntityResponse<>( this.status, this.headers, this.cookies, this.entity, this.inserter, this.hints)); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java index 940118866f4..174899b8894 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -69,7 +69,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { private final List> messageReaders; - private ServerWebExchange exchange; + private final ServerWebExchange exchange; private String methodName; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java index da71fbe565a..cf4452b60ea 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -78,8 +78,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { Assert.notNull(other, "ServerResponse must not be null"); this.headers.addAll(other.headers()); this.cookies.addAll(other.cookies()); - if (other instanceof AbstractServerResponse) { - AbstractServerResponse abstractOther = (AbstractServerResponse) other; + if (other instanceof AbstractServerResponse abstractOther) { this.statusCode = abstractOther.statusCode; this.hints.putAll(abstractOther.hints); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java index abaf4cf86f9..417d7f47c8f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java @@ -153,8 +153,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping { Object handler = this.handlerMap.get(pattern); // Bean name or resolved handler? - if (handler instanceof String) { - String handlerName = (String) handler; + if (handler instanceof String handlerName) { handler = obtainApplicationContext().getBean(handlerName); } @@ -223,8 +222,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping { } // Eagerly resolve handler if referencing singleton via name. - if (!this.lazyInitHandlers && handler instanceof String) { - String handlerName = (String) handler; + if (!this.lazyInitHandlers && handler instanceof String handlerName) { if (obtainApplicationContext().isSingleton(handlerName)) { resolvedHandler = obtainApplicationContext().getBean(handlerName); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java index 3d96ff0b571..ab0dfd4c6c7 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -288,10 +288,9 @@ public class CssLinkResourceTransformer extends ResourceTransformerSupport { if (this == other) { return true; } - if (!(other instanceof ContentChunkInfo)) { + if (!(other instanceof ContentChunkInfo otherCci)) { return false; } - ContentChunkInfo otherCci = (ContentChunkInfo) other; return (this.start == otherCci.start && this.end == otherCci.end); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java index dd86b8e9a26..e99c3978790 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java @@ -42,6 +42,7 @@ import org.springframework.web.util.UriUtils; * expected to be configured at the end in a chain of resolvers. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 5.0 */ public class PathResourceResolver extends AbstractResourceResolver { @@ -119,11 +120,12 @@ public class PathResourceResolver extends AbstractResourceResolver { return Mono.just(resource); } else if (logger.isWarnEnabled()) { - Resource[] allowedLocations = getAllowedLocations(); - logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " + - "but resource \"" + resource.getURL() + "\" is neither under the " + - "current location \"" + location.getURL() + "\" nor under any of the " + - "allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]")); + Object allowedLocationsText = (getAllowedLocations() != null ? Arrays.asList(getAllowedLocations()) : "[]"); + logger.warn(""" + Resource path "%s" was successfully resolved, but resource \ + "%s" is neither under the current location "%s" nor under any \ + of the allowed locations %s"\ + """.formatted(resourcePath, resource.getURL(),location.getURL(), allowedLocationsText)); } } return Mono.empty(); @@ -177,8 +179,8 @@ public class PathResourceResolver extends AbstractResourceResolver { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } - else if (resource instanceof ClassPathResource) { - resourcePath = ((ClassPathResource) resource).getPath(); + else if (resource instanceof ClassPathResource classPathResource) { + resourcePath = classPathResource.getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else { @@ -203,10 +205,7 @@ public class PathResourceResolver extends AbstractResourceResolver { return true; } } - catch (IllegalArgumentException ex) { - // May not be possible to decode... - } - catch (UnsupportedEncodingException ex) { + catch (IllegalArgumentException | UnsupportedEncodingException ex) { // Should never happen... } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java index b78d8532008..8f3e3b0bbf0 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java @@ -106,8 +106,7 @@ public class ResourceUrlProvider implements ApplicationListener mapping.getHandlerMap().forEach((pattern, handler) -> { - if (handler instanceof ResourceWebHandler) { - ResourceWebHandler resourceHandler = (ResourceWebHandler) handler; + if (handler instanceof ResourceWebHandler resourceHandler) { this.handlerMap.put(pattern, resourceHandler); } })); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java index 75ac8ddd320..50fe84bf201 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java @@ -354,8 +354,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { return; } for (int i = getResourceResolvers().size() - 1; i >= 0; i--) { - if (getResourceResolvers().get(i) instanceof PathResourceResolver) { - PathResourceResolver resolver = (PathResourceResolver) getResourceResolvers().get(i); + if (getResourceResolvers().get(i) instanceof PathResourceResolver resolver) { if (ObjectUtils.isEmpty(resolver.getAllowedLocations())) { resolver.setAllowedLocations(getLocations().toArray(new Resource[0])); } @@ -522,10 +521,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { return true; } } - catch (IllegalArgumentException ex) { - // May not be possible to decode... - } - catch (UnsupportedEncodingException ex) { + catch (IllegalArgumentException | UnsupportedEncodingException ex) { // Should never happen... } } @@ -609,9 +605,8 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { headers.setContentType(mediaType); } - if (resource instanceof HttpResource) { - HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders(); - exchange.getResponse().getHeaders().putAll(resourceHeaders); + if (resource instanceof HttpResource httpResource) { + exchange.getResponse().getHeaders().putAll(httpResource.getResponseHeaders()); } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java index ff712f7068b..d923e96024c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -65,7 +65,7 @@ import org.springframework.web.server.ServerWebExchange; */ public class VersionResourceResolver extends AbstractResourceResolver { - private AntPathMatcher pathMatcher = new AntPathMatcher(); + private final AntPathMatcher pathMatcher = new AntPathMatcher(); /** Map from path pattern -> VersionStrategy. */ private final Map versionStrategyMap = new LinkedHashMap<>(); @@ -315,8 +315,8 @@ public class VersionResourceResolver extends AbstractResourceResolver { @Override public HttpHeaders getResponseHeaders() { - HttpHeaders headers = (this.original instanceof HttpResource ? - ((HttpResource) this.original).getResponseHeaders() : new HttpHeaders()); + HttpHeaders headers = (this.original instanceof HttpResource httpResource ? + httpResource.getResponseHeaders() : new HttpHeaders()); headers.setETag("W/\"" + this.version + "\""); return headers; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java index 7d59209955d..f1210150d0f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java @@ -200,8 +200,8 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap * @param handler the bean name of a handler or a handler instance */ protected void detectHandlerMethods(final Object handler) { - Class handlerType = (handler instanceof String ? - obtainApplicationContext().getType((String) handler) : handler.getClass()); + Class handlerType = (handler instanceof String beanName ? + obtainApplicationContext().getType(beanName) : handler.getClass()); if (handlerType != null) { final Class userType = ClassUtils.getUserClass(handlerType); @@ -258,8 +258,8 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap * @return the created HandlerMethod */ protected HandlerMethod createHandlerMethod(Object handler, Method method) { - if (handler instanceof String) { - return new HandlerMethod((String) handler, + if (handler instanceof String beanName) { + return new HandlerMethod(beanName, obtainApplicationContext().getAutowireCapableBeanFactory(), obtainApplicationContext(), method); @@ -397,14 +397,13 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap @Override protected boolean hasCorsConfigurationSource(Object handler) { return super.hasCorsConfigurationSource(handler) || - (handler instanceof HandlerMethod && this.mappingRegistry.getCorsConfiguration((HandlerMethod) handler) != null); + (handler instanceof HandlerMethod handlerMethod && this.mappingRegistry.getCorsConfiguration(handlerMethod) != null); } @Override protected CorsConfiguration getCorsConfiguration(Object handler, ServerWebExchange exchange) { CorsConfiguration corsConfig = super.getCorsConfiguration(handler, exchange); - if (handler instanceof HandlerMethod) { - HandlerMethod handlerMethod = (HandlerMethod) handler; + if (handler instanceof HandlerMethod handlerMethod) { if (handlerMethod.equals(PREFLIGHT_AMBIGUOUS_MATCH)) { return ALLOW_CORS_CONFIG; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java index b117a197aa8..4a2fee3ef11 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java @@ -224,8 +224,7 @@ public class InvocableHandlerMethod extends HandlerMethod { return true; } Type parameterType = returnType.getGenericParameterType(); - if (parameterType instanceof ParameterizedType) { - ParameterizedType type = (ParameterizedType) parameterType; + if (parameterType instanceof ParameterizedType type) { if (type.getActualTypeArguments().length == 1) { return Void.class.equals(type.getActualTypeArguments()[0]); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java index 8782b9a5b83..2842d1213f4 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java @@ -343,10 +343,9 @@ public final class RequestMappingInfo implements RequestCondition errorsMap; @Nullable - private RequestDataValueProcessor dataValueProcessor; + private final RequestDataValueProcessor dataValueProcessor; public RequestContext(ServerWebExchange exchange, Map model, MessageSource messageSource) { @@ -92,8 +92,8 @@ public class RequestContext { LocaleContext localeContext = exchange.getLocaleContext(); Locale locale = localeContext.getLocale(); this.locale = (locale != null ? locale : Locale.getDefault()); - TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? - ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); + TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext tzaLocaleContext ? + tzaLocaleContext.getTimeZone() : null); this.timeZone = (timeZone != null ? timeZone : TimeZone.getDefault()); this.defaultHtmlEscape = null; // TODO @@ -385,15 +385,15 @@ public class RequestContext { } } - if (errors instanceof BindException) { - errors = ((BindException) errors).getBindingResult(); + if (errors instanceof BindException bindException) { + errors = bindException.getBindingResult(); } if (htmlEscape && !(errors instanceof EscapedErrors)) { errors = new EscapedErrors(errors); } - else if (!htmlEscape && errors instanceof EscapedErrors) { - errors = ((EscapedErrors) errors).getSource(); + else if (!htmlEscape && errors instanceof EscapedErrors escapedErrors) { + errors = escapedErrors.getSource(); } this.errorsMap.put(name, errors); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolverSupport.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolverSupport.java index e97a983af5c..6d0db61d149 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolverSupport.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolverSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -40,7 +40,7 @@ public abstract class ViewResolverSupport implements Ordered { public static final MediaType DEFAULT_CONTENT_TYPE = MediaType.parseMediaType("text/html;charset=UTF-8"); - private List mediaTypes = new ArrayList<>(4); + private final List mediaTypes = new ArrayList<>(4); private Charset defaultCharset = StandardCharsets.UTF_8; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/CloseStatus.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/CloseStatus.java index 304eb797d79..603f2ba1656 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/CloseStatus.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/CloseStatus.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -239,10 +239,9 @@ public final class CloseStatus { if (this == other) { return true; } - if (!(other instanceof CloseStatus)) { + if (!(other instanceof CloseStatus otherStatus)) { return false; } - CloseStatus otherStatus = (CloseStatus) other; return (this.code == otherStatus.code && ObjectUtils.nullSafeEquals(this.reason, otherStatus.reason)); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java index 32175f160b5..b7d4de6937d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -151,10 +151,9 @@ public class WebSocketMessage { if (this == other) { return true; } - if (!(other instanceof WebSocketMessage)) { + if (!(other instanceof WebSocketMessage otherMessage)) { return false; } - WebSocketMessage otherMessage = (WebSocketMessage) other; return (this.type.equals(otherMessage.type) && ObjectUtils.nullSafeEquals(this.payload, otherMessage.payload)); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/StandardWebSocketHandlerAdapter.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/StandardWebSocketHandlerAdapter.java index d4699ca32e0..68fed3f2b5d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/StandardWebSocketHandlerAdapter.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/StandardWebSocketHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -41,13 +41,14 @@ import org.springframework.web.reactive.socket.WebSocketSession; * * @author Violeta Georgieva * @author Rossen Stoyanchev + * @author Sam Brannen * @since 5.0 */ public class StandardWebSocketHandlerAdapter extends Endpoint { private final WebSocketHandler delegateHandler; - private Function sessionFactory; + private final Function sessionFactory; @Nullable private StandardWebSocketSession delegateSession; @@ -89,16 +90,16 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { private WebSocketMessage toMessage(T message) { WebSocketSession session = this.delegateSession; Assert.state(session != null, "Cannot create message without a session"); - if (message instanceof String) { - byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8); + if (message instanceof String text) { + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); return new WebSocketMessage(Type.TEXT, session.bufferFactory().wrap(bytes)); } - else if (message instanceof ByteBuffer) { - DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message); + else if (message instanceof ByteBuffer byteBuffer) { + DataBuffer buffer = session.bufferFactory().wrap(byteBuffer); return new WebSocketMessage(Type.BINARY, buffer); } - else if (message instanceof PongMessage) { - DataBuffer buffer = session.bufferFactory().wrap(((PongMessage) message).getApplicationData()); + else if (message instanceof PongMessage pongMessage) { + DataBuffer buffer = session.bufferFactory().wrap(pongMessage.getApplicationData()); return new WebSocketMessage(Type.PONG, buffer); } else {