Skip to content

Commit 9bdd090

Browse files
committed
Apply Nullability to spring-integration-jpa
related to: #10083 Signed-off-by: Jiandong Ma <[email protected]>
1 parent f5da99d commit 9bdd090

18 files changed

+80
-57
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Provides parser classes to provide Xml namespace support for the Jpa components.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.jpa.config.xml;

spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/AbstractJpaOperations.java

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

1919
import jakarta.persistence.EntityManager;
2020
import jakarta.persistence.EntityManagerFactory;
21+
import org.jspecify.annotations.Nullable;
2122

2223
import org.springframework.beans.factory.InitializingBean;
2324
import org.springframework.orm.jpa.SharedEntityManagerCreator;
@@ -32,16 +33,17 @@
3233
*/
3334
abstract class AbstractJpaOperations implements JpaOperations, InitializingBean {
3435

35-
private EntityManager entityManager;
36+
private @Nullable EntityManager entityManager;
3637

37-
private EntityManagerFactory entityManagerFactory;
38+
private @Nullable EntityManagerFactory entityManagerFactory;
3839

3940
public void setEntityManager(EntityManager entityManager) {
4041
Assert.notNull(entityManager, "The provided entityManager must not be null.");
4142
this.entityManager = entityManager;
4243
}
4344

4445
protected EntityManager getEntityManager() {
46+
Assert.state(this.entityManager != null, "'entityManager' must not be null after initialized");
4547
return this.entityManager;
4648
}
4749

@@ -72,6 +74,7 @@ protected void onInit() {
7274

7375
@Override
7476
public void flush() {
77+
Assert.state(this.entityManager != null, "'entityManager' must not be null after initialized");
7578
this.entityManager.flush();
7679
}
7780

spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/DefaultJpaOperations.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import jakarta.persistence.EntityManager;
2626
import jakarta.persistence.Parameter;
2727
import jakarta.persistence.Query;
28+
import org.jspecify.annotations.Nullable;
2829

2930
import org.springframework.core.log.LogAccessor;
3031
import org.springframework.integration.jpa.support.JpaUtils;
3132
import org.springframework.integration.jpa.support.parametersource.ParameterSource;
3233
import org.springframework.integration.jpa.support.parametersource.PositionSupportingParameterSource;
33-
import org.springframework.lang.Nullable;
3434
import org.springframework.util.Assert;
3535
import org.springframework.util.StringUtils;
3636

@@ -74,7 +74,7 @@ public void deleteInBatch(Iterable<?> entities) {
7474
}
7575
}
7676
}
77-
77+
Assert.state(entityClass != null, "'entityClass' must not be null");
7878
EntityManager entityManager = getEntityManager();
7979
final String entityName = JpaUtils.getEntityName(entityManager, entityClass);
8080
final String queryString = JpaUtils.getQueryString(JpaUtils.DELETE_ALL_QUERY_STRING, entityName);
@@ -85,21 +85,21 @@ public void deleteInBatch(Iterable<?> entities) {
8585
}
8686

