Skip to content

Commit a3476d2

Browse files
committed
Nullability clean up in MongoDB module
Follow up after: #10213
1 parent fb12c05 commit a3476d2

10 files changed

+50
-64
lines changed

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/inbound/AbstractMongoDbMessageSource.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Collection;
2020
import java.util.Map;
21-
import java.util.Objects;
2221

2322
import com.mongodb.DBObject;
2423
import org.bson.Document;
@@ -28,7 +27,6 @@
2827
import org.springframework.context.ApplicationContext;
2928
import org.springframework.context.ApplicationContextAware;
3029
import org.springframework.data.mapping.IdentifierAccessor;
31-
import org.springframework.data.mapping.context.MappingContext;
3230
import org.springframework.data.mongodb.core.convert.MongoConverter;
3331
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
3432
import org.springframework.data.mongodb.core.query.BasicQuery;
@@ -190,16 +188,15 @@ protected Query evaluateQueryExpression() {
190188
Assert.notNull(value, "'queryExpression' must not evaluate to null");
191189
Query query = null;
192190
if (value instanceof String string) {
193-
query = new BasicQuery(string);
191+
return new BasicQuery(string);
194192
}
195193
else if (value instanceof Query castQuery) {
196-
query = castQuery;
194+
return castQuery;
197195
}
198196
else {
199197
throw new IllegalStateException("'queryExpression' must evaluate to String " +
200-
"or org.springframework.data.mongodb.core.query.Query, but not: " + query);
198+
"or org.springframework.data.mongodb.core.query.Query, but not: " + value);
201199
}
202-
return query;
203200
}
204201

