Skip to content

Commit 34b2adf

Browse files
committed
Address review feedback
* Change `payloadType` to `Class<?>` * Initialize arrays with empty arrays instead of null Signed-off-by: Jooyoung Pyoung <[email protected]>
1 parent ac89b72 commit 34b2adf

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/ServerWebSocketContainer.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,21 @@ public class ServerWebSocketContainer extends IntegrationWebSocketContainer
5959

6060
private final String[] paths;
6161

62-
@SuppressWarnings("NullAway.Init")
63-
private HandshakeHandler handshakeHandler;
62+
private @Nullable HandshakeHandler handshakeHandler;
6463

65-
private HandshakeInterceptor @Nullable [] interceptors;
64+
private HandshakeInterceptor[] interceptors = new HandshakeInterceptor[0];
6665

6766
private WebSocketHandlerDecoratorFactory @Nullable [] decoratorFactories;
6867

6968
private @Nullable SockJsServiceOptions sockJsServiceOptions;
7069

71-
private String @Nullable [] origins;
70+
private String[] origins = new String[0];
7271

7372
private boolean autoStartup = true;
7473

7574
private int phase = 0;
7675

77-
@SuppressWarnings("NullAway.Init")
78-
private TaskScheduler sockJsTaskScheduler;
76+
private @Nullable TaskScheduler sockJsTaskScheduler;
7977

8078
public ServerWebSocketContainer(String... paths) {
8179
Assert.notEmpty(paths, "'paths' must not be empty");
@@ -149,7 +147,7 @@ public void setSockJsTaskScheduler(TaskScheduler sockJsTaskScheduler) {
149147
this.sockJsTaskScheduler = sockJsTaskScheduler;
150148
}
151149

152-
public TaskScheduler getSockJsTaskScheduler() {
150+
public @Nullable TaskScheduler getSockJsTaskScheduler() {
153151
return this.sockJsTaskScheduler;
154152
}
155153

@@ -164,6 +162,7 @@ public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
164162
}
165163
}
166164

165+
Assert.notNull(this.handshakeHandler, "'handshakeHandler' must not be null");
167166
WebSocketHandlerRegistration registration = registry.addHandler(webSocketHandler, this.paths)
168167
.setHandshakeHandler(this.handshakeHandler);
169168

