From b51813e40853af56ffeca37db08408d02d6c60a5 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 17 Oct 2021 19:04:32 +0200 Subject: [PATCH] Apply "instanceof pattern matching" in spring-websocket This commit also applies additional clean-up tasks such as the following. - final fields - diamond operator (<>) for anonymous inner classes This has only been applied to `src/main/java`. --- .../web/socket/CloseStatus.java | 5 ++--- .../web/socket/WebSocketHttpHeaders.java | 5 ++--- .../client/WebSocketConnectionManager.java | 13 ++++++------ .../messaging/DefaultSimpUserRegistry.java | 14 ++++++------- .../HttpSessionHandshakeInterceptor.java | 9 ++++---- .../client/DefaultTransportRequest.java | 4 ++-- .../socket/sockjs/client/SockJsClient.java | 21 +++++++------------ .../sockjs/client/UndertowXhrTransport.java | 8 +++---- .../sockjs/client/XhrClientSockJsSession.java | 6 +++--- .../web/socket/sockjs/frame/SockJsFrame.java | 5 ++--- 10 files changed, 41 insertions(+), 49 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java b/spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java index 493f009283..fe9972e7c8 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/CloseStatus.java +++ b/spring-websocket/src/main/java/org/springframework/web/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. @@ -209,10 +209,9 @@ public final class CloseStatus implements Serializable { 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-websocket/src/main/java/org/springframework/web/socket/WebSocketHttpHeaders.java b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketHttpHeaders.java index 84a3503bfc..cb30c423bb 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketHttpHeaders.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketHttpHeaders.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. @@ -311,10 +311,9 @@ public class WebSocketHttpHeaders extends HttpHeaders { if (this == other) { return true; } - if (!(other instanceof WebSocketHttpHeaders)) { + if (!(other instanceof WebSocketHttpHeaders otherHeaders)) { return false; } - WebSocketHttpHeaders otherHeaders = (WebSocketHttpHeaders) other; return this.headers.equals(otherHeaders.headers); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/client/WebSocketConnectionManager.java b/spring-websocket/src/main/java/org/springframework/web/socket/client/WebSocketConnectionManager.java index 989100f935..c2ad134e8e 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/client/WebSocketConnectionManager.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/client/WebSocketConnectionManager.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. @@ -35,6 +35,7 @@ import org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator; * this will be done automatically when the Spring ApplicationContext is refreshed. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 4.0 */ public class WebSocketConnectionManager extends ConnectionManagerSupport { @@ -46,7 +47,7 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport { @Nullable private WebSocketSession webSocketSession; - private WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); + private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); public WebSocketConnectionManager(WebSocketClient client, @@ -116,16 +117,16 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport { @Override public void startInternal() { - if (this.client instanceof Lifecycle && !((Lifecycle) this.client).isRunning()) { - ((Lifecycle) this.client).start(); + if (this.client instanceof Lifecycle lifecycle && !lifecycle.isRunning()) { + lifecycle.start(); } super.startInternal(); } @Override public void stopInternal() throws Exception { - if (this.client instanceof Lifecycle && ((Lifecycle) this.client).isRunning()) { - ((Lifecycle) this.client).stop(); + if (this.client instanceof Lifecycle lifecycle && lifecycle.isRunning()) { + lifecycle.stop(); } super.stopInternal(); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/DefaultSimpUserRegistry.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/DefaultSimpUserRegistry.java index 5bef6ac61f..ba9d1f9e54 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/DefaultSimpUserRegistry.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/DefaultSimpUserRegistry.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. @@ -43,6 +43,7 @@ import org.springframework.util.Assert; * track of connected users and their subscriptions. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 4.2 */ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicationListener { @@ -105,8 +106,8 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati return; } String name = user.getName(); - if (user instanceof DestinationUserNameProvider) { - name = ((DestinationUserNameProvider) user).getDestinationUserName(); + if (user instanceof DestinationUserNameProvider destinationUserNameProvider) { + name = destinationUserNameProvider.getDestinationUserName(); } synchronized (this.sessionLock) { LocalSimpUser simpUser = this.users.get(name); @@ -238,7 +239,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati @Override public boolean equals(@Nullable Object other) { return (this == other || - (other instanceof SimpUser && getName().equals(((SimpUser) other).getName()))); + (other instanceof SimpUser otherSimpUser && getName().equals(otherSimpUser.getName()))); } @Override @@ -294,7 +295,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati @Override public boolean equals(@Nullable Object other) { return (this == other || - (other instanceof SimpSubscription && getId().equals(((SimpSubscription) other).getId()))); + (other instanceof SimpSubscription otherSubscription && getId().equals(otherSubscription.getId()))); } @Override @@ -346,10 +347,9 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati if (this == other) { return true; } - if (!(other instanceof SimpSubscription)) { + if (!(other instanceof SimpSubscription otherSubscription)) { return false; } - SimpSubscription otherSubscription = (SimpSubscription) other; return (getId().equals(otherSubscription.getId()) && getSession().getId().equals(otherSubscription.getSession().getId())); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java index ba020c32fe..8ce26a3711 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -33,9 +33,9 @@ import org.springframework.web.socket.server.HandshakeInterceptor; /** * An interceptor to copy information from the HTTP session to the "handshake - * attributes" map to made available via{@link WebSocketSession#getAttributes()}. + * attributes" map to be made available via {@link WebSocketSession#getAttributes()}. * - *

Copies a subset or all HTTP session attributes and/or the HTTP session id + *

Copies a subset or all HTTP session attributes and/or the HTTP session ID * under the key {@link #HTTP_SESSION_ID_ATTR_NAME}. * * @author Rossen Stoyanchev @@ -164,8 +164,7 @@ public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor { @Nullable private HttpSession getSession(ServerHttpRequest request) { - if (request instanceof ServletServerHttpRequest) { - ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request; + if (request instanceof ServletServerHttpRequest serverRequest) { return serverRequest.getServletRequest().getSession(isCreateSession()); } return null; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/DefaultTransportRequest.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/DefaultTransportRequest.java index 32eb9c0f4c..e69f0e2ce6 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/DefaultTransportRequest.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/DefaultTransportRequest.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. @@ -59,7 +59,7 @@ class DefaultTransportRequest implements TransportRequest { private final TransportType serverTransportType; - private SockJsMessageCodec codec; + private final SockJsMessageCodec codec; @Nullable private Principal user; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java index 1ac4bc8fba..6f59483348 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.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. @@ -56,6 +56,7 @@ import org.springframework.web.util.UriComponentsBuilder; * the transports it is configured with. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 4.1 * @see https://github.com/sockjs/sockjs-client * @see org.springframework.web.socket.sockjs.client.Transport @@ -114,8 +115,8 @@ public class SockJsClient implements WebSocketClient, Lifecycle { private static InfoReceiver initInfoReceiver(List transports) { for (Transport transport : transports) { - if (transport instanceof InfoReceiver) { - return ((InfoReceiver) transport); + if (transport instanceof InfoReceiver infoReceiver) { + return infoReceiver; } } return new RestTemplateXhrTransport(); @@ -202,11 +203,8 @@ public class SockJsClient implements WebSocketClient, Lifecycle { if (!isRunning()) { this.running = true; for (Transport transport : this.transports) { - if (transport instanceof Lifecycle) { - Lifecycle lifecycle = (Lifecycle) transport; - if (!lifecycle.isRunning()) { - lifecycle.start(); - } + if (transport instanceof Lifecycle lifecycle && !lifecycle.isRunning()) { + lifecycle.start(); } } } @@ -217,11 +215,8 @@ public class SockJsClient implements WebSocketClient, Lifecycle { if (isRunning()) { this.running = false; for (Transport transport : this.transports) { - if (transport instanceof Lifecycle) { - Lifecycle lifecycle = (Lifecycle) transport; - if (lifecycle.isRunning()) { - lifecycle.stop(); - } + if (transport instanceof Lifecycle lifecycle && lifecycle.isRunning()) { + lifecycle.stop(); } } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/UndertowXhrTransport.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/UndertowXhrTransport.java index 6d2ace3441..1e12b914a6 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/UndertowXhrTransport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/UndertowXhrTransport.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. @@ -148,7 +148,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport { logger.trace("Starting XHR receive request for " + url); } - ClientCallback clientCallback = new ClientCallback() { + ClientCallback clientCallback = new ClientCallback<>() { @Override public void completed(ClientConnection connection) { ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(url.getPath()); @@ -182,7 +182,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport { final URI url, final HttpHeaders headers, final XhrClientSockJsSession sockJsSession, final SettableListenableFuture connectFuture) { - return new ClientCallback() { + return new ClientCallback<>() { @Override public void completed(final ClientExchange exchange) { exchange.setResponseListener(new ClientCallback() { @@ -309,7 +309,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport { private ClientCallback createRequestCallback(final @Nullable String body, final List responses, final CountDownLatch latch) { - return new ClientCallback() { + return new ClientCallback<>() { @Override public void completed(ClientExchange result) { result.setResponseListener(new ClientCallback() { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrClientSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrClientSockJsSession.java index 9137473fbf..4c3954aa27 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrClientSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrClientSockJsSession.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. @@ -43,9 +43,9 @@ public class XhrClientSockJsSession extends AbstractClientSockJsSession { private final XhrTransport transport; - private HttpHeaders headers; + private final HttpHeaders headers; - private HttpHeaders sendHeaders; + private final HttpHeaders sendHeaders; private final URI sendUrl; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java index 55580318be..35cf0fbff6 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.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. @@ -125,10 +125,9 @@ public class SockJsFrame { if (this == other) { return true; } - if (!(other instanceof SockJsFrame)) { + if (!(other instanceof SockJsFrame otherFrame)) { return false; } - SockJsFrame otherFrame = (SockJsFrame) other; return (this.type.equals(otherFrame.type) && this.content.equals(otherFrame.content)); }