205202
protected String evaluateCollectionNameExpression() {
@@ -225,18 +222,17 @@ protected Query getByIdInQuery(Collection<?> entities) {
225222
return new Query(criterias.length == 1 ? criterias[0] : new Criteria().orOperator(criterias));
226223
}
227224

228-
@SuppressWarnings("unchecked")
229225
protected Pair<String, Object> idForEntity(Object entity) {
230226
if (entity instanceof String) {
231227
return idFieldFromMap(Document.parse(entity.toString()));
232228
}
233-
if (entity instanceof Map) {
234-
return idFieldFromMap((Map<String, Object>) entity);
229+
if (entity instanceof Map<?, ?> asMap) {
230+
return idFieldFromMap(asMap);
235231
}
236232

237-
MappingContext<? extends MongoPersistentEntity<?>, ?> context = this.mongoConverter.getMappingContext();
233+
var mappingContext = this.mongoConverter.getMappingContext();
238234

239-
MongoPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(entity.getClass());
235+
MongoPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
240236
String idField = persistentEntity.getRequiredIdProperty().getFieldName();
241237
IdentifierAccessor idAccessor = persistentEntity.getIdentifierAccessor(entity);
242238
return Pair.of(idField, idAccessor.getRequiredIdentifier());
@@ -247,24 +243,23 @@ protected Update evaluateUpdateExpression() {
247243
if (this.updateExpression != null) {
248244
Object value = this.updateExpression.getValue(getEvaluationContext());
249245
Assert.notNull(value, "'updateExpression' must not evaluate to null");
250-
Update update;
251246
if (value instanceof String string) {
252-
update = new BasicUpdate(string);
247+
return new BasicUpdate(string);
253248
}
254249
else if (value instanceof Update castUpdate) {
255-
update = castUpdate;
250+
return castUpdate;
256251
}
257252
else {
258253
throw new IllegalStateException("'updateExpression' must evaluate to String " +
259254
"or org.springframework.data.mongodb.core.query.Update");
260255
}
261-
return update;
262256
}
263257
return null;
264258
}
265259

266-
private static Pair<String, Object> idFieldFromMap(Map<String, Object> map) {
267-
return Pair.of(ID_FIELD, Objects.requireNonNull(map.get(ID_FIELD)));
260+
@SuppressWarnings("NullAway")
261+
private static Pair<String, Object> idFieldFromMap(Map<?, ?> map) {
262+
return Pair.of(ID_FIELD, map.get(ID_FIELD));
268263
}
269264

270265
}

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/inbound/MongoDbChangeStreamMessageProducer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public class MongoDbChangeStreamMessageProducer extends MessageProducerSupport {
4444

4545
private Class<?> domainType = Document.class;
4646

47-
@Nullable
48-
private String collection;
47+
private @Nullable String collection;
4948

5049
private ChangeStreamOptions options = ChangeStreamOptions.empty();
5150

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/inbound/MongoDbMessageSource.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.jspecify.annotations.Nullable;
2323

24-
import org.springframework.context.ApplicationContext;
2524
import org.springframework.data.mongodb.MongoDatabaseFactory;
2625
import org.springframework.data.mongodb.core.MongoOperations;
2726
import org.springframework.data.mongodb.core.MongoTemplate;
@@ -62,15 +61,14 @@
6261
*/
6362
public class MongoDbMessageSource extends AbstractMongoDbMessageSource<Object> {
6463

65-
@Nullable
66-
private final MongoDatabaseFactory mongoDbFactory;
64+
private final @Nullable MongoDatabaseFactory mongoDbFactory;
6765

6866
@SuppressWarnings("NullAway.Init")
6967
private MongoOperations mongoTemplate;
7068

7169
/**
7270
* Create an instance with the provided {@link MongoDatabaseFactory} and SpEL expression
73-
* which should resolve to a MongoDb 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
71+
* which should resolve to a MongoDb 'query' string.
7472
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
7573
* @param mongoDbFactory The mongodb factory.
7674
* @param queryExpression The query expression.
@@ -83,7 +81,7 @@ public MongoDbMessageSource(MongoDatabaseFactory mongoDbFactory, Expression quer
8381

8482
/**
8583
* Create an instance with the provided {@link MongoOperations} and SpEL expression
86-
* which should resolve to a Mongo 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
84+
* which should resolve to a Mongo 'query' string.
8785
* It assumes that the {@link MongoOperations} is fully initialized and ready to be used.
8886
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
8987
* @param mongoTemplate The mongo template.
@@ -104,10 +102,10 @@ public String getComponentType() {
104102
@Override
105103
protected void onInit() {
106104
super.onInit();
107-
if (this.mongoDbFactory != null) {
105+
if (this.mongoTemplate == null) {
106+
Assert.state(this.mongoDbFactory != null, "'mongoDbFactory' must not be null if 'mongoTemplate' is null.");
108107
MongoTemplate template = new MongoTemplate(this.mongoDbFactory, getMongoConverter());
109-
ApplicationContext applicationContext = getApplicationContext();
110-
template.setApplicationContext(applicationContext);
108+
template.setApplicationContext(getApplicationContext());
111109
this.mongoTemplate = template;
112110
}
113111
setMongoConverter(this.mongoTemplate.getConverter());

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/inbound/ReactiveMongoDbMessageSource.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import reactor.core.publisher.Flux;
2222
import reactor.core.publisher.Mono;
2323

24-
import org.springframework.context.ApplicationContext;
2524
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
2625
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
2726
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
@@ -54,15 +53,14 @@
5453
*/
5554
public class ReactiveMongoDbMessageSource extends AbstractMongoDbMessageSource<Publisher<?>> {
5655

57-
@Nullable
58-
private final ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory;
56+
private final @Nullable ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory;
5957

6058
@SuppressWarnings("NullAway.Init")
6159
private ReactiveMongoOperations reactiveMongoTemplate;
6260

6361
/**
6462
* Create an instance with the provided {@link ReactiveMongoDatabaseFactory} and SpEL expression
65-
* which should resolve to a MongoDb 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
63+
* which should resolve to a MongoDb 'query' string.
6664
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
6765
* @param reactiveMongoDatabaseFactory The reactiveMongoDatabaseFactory factory.
6866
* @param queryExpression The query expression.
@@ -77,7 +75,7 @@ public ReactiveMongoDbMessageSource(ReactiveMongoDatabaseFactory reactiveMongoDa
7775

7876
/**
7977
* Create an instance with the provided {@link ReactiveMongoOperations} and SpEL expression
80-
* which should resolve to a Mongo 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
78+
* which should resolve to a Mongo 'query' string.
8179
* It assumes that the {@link ReactiveMongoOperations} is fully initialized and ready to be used.
8280
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
8381
* @param reactiveMongoTemplate The reactive Mongo template.
@@ -98,11 +96,12 @@ public String getComponentType() {
9896
@Override
9997
protected void onInit() {
10098
super.onInit();
101-
if (this.reactiveMongoDatabaseFactory != null) {
99+
if (this.reactiveMongoTemplate == null) {
100+
Assert.state(this.reactiveMongoDatabaseFactory != null,
101+
"'reactiveMongoDatabaseFactory' must not be null if 'reactiveMongoTemplate' is null.");
102102
ReactiveMongoTemplate template =
103103
new ReactiveMongoTemplate(this.reactiveMongoDatabaseFactory, getMongoConverter());
104-
ApplicationContext applicationContext = getApplicationContext();
105-
template.setApplicationContext(applicationContext);
104+
template.setApplicationContext(getApplicationContext());
106105
this.reactiveMongoTemplate = template;
107106
}
108107
setMongoConverter(this.reactiveMongoTemplate.getConverter());

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/outbound/MongoDbOutboundGateway.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.integration.mongodb.outbound;
1818

19-
import java.util.Objects;
20-
2119
import org.bson.Document;
2220
import org.jspecify.annotations.Nullable;
2321

@@ -143,17 +141,16 @@ protected void doInit() {
143141
throw new IllegalStateException("query and collectionCallback are mutually exclusive");
144142
}
145143

146-
if (this.evaluationContext == null) {
147-
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory());
144+
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory());
148145

149-
TypeLocator typeLocator = this.evaluationContext.getTypeLocator();
150-
if (typeLocator instanceof StandardTypeLocator) {
151-
((StandardTypeLocator) typeLocator).registerImport(Query.class.getPackage().getName());
152-
}
146+
TypeLocator typeLocator = this.evaluationContext.getTypeLocator();
147+
if (typeLocator instanceof StandardTypeLocator) {
148+
((StandardTypeLocator) typeLocator).registerImport(Query.class.getPackage().getName());
153149
}
154150

155151
if (this.mongoTemplate == null) {
156-
this.mongoTemplate = new MongoTemplate(Objects.requireNonNull(this.mongoDbFactory), this.mongoConverter);
152+
Assert.state(this.mongoDbFactory != null, "'mongoDbFactory' must not be null if 'mongoTemplate' is null.");
153+
this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mongoConverter);
157154
}
158155
}
159156

@@ -162,32 +159,29 @@ protected void doInit() {
162159
String collectionName =
163160
this.collectionNameExpression.getValue(this.evaluationContext, requestMessage, String.class);
164161
Assert.notNull(collectionName, "'collectionNameExpression' cannot evaluate to null");
165-
Object result;
166162

167163
var collectionCallbackToUse = this.collectionCallback;
168164
if (collectionCallbackToUse != null) {
169-
result = this.mongoTemplate.execute(collectionName,
165+
return this.mongoTemplate.execute(collectionName,
170166
collection -> collectionCallbackToUse.doInCollection(collection, requestMessage));
171167
}
172168
else {
173169
Query query = buildQuery(requestMessage);
174170

175171
if (this.expectSingleResult) {
176-
result = this.mongoTemplate.findOne(query, this.entityClass, collectionName);
172+
return this.mongoTemplate.findOne(query, this.entityClass, collectionName);
177173
}
178174
else {
179-
result = this.mongoTemplate.find(query, this.entityClass, collectionName);
175+
return this.mongoTemplate.find(query, this.entityClass, collectionName);
180176
}
181177
}
182-
183-
return result;
184178
}
185179

186180
private Query buildQuery(Message<?> requestMessage) {
187181
Query query;
188-
Objects.requireNonNull(this.queryExpression);
189182
Object expressionValue =
190-
this.queryExpression.getValue(this.evaluationContext, requestMessage, Object.class);
183+
getRequiredQueryExpression()
184+
.getValue(this.evaluationContext, requestMessage, Object.class);
191185

192186
if (expressionValue instanceof String) {
193187
query = new BasicQuery((String) expressionValue);
@@ -203,4 +197,9 @@ else if (expressionValue instanceof Query) {
203197
return query;
204198
}
205199

200+
@SuppressWarnings("NullAway") // The method is called from the place where we sure that it is not null
201+
private Expression getRequiredQueryExpression() {
202+
return this.queryExpression;
203+
}
204+
206205
}

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/outbound/MongoDbStoringMessageHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.integration.mongodb.outbound;
1818

19-
import java.util.Objects;
20-
2119
import org.jspecify.annotations.Nullable;
2220

2321
import org.springframework.data.mongodb.MongoDatabaseFactory;
@@ -112,7 +110,8 @@ protected void onInit() {
112110
this.evaluationContext =
113111
ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory());
114112
if (this.mongoTemplate == null) {
115-
this.mongoTemplate = new MongoTemplate(Objects.requireNonNull(this.mongoDbFactory), this.mongoConverter);
113+
Assert.state(this.mongoDbFactory != null, "'mongoDbFactory' must not be null if 'mongoTemplate' is null.");
114+
this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mongoConverter);
116115
}
117116
this.initialized = true;
118117
}

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/outbound/ReactiveMongoDbStoringMessageHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.integration.mongodb.outbound;
1818

19-
import java.util.Objects;
20-
2119
import org.jspecify.annotations.Nullable;
2220
import reactor.core.publisher.Mono;
2321

@@ -110,7 +108,9 @@ protected void onInit() {
110108
super.onInit();
111109
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
112110
if (this.mongoTemplate == null) {
113-
ReactiveMongoTemplate template = new ReactiveMongoTemplate(Objects.requireNonNull(this.mongoDbFactory), this.mongoConverter);
111+
Assert.state(this.mongoDbFactory != null, "'mongoDbFactory' must not be null if 'mongoTemplate' is null.");
112+
113+
ReactiveMongoTemplate template = new ReactiveMongoTemplate(this.mongoDbFactory, this.mongoConverter);
114114
template.setApplicationContext(getApplicationContext());
115115
this.mongoTemplate = template;
116116
}

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/AbstractConfigurableMongoDbMessageStore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Iterator;
2222
import java.util.List;
2323
import java.util.Map;
24-
import java.util.Objects;
2524
import java.util.UUID;
2625

2726
import org.apache.commons.logging.Log;
@@ -148,7 +147,7 @@ protected MessageBuilderFactory getMessageBuilderFactory() {
148147
@Override
149148
public void afterPropertiesSet() {
150149
if (this.mongoTemplate == null) {
151-
Objects.requireNonNull(this.mongoDbFactory);
150+
Assert.state(this.mongoDbFactory != null, "'mongoDbFactory' must not be null if 'mongoTemplate' is null.");
152151
if (this.mappingMongoConverter == null) {
153152
this.mappingMongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(this.mongoDbFactory),
154153
new MongoMappingContext());

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/ConfigurableMongoDbMessageStore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ protected void doRemoveMessagesFromGroup(Object groupId, Collection<Message<?>>
205205
}
206206

207207
@Override
208-
@Nullable
209-
public Message<?> getMessageFromGroup(Object groupId, UUID messageId) {
208+
public @Nullable Message<?> getMessageFromGroup(Object groupId, UUID messageId) {
210209
Assert.notNull(groupId, GROUP_ID_MUST_NOT_BE_NULL);
211210
Assert.notNull(messageId, "'messageId' must not be null");
212211
Query query =

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/MongoDbMessageStore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ private void bulkRemove(Object groupId, Collection<UUID> ids) {
358358
}
359359

360360
@Override
361-
@Nullable
362-
public Message<?> getMessageFromGroup(Object groupId, UUID messageId) {
361+
public @Nullable Message<?> getMessageFromGroup(Object groupId, UUID messageId) {
363362
Assert.notNull(groupId, GROUP_ID_MUST_NOT_BE_NULL);
364363
Assert.notNull(messageId, "'messageId' must not be null");
365364
MessageWrapper messageWrapper =

0 commit comments

Comments
 (0)