Skip to content

Commit f4b61cd

Browse files
authored
Apply Nullability to spring-integration-jpa
Related to: #10083 Signed-off-by: Jiandong Ma <[email protected]>
1 parent 5cebdeb commit f4b61cd

18 files changed

+87
-63
lines changed
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

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

19+
import java.util.Objects;
20+
1921
import jakarta.persistence.EntityManager;
2022
import jakarta.persistence.EntityManagerFactory;
23+
import org.jspecify.annotations.Nullable;
2124

2225
import org.springframework.beans.factory.InitializingBean;
2326
import org.springframework.orm.jpa.SharedEntityManagerCreator;
@@ -32,17 +35,17 @@
3235
*/
3336
abstract class AbstractJpaOperations implements JpaOperations, InitializingBean {
3437

35-
private EntityManager entityManager;
38+
private @Nullable EntityManager entityManager;
3639

37-
private EntityManagerFactory entityManagerFactory;
40+
private @Nullable EntityManagerFactory entityManagerFactory;
3841

3942
public void setEntityManager(EntityManager entityManager) {
4043
Assert.notNull(entityManager, "The provided entityManager must not be null.");
4144
this.entityManager = entityManager;
4245
}
4346

4447
protected EntityManager getEntityManager() {
45-
return this.entityManager;
48+
return Objects.requireNonNull(this.entityManager);
4649
}
4750

4851
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
@@ -72,7 +75,7 @@ protected void onInit() {
7275

7376
@Override
7477
public void flush() {
75-
this.entityManager.flush();
78+
Objects.requireNonNull(this.entityManager).flush();
7679
}
7780

7881
}

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: 27 additions & 22 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,10 +111,12 @@ 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+
@SuppressWarnings("NullAway.Init")
116117
private BeanFactory beanFactory;
117118

119+
@SuppressWarnings("NullAway.Init")
118120
private EvaluationContext evaluationContext;
119121

120122
/**
@@ -157,6 +159,10 @@ public JpaExecutor(JpaOperations jpaOperations) {
157159
this.jpaOperations = jpaOperations;
158160
}
159161

162+
/**
163+
* @deprecated in favor of the one obtained from the application context.
164+
*/
165+
@Deprecated(since = "7.0", forRemoval = true)
160166
public void setIntegrationEvaluationContext(EvaluationContext evaluationContext) {
161167
this.evaluationContext = evaluationContext;
162168
}
@@ -398,10 +404,7 @@ public void afterPropertiesSet() {
398404
else if (this.flush) {
399405
this.flushSize = 1;
400406
}
401-
402-
if (this.evaluationContext == null) {
403-
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
404-
}
407+
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
405408
}
406409

407410
/**
@@ -414,7 +417,7 @@ else if (this.flush) {
414417
* @return Either the number of affected entities when using a JPQL query.
415418
* When using a merge/persist the updated/inserted itself is returned.
416419
*/
417-
public Object executeOutboundJpaOperation(Message<?> message) {
420+
public @Nullable Object executeOutboundJpaOperation(Message<?> message) {
418421
ParameterSource paramSource = null;
419422
if (this.jpaQuery != null || this.nativeQuery != null || this.namedQuery != null) {
420423
paramSource = determineParameterSource(message);
@@ -433,7 +436,7 @@ else if (this.namedQuery != null) {
433436
}
434437
}
435438

436-
private Object executeOutboundJpaOperationOnPersistentMode(Message<?> message) {
439+
private @Nullable Object executeOutboundJpaOperationOnPersistentMode(Message<?> message) {
437440
Object payload = message.getPayload();
438441
switch (this.persistMode) {
439442
case PERSIST -> {
@@ -511,8 +514,9 @@ public Object poll(@Nullable final Message<?> requestMessage) {
511514
payload = result.iterator().next();
512515
}
513516
else {
514-
throw new MessagingException(requestMessage, // NOSONAR
515-
"The Jpa operation returned more than 1 result for expectSingleResult mode.");
517+
String description = "The Jpa operation returned more than 1 result for expectSingleResult mode.";
518+
throw requestMessage == null ? new MessagingException(description)
519+
: new MessagingException(requestMessage, description);
516520
}
517521
}
518522
else {
@@ -546,7 +550,7 @@ private void checkDelete(@Nullable Object payload) {
546550
}
547551
}
548552

549-
protected List<?> doPoll(ParameterSource jpaQLParameterSource, int firstResult, int maxNumberOfResults) {
553+
protected List<?> doPoll(@Nullable ParameterSource jpaQLParameterSource, int firstResult, int maxNumberOfResults) {
550554
List<?> payload;
551555
if (this.jpaQuery != null) {
552556
payload =
@@ -609,7 +613,8 @@ else if (evaluationResult instanceof String) {
609613
}
610614

611615
private ParameterSource determineParameterSource(final Message<?> requestMessage) {
612-
if (this.usePayloadAsParameterSource) {
616+
Assert.state(this.parameterSourceFactory != null, "'parameterSourceFactory' must not be null");
617+
if (Boolean.TRUE.equals(this.usePayloadAsParameterSource)) {
613618
return this.parameterSourceFactory.createParameterSource(requestMessage.getPayload());
614619
}
615620
else {

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;

0 commit comments

Comments
 (0)