Skip to content

Commit 8434446

Browse files
committed
Add nullability annotations to module/spring-boot-rsocket
See gh-46587
1 parent 80b1de2 commit 8434446

File tree

13 files changed

+85
-54
lines changed

13 files changed

+85
-54
lines changed

module/spring-boot-rsocket/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ dependencies {
2929
api("io.rsocket:rsocket-core")
3030
api("org.springframework:spring-messaging")
3131

32+
compileOnly("com.google.code.findbugs:jsr305")
33+
3234
implementation("org.springframework:spring-web")
3335

3436
optional(project(":core:spring-boot-autoconfigure"))

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/autoconfigure/RSocketProperties.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.net.InetAddress;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.boot.context.properties.ConfigurationProperties;
2224
import org.springframework.boot.context.properties.NestedConfigurationProperty;
2325
import org.springframework.boot.rsocket.server.RSocketServer;
@@ -46,12 +48,12 @@ public static class Server {
4648
/**
4749
* Server port.
4850
*/
49-
private Integer port;
51+
private @Nullable Integer port;
5052

5153
/**
5254
* Network address to which the server should bind.
5355
*/
54-
private InetAddress address;
56+
private @Nullable InetAddress address;
5557

5658
/**
5759
* RSocket transport protocol.
@@ -62,32 +64,32 @@ public static class Server {
6264
* Path under which RSocket handles requests (only works with websocket
6365
* transport).
6466
*/
65-
private String mappingPath;
67+
private @Nullable String mappingPath;
6668

6769
/**
6870
* Maximum transmission unit. Frames larger than the specified value are
6971
* fragmented.
7072
*/
71-
private DataSize fragmentSize;
73+
private @Nullable DataSize fragmentSize;
7274

7375
@NestedConfigurationProperty
74-
private Ssl ssl;
76+
private @Nullable Ssl ssl;
7577

7678
private final Spec spec = new Spec();
7779

78-
public Integer getPort() {
80+
public @Nullable Integer getPort() {
7981
return this.port;
8082
}
8183

82-
public void setPort(Integer port) {
84+
public void setPort(@Nullable Integer port) {
8385
this.port = port;
8486
}
8587

86-
public InetAddress getAddress() {
88+
public @Nullable InetAddress getAddress() {
8789
return this.address;
8890
}
8991

90-
public void setAddress(InetAddress address) {
92+
public void setAddress(@Nullable InetAddress address) {
9193
this.address = address;
9294
}
9395

@@ -99,27 +101,27 @@ public void setTransport(RSocketServer.Transport transport) {
99101
this.transport = transport;
100102
}
101103

102-
public String getMappingPath() {
104+
public @Nullable String getMappingPath() {
103105
return this.mappingPath;
104106
}
105107

106-
public void setMappingPath(String mappingPath) {
108+
public void setMappingPath(@Nullable String mappingPath) {
107109
this.mappingPath = mappingPath;
108110
}
109111

110-
public DataSize getFragmentSize() {
112+
public @Nullable DataSize getFragmentSize() {
111113
return this.fragmentSize;
112114
}
113115

114-
public void setFragmentSize(DataSize fragmentSize) {
116+
public void setFragmentSize(@Nullable DataSize fragmentSize) {
115117
this.fragmentSize = fragmentSize;
116118
}
117119

118-
public Ssl getSsl() {
120+
public @Nullable Ssl getSsl() {
119121
return this.ssl;
120122
}
121123

122-
public void setSsl(Ssl ssl) {
124+
public void setSsl(@Nullable Ssl ssl) {
123125
this.ssl = ssl;
124126
}
125127

@@ -132,7 +134,7 @@ public static class Spec {
132134
/**
133135
* Sub-protocols to use in websocket handshake signature.
134136
*/
135-
private String protocols;
137+
private @Nullable String protocols;
136138

137139
/**
138140
* Maximum allowable frame payload length.
@@ -149,11 +151,11 @@ public static class Spec {
149151
*/
150152
private boolean compress;
151153

152-
public String getProtocols() {
154+
public @Nullable String getProtocols() {
153155
return this.protocols;
154156
}
155157

156-
public void setProtocols(String protocols) {
158+
public void setProtocols(@Nullable String protocols) {
157159
this.protocols = protocols;
158160
}
159161

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/autoconfigure/RSocketServerAutoConfiguration.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.http.client.ReactorResourceFactory;
5252
import org.springframework.messaging.rsocket.RSocketStrategies;
5353
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
54+
import org.springframework.util.Assert;
5455
import org.springframework.util.unit.DataSize;
5556

5657
/**
@@ -78,9 +79,10 @@ static class WebFluxServerConfiguration {
7879
@ConditionalOnMissingBean
7980
RSocketWebSocketNettyRouteProvider rSocketWebsocketRouteProvider(RSocketProperties properties,
8081
RSocketMessageHandler messageHandler, ObjectProvider<RSocketServerCustomizer> customizers) {
81-
return new RSocketWebSocketNettyRouteProvider(properties.getServer().getMappingPath(),
82-
messageHandler.responder(), customizeWebsocketServerSpec(properties.getServer().getSpec()),
83-
customizers.orderedStream());
82+
String mappingPath = properties.getServer().getMappingPath();
83+
Assert.state(mappingPath != null, "'mappingPath' must not be null");
84+
return new RSocketWebSocketNettyRouteProvider(mappingPath, messageHandler.responder(),
85+
customizeWebsocketServerSpec(properties.getServer().getSpec()), customizers.orderedStream());
8486
}
8587

8688
private Consumer<Builder> customizeWebsocketServerSpec(Spec spec) {

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/autoconfigure/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Auto-configuration for RSocket.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.rsocket.autoconfigure;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/context/RSocketServerBootstrap.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class RSocketServerBootstrap implements ApplicationEventPublisherAware, S
3535

3636
private final RSocketServer server;
3737

38+
@SuppressWarnings("NullAway.Init")
3839
private ApplicationEventPublisher eventPublisher;
3940

4041
public RSocketServerBootstrap(RSocketServerFactory serverFactory, SocketAcceptor socketAcceptor) {
@@ -60,11 +61,7 @@ public void stop() {
6061

6162
@Override
6263
public boolean isRunning() {
63-
RSocketServer server = this.server;
64-
if (server != null) {
65-
return server.address() != null;
66-
}
67-
return false;
64+
return this.server.address() != null;
6865
}
6966

7067
}

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/context/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
* RSocket integrations with Spring Framework's
1919
* {@link org.springframework.context.ApplicationContext ApplicationContext}.
2020
*/
21+
@NullMarked
2122
package org.springframework.boot.rsocket.context;
23+
24+
import org.jspecify.annotations.NullMarked;

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/messaging/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Support for RSocket-based messaging.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.rsocket.messaging;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServer.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.rsocket.transport.netty.server.CloseableChannel;
2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
25+
import org.jspecify.annotations.Nullable;
2526
import reactor.core.publisher.Mono;
2627

2728
import org.springframework.boot.rsocket.server.RSocketServer;
@@ -41,18 +42,18 @@ public class NettyRSocketServer implements RSocketServer {
4142

4243
private final Mono<CloseableChannel> starter;
4344

44-
private final Duration lifecycleTimeout;
45+
private final @Nullable Duration lifecycleTimeout;
4546

46-
private CloseableChannel channel;
47+
private @Nullable CloseableChannel channel;
4748

48-
public NettyRSocketServer(Mono<CloseableChannel> starter, Duration lifecycleTimeout) {
49+
public NettyRSocketServer(Mono<CloseableChannel> starter, @Nullable Duration lifecycleTimeout) {
4950
Assert.notNull(starter, "'starter' must not be null");
5051
this.starter = starter;
5152
this.lifecycleTimeout = lifecycleTimeout;
5253
}
5354

5455
@Override
55-
public InetSocketAddress address() {
56+
public @Nullable InetSocketAddress address() {
5657
if (this.channel != null) {
5758
return this.channel.address();
5859
}
@@ -62,11 +63,16 @@ public InetSocketAddress address() {
6263
@Override
6364
public void start() throws RSocketServerException {
6465
this.channel = block(this.starter, this.lifecycleTimeout);
65-
logger.info("Netty RSocket started on port " + address().getPort());
66+
InetSocketAddress address = address();
67+
Assert.state(address != null, "'address' must not be null");
68+
logger.info("Netty RSocket started on port " + address.getPort());
6669
startDaemonAwaitThread(this.channel);
6770
}
6871

69-
private void startDaemonAwaitThread(CloseableChannel channel) {
72+
private void startDaemonAwaitThread(@Nullable CloseableChannel channel) {
73+
if (channel == null) {
74+
return;
75+
}
7076
Thread awaitThread = new Thread(() -> channel.onClose().block(), "rsocket");
7177
awaitThread.setContextClassLoader(getClass().getClassLoader());
7278
awaitThread.setDaemon(false);
@@ -81,7 +87,7 @@ public void stop() throws RSocketServerException {
8187
}
8288
}
8389

84-
private <T> T block(Mono<T> mono, Duration timeout) {
90+
private <T> @Nullable T block(Mono<T> mono, @Nullable Duration timeout) {
8591
return (timeout != null) ? mono.block(timeout) : mono.block();
8692
}
8793

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.rsocket.transport.netty.server.CloseableChannel;
3434
import io.rsocket.transport.netty.server.TcpServerTransport;
3535
import io.rsocket.transport.netty.server.WebsocketServerTransport;
36+
import org.jspecify.annotations.Nullable;
3637
import reactor.core.publisher.Mono;
3738
import reactor.netty.http.Http11SslContextSpec;
3839
import reactor.netty.http.server.HttpServer;
@@ -70,34 +71,34 @@ public class NettyRSocketServerFactory implements RSocketServerFactory, Configur
7071

7172
private int port = 9898;
7273

73-
private DataSize fragmentSize;
74+
private @Nullable DataSize fragmentSize;
7475

75-
private InetAddress address;
76+
private @Nullable InetAddress address;
7677

7778
private RSocketServer.Transport transport = RSocketServer.Transport.TCP;
7879

79-
private ReactorResourceFactory resourceFactory;
80+
private @Nullable ReactorResourceFactory resourceFactory;
8081

81-
private Duration lifecycleTimeout;
82+
private @Nullable Duration lifecycleTimeout;
8283

8384
private List<RSocketServerCustomizer> rSocketServerCustomizers = new ArrayList<>();
8485

85-
private Ssl ssl;
86+
private @Nullable Ssl ssl;
8687

87-
private SslBundles sslBundles;
88+
private @Nullable SslBundles sslBundles;
8889

8990
@Override
9091
public void setPort(int port) {
9192
this.port = port;
9293
}
9394

9495
@Override
95-
public void setFragmentSize(DataSize fragmentSize) {
96+
public void setFragmentSize(@Nullable DataSize fragmentSize) {
9697
this.fragmentSize = fragmentSize;
9798
}
9899

99100
@Override
100-
public void setAddress(InetAddress address) {
101+
public void setAddress(@Nullable InetAddress address) {
101102
this.address = address;
102103
}
103104

@@ -107,20 +108,20 @@ public void setTransport(RSocketServer.Transport transport) {
107108
}
108109

109110
@Override
110-
public void setSsl(Ssl ssl) {
111+
public void setSsl(@Nullable Ssl ssl) {
111112
this.ssl = ssl;
112113
}
113114

114115
@Override
115-
public void setSslBundles(SslBundles sslBundles) {
116+
public void setSslBundles(@Nullable SslBundles sslBundles) {
116117
this.sslBundles = sslBundles;
117118
}
118119

119120
/**
120121
* Set the {@link ReactorResourceFactory} to get the shared resources from.
121122
* @param resourceFactory the server resources
122123
*/
123-
public void setResourceFactory(ReactorResourceFactory resourceFactory) {
124+
public void setResourceFactory(@Nullable ReactorResourceFactory resourceFactory) {
124125
this.resourceFactory = resourceFactory;
125126
}
126127

@@ -184,13 +185,13 @@ private ServerTransport<CloseableChannel> createWebSocketTransport() {
184185
httpServer = httpServer.runOn(this.resourceFactory.getLoopResources());
185186
}
186187
if (Ssl.isEnabled(this.ssl)) {
187-
httpServer = customizeSslConfiguration(httpServer);
188+
httpServer = customizeSslConfiguration(httpServer, this.ssl);
188189
}
189190
return WebsocketServerTransport.create(httpServer.bindAddress(this::getListenAddress));
190191
}
191192

192-
private HttpServer customizeSslConfiguration(HttpServer httpServer) {
193-
return new HttpServerSslCustomizer(this.ssl.getClientAuth(), getSslBundle(), getServerNameSslBundles())
193+
private HttpServer customizeSslConfiguration(HttpServer httpServer, Ssl ssl) {
194+
return new HttpServerSslCustomizer(ssl.getClientAuth(), getSslBundle(), getServerNameSslBundles())
194195
.apply(httpServer);
195196
}
196197

@@ -211,12 +212,14 @@ private SslBundle getSslBundle() {
211212
}
212213

213214
protected final Map<String, SslBundle> getServerNameSslBundles() {
215+
Assert.state(this.ssl != null, "'ssl' must not be null");
214216
return this.ssl.getServerNameBundles()
215217
.stream()
216218
.collect(Collectors.toMap(Ssl.ServerNameSslBundle::serverName, this::getBundle));
217219
}
218220

219221
private SslBundle getBundle(ServerNameSslBundle serverNameSslBundle) {
222+
Assert.state(this.sslBundles != null, "'sslBundles' must not be null");
220223
return this.sslBundles.getBundle(serverNameSslBundle.bundle());
221224
}
222225

@@ -253,7 +256,7 @@ private static final class TcpServerSslCustomizer extends SslCustomizer {
253256

254257
private final SslBundle sslBundle;
255258

256-
private TcpServerSslCustomizer(Ssl.ClientAuth clientAuth, SslBundle sslBundle,
259+
private TcpServerSslCustomizer(Ssl.@Nullable ClientAuth clientAuth, SslBundle sslBundle,
257260
Map<String, SslBundle> serverNameSslBundles) {
258261
super(Ssl.ClientAuth.map(clientAuth, ClientAuth.NONE, ClientAuth.OPTIONAL, ClientAuth.REQUIRE));
259262
this.sslBundle = sslBundle;
@@ -272,7 +275,7 @@ private static final class HttpServerSslCustomizer extends SslCustomizer {
272275

273276
private final Map<String, SslProvider> serverNameSslProviders;
274277

275-
private HttpServerSslCustomizer(Ssl.ClientAuth clientAuth, SslBundle sslBundle,
278+
private HttpServerSslCustomizer(Ssl.@Nullable ClientAuth clientAuth, SslBundle sslBundle,
276279
Map<String, SslBundle> serverNameSslBundles) {
277280
super(Ssl.ClientAuth.map(clientAuth, ClientAuth.NONE, ClientAuth.OPTIONAL, ClientAuth.REQUIRE));
278281
this.sslProvider = createSslProvider(sslBundle);

module/spring-boot-rsocket/src/main/java/org/springframework/boot/rsocket/netty/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Reactor Netty based RSocket server implementation.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.rsocket.netty;
22+
23+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)