Skip to content

Commit cea8fed

Browse files
committed
Add nullability annotations to module/spring-boot-mongodb
See gh-46587
1 parent 3e3c33e commit cea8fed

13 files changed

+88
-95
lines changed

module/spring-boot-mongodb/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ description = "Spring Boot MongoDB"
2828
dependencies {
2929
api(project(":core:spring-boot"))
3030

31+
compileOnly("com.google.code.findbugs:jsr305")
32+
3133
optional(project(":core:spring-boot-autoconfigure"))
3234
optional(project(":core:spring-boot-docker-compose"))
3335
optional(project(":core:spring-boot-testcontainers"))

module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoClientFactorySupport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.mongodb.MongoClientSettings;
2424
import com.mongodb.MongoClientSettings.Builder;
2525
import com.mongodb.MongoDriverInformation;
26+
import org.jspecify.annotations.Nullable;
2627

2728
/**
2829
* Base class for setup that is common to MongoDB client factories.
@@ -38,7 +39,7 @@ public abstract class MongoClientFactorySupport<T> {
3839

3940
private final BiFunction<MongoClientSettings, MongoDriverInformation, T> clientCreator;
4041

41-
protected MongoClientFactorySupport(List<MongoClientSettingsBuilderCustomizer> builderCustomizers,
42+
protected MongoClientFactorySupport(@Nullable List<MongoClientSettingsBuilderCustomizer> builderCustomizers,
4243
BiFunction<MongoClientSettings, MongoDriverInformation, T> clientCreator) {
4344
this.builderCustomizers = (builderCustomizers != null) ? builderCustomizers : Collections.emptyList();
4445
this.clientCreator = clientCreator;

module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoConnectionDetails.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.mongodb.autoconfigure;
1818

1919
import com.mongodb.ConnectionString;
20+
import org.jspecify.annotations.Nullable;
2021

2122
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
2223
import org.springframework.boot.ssl.SslBundle;
@@ -41,15 +42,15 @@ public interface MongoConnectionDetails extends ConnectionDetails {
4142
* SSL bundle to use.
4243
* @return the SSL bundle to use
4344
*/
44-
default SslBundle getSslBundle() {
45+
default @Nullable SslBundle getSslBundle() {
4546
return null;
4647
}
4748

4849
/**
4950
* GridFS configuration.
5051
* @return the GridFS configuration or {@code null}
5152
*/
52-
default GridFs getGridFs() {
53+
default @Nullable GridFs getGridFs() {
5354
return null;
5455
}
5556

@@ -62,30 +63,30 @@ interface GridFs {
6263
* GridFS database name.
6364
* @return the GridFS database name or {@code null}
6465
*/
65-
String getDatabase();
66+
@Nullable String getDatabase();
6667

6768
/**
6869
* GridFS bucket name.
6970
* @return the GridFS bucket name or {@code null}
7071
*/
71-
String getBucket();
72+
@Nullable String getBucket();
7273

7374
/**
7475
* Factory method to create a new {@link GridFs} instance.
7576
* @param database the database
7677
* @param bucket the bucket name
7778
* @return a new {@link GridFs} instance
7879
*/
79-
static GridFs of(String database, String bucket) {
80+
static GridFs of(@Nullable String database, @Nullable String bucket) {
8081
return new GridFs() {
8182

8283
@Override
83-
public String getDatabase() {
84+
public @Nullable String getDatabase() {
8485
return database;
8586
}
8687

8788
@Override
88-
public String getBucket() {
89+
public @Nullable String getBucket() {
8990
return bucket;
9091
}
9192

module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoProperties.java

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.mongodb.ConnectionString;
2222
import org.bson.UuidRepresentation;
23+
import org.jspecify.annotations.Nullable;
2324

2425
import org.springframework.boot.context.properties.ConfigurationProperties;
2526

@@ -59,56 +60,56 @@ public class MongoProperties {
5960
/**
6061
* Mongo server host. Ignored if 'uri' is set.
6162
*/
62-
private String host;
63+
private @Nullable String host;
6364

6465
/**
6566
* Mongo server port. Ignored if 'uri' is set.
6667
*/
67-
private Integer port = null;
68+
private @Nullable Integer port;
6869

6970
/**
7071
* Additional server hosts. Ignored if 'uri' is set or if 'host' is omitted.
7172
* Additional hosts will use the default mongo port of 27017. If you want to use a
7273
* different port you can use the "host:port" syntax.
7374
*/
74-
private List<String> additionalHosts;
75+
private @Nullable List<String> additionalHosts;
7576

7677
/**
7778
* Mongo database URI. Overrides host, port, username, and password.
7879
*/
79-
private String uri;
80+
private @Nullable String uri;
8081

8182
/**
8283
* Database name. Overrides database in URI.
8384
*/
84-
private String database;
85+
private @Nullable String database;
8586

8687
/**
8788
* Authentication database name.
8889
*/
89-
private String authenticationDatabase;
90+
private @Nullable String authenticationDatabase;
9091

9192
private final Gridfs gridfs = new Gridfs();
9293

9394
/**
9495
* Login user of the mongo server. Ignored if 'uri' is set.
9596
*/
96-
private String username;
97+
private @Nullable String username;
9798

9899
/**
99100
* Login password of the mongo server. Ignored if 'uri' is set.
100101
*/
101-
private char[] password;
102+
private char @Nullable [] password;
102103

103104
/**
104105
* Required replica set name for the cluster. Ignored if 'uri' is set.
105106
*/
106-
private String replicaSetName;
107+
private @Nullable String replicaSetName;
107108

108109
/**
109110
* Fully qualified name of the FieldNamingStrategy to use.
110111
*/
111-
private Class<?> fieldNamingStrategy;
112+
private @Nullable Class<?> fieldNamingStrategy;
112113

113114
/**
114115
* Representation to use when converting a UUID to a BSON binary value.
@@ -120,7 +121,7 @@ public class MongoProperties {
120121
/**
121122
* Whether to enable auto-index creation.
122123
*/
123-
private Boolean autoIndexCreation;
124+
private @Nullable Boolean autoIndexCreation;
124125

125126
public void setProtocol(String protocol) {
126127
this.protocol = protocol;
@@ -130,59 +131,59 @@ public String getProtocol() {
130131
return this.protocol;
131132
}
132133

133-
public String getHost() {
134+
public @Nullable String getHost() {
134135
return this.host;
135136
}
136137

137-
public void setHost(String host) {
138+
public void setHost(@Nullable String host) {
138139
this.host = host;
139140
}
140141

141-
public String getDatabase() {
142+
public @Nullable String getDatabase() {
142143
return this.database;
143144
}
144145

145-
public void setDatabase(String database) {
146+
public void setDatabase(@Nullable String database) {
146147
this.database = database;
147148
}
148149

149-
public String getAuthenticationDatabase() {
150+
public @Nullable String getAuthenticationDatabase() {
150151
return this.authenticationDatabase;
151152
}
152153

153-
public void setAuthenticationDatabase(String authenticationDatabase) {
154+
public void setAuthenticationDatabase(@Nullable String authenticationDatabase) {
154155
this.authenticationDatabase = authenticationDatabase;
155156
}
156157

157-
public String getUsername() {
158+
public @Nullable String getUsername() {
158159
return this.username;
159160
}
160161

161-
public void setUsername(String username) {
162+
public void setUsername(@Nullable String username) {
162163
this.username = username;
163164
}
164165

165-
public char[] getPassword() {
166+
public char @Nullable [] getPassword() {
166167
return this.password;
167168
}
168169

169-
public void setPassword(char[] password) {
170+
public void setPassword(char @Nullable [] password) {
170171
this.password = password;
171172
}
172173

173-
public String getReplicaSetName() {
174+
public @Nullable String getReplicaSetName() {
174175
return this.replicaSetName;
175176
}
176177

177-
public void setReplicaSetName(String replicaSetName) {
178+
public void setReplicaSetName(@Nullable String replicaSetName) {
178179
this.replicaSetName = replicaSetName;
179180
}
180181

181-
public Class<?> getFieldNamingStrategy() {
182+
public @Nullable Class<?> getFieldNamingStrategy() {
182183
return this.fieldNamingStrategy;
183184
}
184185

185-
public void setFieldNamingStrategy(Class<?> fieldNamingStrategy) {
186+
public void setFieldNamingStrategy(@Nullable Class<?> fieldNamingStrategy) {
186187
this.fieldNamingStrategy = fieldNamingStrategy;
187188
}
188189

@@ -194,50 +195,50 @@ public void setUuidRepresentation(UuidRepresentation uuidRepresentation) {
194195
this.uuidRepresentation = uuidRepresentation;
195196
}
196197

197-
public String getUri() {
198+
public @Nullable String getUri() {
198199
return this.uri;
199200
}
200201

201202
public String determineUri() {
202203
return (this.uri != null) ? this.uri : DEFAULT_URI;
203204
}
204205

205-
public void setUri(String uri) {
206+
public void setUri(@Nullable String uri) {
206207
this.uri = uri;
207208
}
208209

209-
public Integer getPort() {
210+
public @Nullable Integer getPort() {
210211
return this.port;
211212
}
212213

213-
public void setPort(Integer port) {
214+
public void setPort(@Nullable Integer port) {
214215
this.port = port;
215216
}
216217

217218
public Gridfs getGridfs() {
218219
return this.gridfs;
219220
}
220221

221-
public String getMongoClientDatabase() {
222+
public @Nullable String getMongoClientDatabase() {
222223
if (this.database != null) {
223224
return this.database;
224225
}
225226
return new ConnectionString(determineUri()).getDatabase();
226227
}
227228

228-
public Boolean isAutoIndexCreation() {
229+
public @Nullable Boolean isAutoIndexCreation() {
229230
return this.autoIndexCreation;
230231
}
231232

232-
public void setAutoIndexCreation(Boolean autoIndexCreation) {
233+
public void setAutoIndexCreation(@Nullable Boolean autoIndexCreation) {
233234
this.autoIndexCreation = autoIndexCreation;
234235
}
235236

236-
public List<String> getAdditionalHosts() {
237+
public @Nullable List<String> getAdditionalHosts() {
237238
return this.additionalHosts;
238239
}
239240

240-
public void setAdditionalHosts(List<String> additionalHosts) {
241+
public void setAdditionalHosts(@Nullable List<String> additionalHosts) {
241242
this.additionalHosts = additionalHosts;
242243
}
243244

@@ -250,26 +251,26 @@ public static class Gridfs {
250251
/**
251252
* GridFS database name.
252253
*/
253-
private String database;
254+
private @Nullable String database;
254255

255256
/**
256257
* GridFS bucket name.
257258
*/
258-
private String bucket;
259+
private @Nullable String bucket;
259260

260-
public String getDatabase() {
261+
public @Nullable String getDatabase() {
261262
return this.database;
262263
}
263264

264-
public void setDatabase(String database) {
265+
public void setDatabase(@Nullable String database) {
265266
this.database = database;
266267
}
267268

268-
public String getBucket() {
269+
public @Nullable String getBucket() {
269270
return this.bucket;
270271
}
271272

272-
public void setBucket(String bucket) {
273+
public void setBucket(@Nullable String bucket) {
273274
this.bucket = bucket;
274275
}
275276

@@ -281,12 +282,12 @@ public static class Ssl {
281282
* Whether to enable SSL support. Enabled automatically if "bundle" is provided
282283
* unless specified otherwise.
283284
*/
284-
private Boolean enabled;
285+
private @Nullable Boolean enabled;
285286

286287
/**
287288
* SSL bundle name.
288289
*/
289-
private String bundle;
290+
private @Nullable String bundle;
290291

291292
public boolean isEnabled() {
292293
return (this.enabled != null) ? this.enabled : this.bundle != null;
@@ -296,11 +297,11 @@ public void setEnabled(boolean enabled) {
296297
this.enabled = enabled;
297298
}
298299

299-
public String getBundle() {
300+
public @Nullable String getBundle() {
300301
return this.bundle;
301302
}
302303

303-
public void setBundle(String bundle) {
304+
public void setBundle(@Nullable String bundle) {
304305
this.bundle = bundle;
305306
}
306307

module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoReactiveAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.netty.channel.MultiThreadIoEventLoopGroup;
2525
import io.netty.channel.nio.NioIoHandler;
2626
import io.netty.channel.socket.SocketChannel;
27+
import org.jspecify.annotations.Nullable;
2728
import reactor.core.publisher.Flux;
2829

2930
import org.springframework.beans.factory.DisposableBean;
@@ -107,7 +108,7 @@ static final class NettyDriverMongoClientSettingsBuilderCustomizer
107108

108109
private final ObjectProvider<MongoClientSettings> settings;
109110

110-
private volatile EventLoopGroup eventLoopGroup;
111+
private volatile @Nullable EventLoopGroup eventLoopGroup;
111112

112113
NettyDriverMongoClientSettingsBuilderCustomizer(ObjectProvider<MongoClientSettings> settings) {
113114
this.settings = settings;
@@ -130,7 +131,7 @@ public void destroy() {
130131
}
131132
}
132133

133-
private boolean isCustomTransportConfiguration(MongoClientSettings settings) {
134+
private boolean isCustomTransportConfiguration(@Nullable MongoClientSettings settings) {
134135
return settings != null && settings.getTransportSettings() != null;
135136
}
136137

0 commit comments

Comments
 (0)