Skip to content

Commit a31cfba

Browse files
committed
Apply "instanceof pattern matching" in remainder of spring-websocket module
See gh-30067
1 parent 14973fd commit a31cfba

26 files changed

+147
-150
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,17 +104,17 @@ public final void sendMessage(WebSocketMessage<?> message) throws IOException {
104104
logger.trace("Sending " + message + ", " + this);
105105
}
106106

107-
if (message instanceof TextMessage) {
108-
sendTextMessage((TextMessage) message);
107+
if (message instanceof TextMessage textMessage) {
108+
sendTextMessage(textMessage);
109109
}
110-
else if (message instanceof BinaryMessage) {
111-
sendBinaryMessage((BinaryMessage) message);
110+
else if (message instanceof BinaryMessage binaryMessage) {
111+
sendBinaryMessage(binaryMessage);
112112
}
113-
else if (message instanceof PingMessage) {
114-
sendPingMessage((PingMessage) message);
113+
else if (message instanceof PingMessage pingMessage) {
114+
sendPingMessage(pingMessage);
115115
}
116-
else if (message instanceof PongMessage) {
117-
sendPongMessage((PongMessage) message);
116+
else if (message instanceof PongMessage pongMessage) {
117+
sendPongMessage(pongMessage);
118118
}
119119
else {
120120
throw new IllegalStateException("Unexpected WebSocketMessage type: " + message);

spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupport.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,10 +84,8 @@ public abstract class ConvertingEncoderDecoderSupport<T, M> {
8484
* @see jakarta.websocket.Decoder#init(EndpointConfig)
8585
*/
8686
public void init(EndpointConfig config) {
87-
ApplicationContext applicationContext = getApplicationContext();
88-
if (applicationContext instanceof ConfigurableApplicationContext) {
89-
ConfigurableListableBeanFactory beanFactory =
90-
((ConfigurableApplicationContext) applicationContext).getBeanFactory();
87+
if (getApplicationContext() instanceof ConfigurableApplicationContext cac) {
88+
ConfigurableListableBeanFactory beanFactory = cac.getBeanFactory();
9189
beanFactory.autowireBean(this);
9290
}
9391
}
@@ -194,12 +192,12 @@ public T decode(M message) throws DecodeException {
194192
return (T) getConversionService().convert(message, getMessageType(), getType());
195193
}
196194
catch (ConversionException ex) {
197-
if (message instanceof String) {
198-
throw new DecodeException((String) message,
195+
if (message instanceof String string) {
196+
throw new DecodeException(string,
199197
"Unable to decode websocket message using ConversionService", ex);
200198
}
201-
if (message instanceof ByteBuffer) {
202-
throw new DecodeException((ByteBuffer) message,
199+
if (message instanceof ByteBuffer byteBuffer) {
200+
throw new DecodeException(byteBuffer,
203201
"Unable to decode websocket message using ConversionService", ex);
204202
}
205203
throw ex;

spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,13 +93,13 @@ private StompSubProtocolHandler initStompSubProtocolHandler() {
9393
return null;
9494
}
9595
for (SubProtocolHandler handler : this.webSocketHandler.getProtocolHandlers()) {
96-
if (handler instanceof StompSubProtocolHandler) {
97-
return (StompSubProtocolHandler) handler;
96+
if (handler instanceof StompSubProtocolHandler stompHandler) {
97+
return stompHandler;
9898
}
9999
}
100100
SubProtocolHandler defaultHandler = this.webSocketHandler.getDefaultProtocolHandler();
101-
if (defaultHandler instanceof StompSubProtocolHandler) {
102-
return (StompSubProtocolHandler) defaultHandler;
101+
if (defaultHandler instanceof StompSubProtocolHandler stompHandler) {
102+
return stompHandler;
103103
}
104104
return null;
105105
}
@@ -193,9 +193,8 @@ public String getSockJsTaskSchedulerStatsInfo() {
193193
if (this.sockJsTaskScheduler == null) {
194194
return "null";
195195
}
196-
if (this.sockJsTaskScheduler instanceof ThreadPoolTaskScheduler) {
197-
return getExecutorStatsInfo(((ThreadPoolTaskScheduler) this.sockJsTaskScheduler)
198-
.getScheduledThreadPoolExecutor());
196+
if (this.sockJsTaskScheduler instanceof ThreadPoolTaskScheduler threadPoolTaskScheduler) {
197+
return getExecutorStatsInfo(threadPoolTaskScheduler.getScheduledThreadPoolExecutor());
199198
}
200199
return "unknown";
201200
}
@@ -205,8 +204,8 @@ private String getExecutorStatsInfo(@Nullable Executor executor) {
205204
return "null";
206205
}
207206

208-
if (executor instanceof ThreadPoolTaskExecutor) {
209-
executor = ((ThreadPoolTaskExecutor) executor).getThreadPoolExecutor();
207+
if (executor instanceof ThreadPoolTaskExecutor threadPoolTaskScheduler) {
208+
executor = threadPoolTaskScheduler.getThreadPoolExecutor();
210209
}
211210

212211
if (executor instanceof ThreadPoolExecutor) {

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,10 +91,10 @@ public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler,
9191

9292
private static SubProtocolWebSocketHandler unwrapSubProtocolWebSocketHandler(WebSocketHandler handler) {
9393
WebSocketHandler actual = WebSocketHandlerDecorator.unwrap(handler);
94-
if (!(actual instanceof SubProtocolWebSocketHandler)) {
94+
if (!(actual instanceof SubProtocolWebSocketHandler subProtocolWebSocketHandler)) {
9595
throw new IllegalArgumentException("No SubProtocolWebSocketHandler in " + handler);
9696
}
97-
return (SubProtocolWebSocketHandler) actual;
97+
return subProtocolWebSocketHandler;
9898
}
9999

100100

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,8 +137,8 @@ public WebSocketMessageBrokerStats webSocketMessageBrokerStats(
137137

138138
WebSocketMessageBrokerStats stats = new WebSocketMessageBrokerStats();
139139
stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler);
140-
if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler) {
141-
stats.setStompBrokerRelay((StompBrokerRelayMessageHandler) stompBrokerRelayMessageHandler);
140+
if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler sbrmh) {
141+
stats.setStompBrokerRelay(sbrmh);
142142
}
143143
stats.setInboundChannelExecutor(inboundExecutor);
144144
stats.setOutboundChannelExecutor(outboundExecutor);

spring-websocket/src/main/java/org/springframework/web/socket/handler/AbstractWebSocketHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,14 +39,14 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
3939

4040
@Override
4141
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
42-
if (message instanceof TextMessage) {
43-
handleTextMessage(session, (TextMessage) message);
42+
if (message instanceof TextMessage textMessage) {
43+
handleTextMessage(session, textMessage);
4444
}
45-
else if (message instanceof BinaryMessage) {
46-
handleBinaryMessage(session, (BinaryMessage) message);
45+
else if (message instanceof BinaryMessage binaryMessage) {
46+
handleBinaryMessage(session, binaryMessage);
4747
}
48-
else if (message instanceof PongMessage) {
49-
handlePongMessage(session, (PongMessage) message);
48+
else if (message instanceof PongMessage pongMessage) {
49+
handlePongMessage(session, pongMessage);
5050
}
5151
else {
5252
throw new IllegalStateException("Unexpected WebSocket message type: " + message);

spring-websocket/src/main/java/org/springframework/web/socket/handler/BeanCreatingHandlerProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,8 +48,8 @@ public BeanCreatingHandlerProvider(Class<? extends T> handlerType) {
4848

4949
@Override
5050
public void setBeanFactory(BeanFactory beanFactory) {
51-
if (beanFactory instanceof AutowireCapableBeanFactory) {
52-
this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
51+
if (beanFactory instanceof AutowireCapableBeanFactory autowireCapableBeanFactory) {
52+
this.beanFactory = autowireCapableBeanFactory;
5353
}
5454
}
5555

spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketHandlerDecorator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,15 +50,15 @@ public WebSocketHandler getDelegate() {
5050

5151
public WebSocketHandler getLastHandler() {
5252
WebSocketHandler result = this.delegate;
53-
while (result instanceof WebSocketHandlerDecorator) {
54-
result = ((WebSocketHandlerDecorator) result).getDelegate();
53+
while (result instanceof WebSocketHandlerDecorator webSocketHandlerDecorator) {
54+
result = webSocketHandlerDecorator.getDelegate();
5555
}
5656
return result;
5757
}
5858

5959
public static WebSocketHandler unwrap(WebSocketHandler handler) {
60-
if (handler instanceof WebSocketHandlerDecorator) {
61-
return ((WebSocketHandlerDecorator) handler).getLastHandler();
60+
if (handler instanceof WebSocketHandlerDecorator webSocketHandlerDecorator) {
61+
return webSocketHandlerDecorator.getLastHandler();
6262
}
6363
else {
6464
return handler;

spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketSessionDecorator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,15 +59,15 @@ public WebSocketSession getDelegate() {
5959

6060
public WebSocketSession getLastSession() {
6161
WebSocketSession result = this.delegate;
62-
while (result instanceof WebSocketSessionDecorator) {
63-
result = ((WebSocketSessionDecorator) result).getDelegate();
62+
while (result instanceof WebSocketSessionDecorator webSocketSessionDecorator) {
63+
result = webSocketSessionDecorator.getDelegate();
6464
}
6565
return result;
6666
}
6767

6868
public static WebSocketSession unwrap(WebSocketSession session) {
69-
if (session instanceof WebSocketSessionDecorator) {
70-
return ((WebSocketSessionDecorator) session).getLastSession();
69+
if (session instanceof WebSocketSessionDecorator webSocketSessionDecorator) {
70+
return webSocketSessionDecorator.getLastSession();
7171
}
7272
else {
7373
return session;

spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ public void handleMessageFromClient(WebSocketSession session,
230230
List<Message<byte[]>> messages;
231231
try {
232232
ByteBuffer byteBuffer;
233-
if (webSocketMessage instanceof TextMessage) {
234-
byteBuffer = ByteBuffer.wrap(((TextMessage) webSocketMessage).asBytes());
233+
if (webSocketMessage instanceof TextMessage textMessage) {
234+
byteBuffer = ByteBuffer.wrap(textMessage.asBytes());
235235
}
236-
else if (webSocketMessage instanceof BinaryMessage) {
237-
byteBuffer = ((BinaryMessage) webSocketMessage).getPayload();
236+
else if (webSocketMessage instanceof BinaryMessage binaryMessage) {
237+
byteBuffer = binaryMessage.getPayload();
238238
}
239239
else {
240240
return;
@@ -398,8 +398,8 @@ private boolean detectImmutableMessageInterceptor(MessageChannel channel) {
398398
return this.immutableMessageInterceptorPresent;
399399
}
400400

401-
if (channel instanceof AbstractMessageChannel) {
402-
for (ChannelInterceptor interceptor : ((AbstractMessageChannel) channel).getInterceptors()) {
401+
if (channel instanceof AbstractMessageChannel abstractMessageChannel) {
402+
for (ChannelInterceptor interceptor : abstractMessageChannel.getInterceptors()) {
403403
if (interceptor instanceof ImmutableMessageChannelInterceptor) {
404404
this.immutableMessageInterceptorPresent = true;
405405
return true;
@@ -520,8 +520,8 @@ private void sendToClient(WebSocketSession session, StompHeaderAccessor stompAcc
520520

521521
private StompHeaderAccessor getStompHeaderAccessor(Message<?> message) {
522522
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
523-
if (accessor instanceof StompHeaderAccessor) {
524-
return (StompHeaderAccessor) accessor;
523+
if (accessor instanceof StompHeaderAccessor stompHeaderAccessor) {
524+
return stompHeaderAccessor;
525525
}
526526
else {
527527
StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(message);
@@ -614,8 +614,8 @@ private StompHeaderAccessor afterStompSessionConnected(Message<?> message, Stomp
614614
long[] heartbeat = accessor.getHeartbeat();
615615
if (heartbeat[1] > 0) {
616616
session = WebSocketSessionDecorator.unwrap(session);
617-
if (session instanceof SockJsSession) {
618-
((SockJsSession) session).disableHeartbeat();
617+
if (session instanceof SockJsSession sockJsSession) {
618+
sockJsSession.disableHeartbeat();
619619
}
620620
}
621621

0 commit comments

Comments
 (0)