8787
@Override
88-
public int executeUpdate(String updateQuery, ParameterSource source) {
88+
public int executeUpdate(String updateQuery, @Nullable ParameterSource source) {
8989
Query query = getEntityManager().createQuery(updateQuery);
9090
setParametersIfRequired(updateQuery, source, query);
9191
return query.executeUpdate();
9292
}
9393

9494
@Override
95-
public int executeUpdateWithNamedQuery(String updateQuery, ParameterSource source) {
95+
public int executeUpdateWithNamedQuery(String updateQuery, @Nullable ParameterSource source) {
9696
Query query = getEntityManager().createNamedQuery(updateQuery);
9797
setParametersIfRequired(updateQuery, source, query);
9898
return query.executeUpdate();
9999
}
100100

101101
@Override
102-
public int executeUpdateWithNativeQuery(String updateQuery, ParameterSource source) {
102+
public int executeUpdateWithNativeQuery(String updateQuery, @Nullable ParameterSource source) {
103103
Query query = getEntityManager().createNativeQuery(updateQuery);
104104
setParametersIfRequired(updateQuery, source, query);
105105
return query.executeUpdate();
@@ -110,7 +110,7 @@ public <T> T find(Class<T> entityType, Object id) {
110110
return getEntityManager().find(entityType, id);
111111
}
112112

113-
private Query getQuery(String queryString, ParameterSource source) {
113+
private Query getQuery(String queryString, @Nullable ParameterSource source) {
114114
Query query = getEntityManager().createQuery(queryString);
115115
setParametersIfRequired(queryString, source, query);
116116
return query;
@@ -134,7 +134,7 @@ public List<?> getResultListForClass(Class<?> entityClass, int firstResult, int
134134

135135
@Override
136136
public List<?> getResultListForNamedQuery(String selectNamedQuery,
137-
ParameterSource parameterSource, int firstResult, int maxNumberOfResults) {
137+
@Nullable ParameterSource parameterSource, int firstResult, int maxNumberOfResults) {
138138

139139
final Query query = getEntityManager().createNamedQuery(selectNamedQuery);
140140
setParametersIfRequired(selectNamedQuery, parameterSource, query);
@@ -152,7 +152,7 @@ public List<?> getResultListForNamedQuery(String selectNamedQuery,
152152

153153
@Override
154154
public List<?> getResultListForNativeQuery(String selectQuery, @Nullable Class<?> entityClass,
155-
ParameterSource parameterSource, int firstResult, int maxNumberOfResults) {
155+
@Nullable ParameterSource parameterSource, int firstResult, int maxNumberOfResults) {
156156

157157
final Query query;
158158

@@ -181,7 +181,7 @@ public List<?> getResultListForQuery(String query, ParameterSource source) {
181181
}
182182

183183
@Override
184-
public List<?> getResultListForQuery(String queryString, ParameterSource source,
184+
public List<?> getResultListForQuery(String queryString, @Nullable ParameterSource source,
185185
int firstResult, int maxNumberOfResults) {
186186

187187
Query query = getQuery(queryString, source);
@@ -266,7 +266,7 @@ private Object persistOrMergeIterable(Object entity, boolean isMerge, int flushS
266266
List<Object> mergedEntities = new ArrayList<>();
267267

268268
EntityManager entityManager = getEntityManager();
269-
for (Object iteratedEntity : entities) {
269+
for (@Nullable Object iteratedEntity : entities) {
270270
if (iteratedEntity == null) {
271271
nullEntities.incrementAndGet();
272272
}

spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/JpaExecutor.java

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

2121
import jakarta.persistence.EntityManager;
2222
import jakarta.persistence.EntityManagerFactory;
23+
import org.jspecify.annotations.Nullable;
2324

2425
import org.springframework.beans.BeansException;
2526
import org.springframework.beans.factory.BeanFactory;
@@ -35,7 +36,6 @@
3536
import org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory;
3637
import org.springframework.integration.jpa.support.parametersource.ParameterSource;
3738
import org.springframework.integration.jpa.support.parametersource.ParameterSourceFactory;
38-
import org.springframework.lang.Nullable;
3939
import org.springframework.messaging.Message;
4040
import org.springframework.messaging.MessagingException;
4141
import org.springframework.util.Assert;
@@ -71,27 +71,27 @@ public class JpaExecutor implements InitializingBean, BeanFactoryAware {
7171

7272
private final JpaOperations jpaOperations;
7373

74-
private List<JpaParameter> jpaParameters;
74+
private @Nullable List<JpaParameter> jpaParameters;
7575

76-
private Class<?> entityClass;
76+
private @Nullable Class<?> entityClass;
7777

78-
private String jpaQuery;
78+
private @Nullable String jpaQuery;
7979

80-
private String nativeQuery;
80+
private @Nullable String nativeQuery;
8181

82-
private String namedQuery;
82+
private @Nullable String namedQuery;
8383

84-
private Expression maxResultsExpression;
84+
private @Nullable Expression maxResultsExpression;
8585

86-
private Expression firstResultExpression;
86+
private @Nullable Expression firstResultExpression;
8787

88-
private Expression idExpression;
88+
private @Nullable Expression idExpression;
8989

9090
private PersistMode persistMode = PersistMode.MERGE;
9191

92-
private ParameterSourceFactory parameterSourceFactory = null;
92+
private @Nullable ParameterSourceFactory parameterSourceFactory = null;
9393

94-
private ParameterSource parameterSource;
94+
private @Nullable ParameterSource parameterSource;
9595

9696
private boolean flush = false;
9797

@@ -111,11 +111,11 @@ public class JpaExecutor implements InitializingBean, BeanFactoryAware {
111111
* default a {@link BeanPropertyParameterSourceFactory} implementation is
112112
* used for the sqlParameterSourceFactory property.
113113
*/
114-
private Boolean usePayloadAsParameterSource = null;
114+
private @Nullable Boolean usePayloadAsParameterSource = null;
115115

116-
private BeanFactory beanFactory;
116+
private @Nullable BeanFactory beanFactory;
117117

118-
private EvaluationContext evaluationContext;
118+
private @Nullable EvaluationContext evaluationContext;
119119

120120
/**
121121
* Constructor taking an {@link EntityManagerFactory} from which the
@@ -414,7 +414,7 @@ else if (this.flush) {
414414
* @return Either the number of affected entities when using a JPQL query.
415415
* When using a merge/persist the updated/inserted itself is returned.
416416
*/
417-
public Object executeOutboundJpaOperation(Message<?> message) {
417+
public @Nullable Object executeOutboundJpaOperation(Message<?> message) {
418418
ParameterSource paramSource = null;
419419
if (this.jpaQuery != null || this.nativeQuery != null || this.namedQuery != null) {
420420
paramSource = determineParameterSource(message);
@@ -433,7 +433,7 @@ else if (this.namedQuery != null) {
433433
}
434434
}
435435

436-
private Object executeOutboundJpaOperationOnPersistentMode(Message<?> message) {
436+
private @Nullable Object executeOutboundJpaOperationOnPersistentMode(Message<?> message) {
437437
Object payload = message.getPayload();
438438
switch (this.persistMode) {
439439
case PERSIST -> {
@@ -482,6 +482,7 @@ public Object poll(@Nullable final Message<?> requestMessage) {
482482
final Object payload;
483483

484484
if (this.idExpression != null) {
485+
Assert.state(this.evaluationContext != null, "'evaluationContext' must not be null");
485486
Object id = this.idExpression.getValue(this.evaluationContext, requestMessage); // NOSONAR It can be null
486487
Assert.state(id != null, "The 'idExpression' cannot evaluate to null.");
487488
Class<?> entityClazz = this.entityClass;
@@ -511,8 +512,9 @@ public Object poll(@Nullable final Message<?> requestMessage) {
511512
payload = result.iterator().next();
512513
}
513514
else {
514-
throw new MessagingException(requestMessage, // NOSONAR
515-
"The Jpa operation returned more than 1 result for expectSingleResult mode.");
515+
String description = "The Jpa operation returned more than 1 result for expectSingleResult mode.";
516+
throw requestMessage == null ? new MessagingException(description)
517+
: new MessagingException(requestMessage, description);
516518
}
517519
}
518520
else {
@@ -546,7 +548,7 @@ private void checkDelete(@Nullable Object payload) {
546548
}
547549
}
548550

549-
protected List<?> doPoll(ParameterSource jpaQLParameterSource, int firstResult, int maxNumberOfResults) {
551+
protected List<?> doPoll(@Nullable ParameterSource jpaQLParameterSource, int firstResult, int maxNumberOfResults) {
550552
List<?> payload;
551553
if (this.jpaQuery != null) {
552554
payload =
@@ -583,6 +585,7 @@ private int evaluateExpressionForNumericResult(@Nullable final Message<?> reques
583585

584586
int evaluatedResult = 0;
585587
if (expression != null) {
588+
Assert.state(this.evaluationContext != null, "'evaluationContext' must not be null");
586589
Object evaluationResult = expression.getValue(this.evaluationContext, requestMessage); // NOSONAR can be
587590
// null
588591
if (evaluationResult != null) {
@@ -609,6 +612,8 @@ else if (evaluationResult instanceof String) {
609612
}
610613

611614
private ParameterSource determineParameterSource(final Message<?> requestMessage) {
615+
Assert.state(this.usePayloadAsParameterSource != null, "'usePayloadAsParameterSource' must not be null");
616+
Assert.state(this.parameterSourceFactory != null, "'parameterSourceFactory' must not be null");
612617
if (this.usePayloadAsParameterSource) {
613618
return this.parameterSourceFactory.createParameterSource(requestMessage.getPayload());
614619
}

spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/JpaOperations.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
import java.util.List;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.integration.jpa.support.parametersource.ParameterSource;
22-
import org.springframework.lang.Nullable;
2324

2425
/**
2526
* The Interface containing all the JpaOperations those will be executed by
@@ -51,26 +52,26 @@ public interface JpaOperations {
5152
* Executes the given update statement and uses the given parameter source to
5253
* set the required query parameters.
5354
* @param updateQuery Must Not be empty.
54-
* @param source Must Not be null.
55+
* @param source Can be null.
5556
* @return The number of entities updated
5657
*/
57-
int executeUpdate(String updateQuery, ParameterSource source);
58+
int executeUpdate(String updateQuery, @Nullable ParameterSource source);
5859

5960
/**
6061
*
6162
* @param updateQuery The update query.
6263
* @param source The parameter source.
6364
* @return The number of entities updated.
6465
*/
65-
int executeUpdateWithNamedQuery(String updateQuery, ParameterSource source);
66+
int executeUpdateWithNamedQuery(String updateQuery, @Nullable ParameterSource source);
6667

6768
/**
6869
*
6970
* @param updateQuery The update query.
7071
* @param source The parameter source.
7172
* @return The number of entities updated
7273
*/
73-
int executeUpdateWithNativeQuery(String updateQuery, ParameterSource source);
74+
int executeUpdateWithNativeQuery(String updateQuery, @Nullable ParameterSource source);
7475

7576
/**
7677
* Find an Entity of given type with the given primary key type.
@@ -100,7 +101,7 @@ List<?> getResultListForClass(Class<?> entityClass,
100101
* @param maxNumberOfResults The number of objects to return.
101102
* @return The list of found entities.
102103
*/
103-
List<?> getResultListForNamedQuery(String selectNamedQuery, ParameterSource jpaQLParameterSource,
104+
List<?> getResultListForNamedQuery(String selectNamedQuery, @Nullable ParameterSource jpaQLParameterSource,
104105
int firstResult,
105106
int maxNumberOfResults);
106107

@@ -115,7 +116,7 @@ List<?> getResultListForNamedQuery(String selectNamedQuery, ParameterSource jpaQ
115116
*/
116117
List<?> getResultListForNativeQuery(String selectQuery,
117118
@Nullable Class<?> entityClass,
118-
ParameterSource jpaQLParameterSource,
119+
@Nullable ParameterSource jpaQLParameterSource,
119120
int firstResult,
120121
int maxNumberOfResults);
121122

@@ -135,7 +136,8 @@ List<?> getResultListForNativeQuery(String selectQuery,
135136
* @param source the Parameter source for this query to be executed, if none then set null.
136137
* @return The list of found entities.
137138
*/
138-
List<?> getResultListForQuery(String query, ParameterSource source, int firstResult, int maxNumberOfResults);
139+
List<?> getResultListForQuery(String query, @Nullable ParameterSource source, int firstResult,
140+
int maxNumberOfResults);
139141

140142
/**
141143
* Execute the provided query to return a single element.
@@ -155,7 +157,7 @@ List<?> getResultListForNativeQuery(String selectQuery,
155157
* @param entity Must not be null.
156158
* @return The merged managed instance of the entity.
157159
*/
158-
Object merge(Object entity);
160+
@Nullable Object merge(Object entity);
159161

160162
/**
161163
* The entity to be merged with the {@link jakarta.persistence.EntityManager}.
@@ -174,7 +176,7 @@ List<?> getResultListForNativeQuery(String selectQuery,
174176
* @return The merged object.
175177
*/
176178

177-
Object merge(Object entity, int flushSize, boolean clearOnFlush);
179+
@Nullable Object merge(Object entity, int flushSize, boolean clearOnFlush);
178180

179181
/**
180182
* Persists the entity. The provided object can also be an {@link Iterable}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
22
* Provides core classes of the JPA module.
33
*/
4-
@org.springframework.lang.NonNullApi
4+
@org.jspecify.annotations.NullMarked
55
package org.springframework.integration.jpa.core;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* Provides JPA Components support for Java DSL.
33
*/
4-
@org.springframework.lang.NonNullApi
5-
@org.springframework.lang.NonNullFields
4+
@org.jspecify.annotations.NullMarked
65
package org.springframework.integration.jpa.dsl;

spring-integration-jpa/src/main/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapter.java

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

1717
package org.springframework.integration.jpa.inbound;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import org.springframework.integration.endpoint.AbstractMessageSource;
2022
import org.springframework.integration.jpa.core.JpaExecutor;
2123
import org.springframework.util.Assert;
@@ -76,7 +78,7 @@ protected void onInit() {
7678
* no reason to emit an empty message from this message source.
7779
*/
7880
@Override
79-
protected Object doReceive() {
81+
protected @Nullable Object doReceive() {
8082
Object result = this.jpaExecutor.poll();
8183
return ObjectUtils.isEmpty(result) ? null : result;
8284
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Provides inbound Spring Integration Jpa components.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.jpa.inbound;

0 commit comments

Comments
 (0)