@@ -240,8 +239,8 @@ public boolean isRunning() {
240239
public void start() {
241240
this.lock.lock();
242241
try {
243-
if (this.handshakeHandler instanceof Lifecycle && !isRunning()) {
244-
((Lifecycle) this.handshakeHandler).start();
242+
if (this.handshakeHandler instanceof Lifecycle lifeCycleHandler && !isRunning()) {
243+
lifeCycleHandler.start();
245244
}
246245
}
247246
finally {
@@ -251,15 +250,15 @@ public void start() {
251250

252251
@Override
253252
public void stop() {
254-
if (isRunning()) {
255-
((Lifecycle) this.handshakeHandler).stop();
253+
if (this.handshakeHandler instanceof Lifecycle lifeCycleHandler && isRunning()) {
254+
lifeCycleHandler.stop();
256255
}
257256
}
258257

259258
@Override
260259
public void stop(Runnable callback) {
261-
if (isRunning()) {
262-
((Lifecycle) this.handshakeHandler).stop();
260+
if (this.handshakeHandler instanceof Lifecycle lifeCycleHandler && isRunning()) {
261+
lifeCycleHandler.stop();
263262
}
264263
callback.run();
265264
}

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.ListIterator;
2323
import java.util.Map;
24-
import java.util.concurrent.atomic.AtomicReference;
2524

2625
import org.jspecify.annotations.Nullable;
2726

@@ -69,6 +68,7 @@
6968
*
7069
* @author Artem Bilan
7170
* @author Ngoc Nhan
71+
* @author Jooyoung Pyoung
7272
*
7373
* @since 4.1
7474
*/
@@ -101,7 +101,7 @@ public class WebSocketInboundChannelAdapter extends MessageProducerSupport
101101

102102
private final MessageChannel subProtocolHandlerChannel;
103103

104-
private final AtomicReference<Class<?>> payloadType = new AtomicReference<>(String.class);
104+
private Class<?> payloadType = String.class;
105105

106106
@SuppressWarnings("NullAway.Init")
107107
private ApplicationEventPublisher eventPublisher;
@@ -166,7 +166,7 @@ public void setMergeWithDefaultConverters(boolean mergeWithDefaultConverters) {
166166
*/
167167
public void setPayloadType(Class<?> payloadType) {
168168
Assert.notNull(payloadType, "'payloadType' must not be null");
169-
this.payloadType.set(payloadType);
169+
this.payloadType = payloadType;
170170
}
171171

172172
/**
@@ -306,7 +306,7 @@ public boolean isActive() {
306306
return active;
307307
}
308308

309-
@SuppressWarnings("unchecked")
309+
@SuppressWarnings({"unchecked", "NullAway"}) // Dataflow analysis limitation
310310
private void handleMessageAndSend(final Message<?> message) {
311311
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message);
312312
StompCommand stompCommand = (StompCommand) headerAccessor.getHeader("stompCommand");
@@ -327,7 +327,6 @@ else if (StompCommand.RECEIPT.equals(stompCommand)) {
327327
}
328328
else {
329329
if (this.useBroker) {
330-
Assert.notNull(this.brokerHandler, "brokerHandler is required");
331330
this.brokerHandler.handleMessage(message);
332331
}
333332
else {
@@ -350,9 +349,9 @@ private boolean isProcessingTypeOrCommand(SimpMessageHeaderAccessor headerAccess
350349
&& !checkDestinationPrefix(headerAccessor.getDestination());
351350
}
352351

352+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
353353
private boolean checkDestinationPrefix(@Nullable String destination) {
354354
if (this.useBroker) {
355-
Assert.notNull(this.brokerHandler, "brokerHandler is required");
356355
Collection<String> destinationPrefixes = this.brokerHandler.getDestinationPrefixes();
357356
if ((destination == null) || CollectionUtils.isEmpty(destinationPrefixes)) {
358357
return false;
@@ -379,14 +378,11 @@ private void produceConnectAckMessage(Message<?> message, SimpMessageHeaderAcces
379378
}
380379

381380
private void produceMessage(Message<?> message, SimpMessageHeaderAccessor headerAccessor) {
382-
Class<?> targetType = this.payloadType.get();
383-
Assert.notNull(targetType, "payloadType must not be null");
384-
385-
Object payload = this.messageConverter.fromMessage(message, targetType);
381+
Object payload = this.messageConverter.fromMessage(message, this.payloadType);
386382
Assert.state(payload != null,
387383
() -> "The message converter '" + this.messageConverter +
388384
"' produced no payload for message '" + message +
389-
"' and expected payload type: " + this.payloadType.get());
385+
"' and expected payload type: " + this.payloadType);
390386
Message<Object> messageToSend =
391387
getMessageBuilderFactory()
392388
.withPayload(payload)

spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323
import java.util.Map;
24-
import java.util.concurrent.atomic.AtomicReference;
2524

2625
import org.junit.jupiter.api.Test;
2726

@@ -62,6 +61,7 @@
6261
/**
6362
* @author Artem Bilan
6463
* @author Julian Koch
64+
* @author Jooyoung Pyoung
6565
*
6666
* @since 4.1
6767
*/
@@ -193,7 +193,7 @@ public void testDefaultInboundChannelAdapterAndServerContainer() {
193193
assertThat(TestUtils.getPropertyValue(this.defaultInboundAdapter, "messageConverters")).isNull();
194194
assertThat(TestUtils.getPropertyValue(this.defaultInboundAdapter, "defaultConverters"))
195195
.isEqualTo(TestUtils.getPropertyValue(this.defaultInboundAdapter, "messageConverter.converters"));
196-
assertThat(TestUtils.getPropertyValue(this.defaultInboundAdapter, "payloadType", AtomicReference.class).get())
196+
assertThat(TestUtils.getPropertyValue(this.defaultInboundAdapter, "payloadType", Class.class))
197197
.isEqualTo(String.class);
198198
assertThat(TestUtils.getPropertyValue(this.defaultInboundAdapter, "useBroker", Boolean.class)).isTrue();
199199
assertThat(TestUtils.getPropertyValue(this.defaultInboundAdapter, "brokerHandler"))
@@ -218,7 +218,7 @@ public void testCustomInboundChannelAdapterAndClientContainer() throws URISyntax
218218
.isEqualTo(2000L);
219219
assertThat(TestUtils.getPropertyValue(this.customInboundAdapter, "phase")).isEqualTo(200);
220220
assertThat(TestUtils.getPropertyValue(this.customInboundAdapter, "autoStartup", Boolean.class)).isFalse();
221-
assertThat(TestUtils.getPropertyValue(this.customInboundAdapter, "payloadType", AtomicReference.class).get())
221+
assertThat(TestUtils.getPropertyValue(this.customInboundAdapter, "payloadType", Class.class))
222222
.isEqualTo(Integer.class);
223223
SubProtocolHandlerRegistry subProtocolHandlerRegistry = TestUtils.getPropertyValue(this.customInboundAdapter,
224224
"subProtocolHandlerRegistry", SubProtocolHandlerRegistry.class);

0 commit comments

Comments
 (0)