Skip to content

Commit ac8db5d

Browse files
committed
Add nullability annotations to module/spring-boot-jpa
See gh-46587
1 parent 78ec5ce commit ac8db5d

File tree

6 files changed

+49
-28
lines changed

6 files changed

+49
-28
lines changed

config/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,5 @@
9191
<suppress files="Bindable\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
9292
<suppress files="LambdaSafe\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
9393
<suppress files="ConditionMessage\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
94+
<suppress files="EntityManagerFactoryBuilder\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
9495
</suppressions>

module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/EntityManagerFactoryBuilder.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import javax.sql.DataSource;
2828

29+
import org.jspecify.annotations.Nullable;
30+
2931
import org.springframework.core.task.AsyncTaskExecutor;
3032
import org.springframework.orm.jpa.JpaVendorAdapter;
3133
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -53,15 +55,15 @@ public class EntityManagerFactoryBuilder {
5355

5456
private final JpaVendorAdapter jpaVendorAdapter;
5557

56-
private final PersistenceUnitManager persistenceUnitManager;
58+
private final @Nullable PersistenceUnitManager persistenceUnitManager;
5759

5860
private final Function<DataSource, Map<String, ?>> jpaPropertiesFactory;
5961

60-
private final URL persistenceUnitRootLocation;
62+
private final @Nullable URL persistenceUnitRootLocation;
6163

62-
private AsyncTaskExecutor bootstrapExecutor;
64+
private @Nullable AsyncTaskExecutor bootstrapExecutor;
6365

64-
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
66+
private PersistenceUnitPostProcessor @Nullable [] persistenceUnitPostProcessors;
6567

6668
/**
6769
* Create a new instance passing in the common pieces that will be shared if multiple
@@ -74,7 +76,8 @@ public class EntityManagerFactoryBuilder {
7476
* @since 3.4.4
7577
*/
7678
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
77-
Function<DataSource, Map<String, ?>> jpaPropertiesFactory, PersistenceUnitManager persistenceUnitManager) {
79+
Function<DataSource, Map<String, ?>> jpaPropertiesFactory,
80+
@Nullable PersistenceUnitManager persistenceUnitManager) {
7881
this(jpaVendorAdapter, jpaPropertiesFactory, persistenceUnitManager, null);
7982
}
8083

@@ -91,8 +94,8 @@ public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
9194
* @since 3.4.4
9295
*/
9396
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
94-
Function<DataSource, Map<String, ?>> jpaPropertiesFactory, PersistenceUnitManager persistenceUnitManager,
95-
URL persistenceUnitRootLocation) {
97+
Function<DataSource, Map<String, ?>> jpaPropertiesFactory,
98+
@Nullable PersistenceUnitManager persistenceUnitManager, @Nullable URL persistenceUnitRootLocation) {
9699
this.jpaVendorAdapter = jpaVendorAdapter;
97100
this.persistenceUnitManager = persistenceUnitManager;
98101
this.jpaPropertiesFactory = jpaPropertiesFactory;
@@ -137,15 +140,15 @@ public final class Builder {
137140

138141
private final DataSource dataSource;
139142

140-
private PersistenceManagedTypes managedTypes;
143+
private @Nullable PersistenceManagedTypes managedTypes;
141144

142-
private String[] packagesToScan;
145+
private String @Nullable [] packagesToScan;
143146

144-
private String persistenceUnit;
147+
private @Nullable String persistenceUnit;
145148

146149
private final Map<String, Object> properties = new HashMap<>();
147150

148-
private String[] mappingResources;
151+
private String @Nullable [] mappingResources;
149152

150153
private boolean jta;
151154

@@ -159,7 +162,7 @@ private Builder(DataSource dataSource) {
159162
* @param managedTypes managed types.
160163
* @return the builder for fluent usage
161164
*/
162-
public Builder managedTypes(PersistenceManagedTypes managedTypes) {
165+
public Builder managedTypes(@Nullable PersistenceManagedTypes managedTypes) {
163166
this.managedTypes = managedTypes;
164167
return this;
165168
}
@@ -170,7 +173,7 @@ public Builder managedTypes(PersistenceManagedTypes managedTypes) {
170173
* @return the builder for fluent usage
171174
* @see #managedTypes(PersistenceManagedTypes)
172175
*/
173-
public Builder packages(String... packagesToScan) {
176+
public Builder packages(String @Nullable ... packagesToScan) {
174177
this.packagesToScan = packagesToScan;
175178
return this;
176179
}
@@ -197,7 +200,7 @@ public Builder packages(Class<?>... basePackageClasses) {
197200
* @param persistenceUnit the name of the persistence unit
198201
* @return the builder for fluent usage
199202
*/
200-
public Builder persistenceUnit(String persistenceUnit) {
203+
public Builder persistenceUnit(@Nullable String persistenceUnit) {
201204
this.persistenceUnit = persistenceUnit;
202205
return this;
203206
}
@@ -223,7 +226,7 @@ public Builder properties(Map<String, ?> properties) {
223226
* @param mappingResources the mapping resources to use
224227
* @return the builder for fluent usage
225228
*/
226-
public Builder mappingResources(String... mappingResources) {
229+
public Builder mappingResources(String @Nullable ... mappingResources) {
227230
this.mappingResources = mappingResources;
228231
return this;
229232
}
@@ -264,7 +267,7 @@ public LocalContainerEntityManagerFactoryBean build() {
264267
entityManagerFactoryBean.setManagedTypes(this.managedTypes);
265268
}
266269
else {
267-
entityManagerFactoryBean.setPackagesToScan(this.packagesToScan);
270+
setPackagesToScan(entityManagerFactoryBean);
268271
}
269272
Map<String, ?> jpaProperties = EntityManagerFactoryBuilder.this.jpaPropertiesFactory.apply(this.dataSource);
270273
entityManagerFactoryBean.getJpaPropertyMap().putAll(new LinkedHashMap<>(jpaProperties));
@@ -286,6 +289,14 @@ public LocalContainerEntityManagerFactoryBean build() {
286289
return entityManagerFactoryBean;
287290
}
288291

292+
// TODO: Review this. The test
293+
// HibernateJpaAutoConfigurationTests.usesManuallyDefinedLocalContainerEntityManagerFactoryBeanUsingBuilder
294+
// fails if an non-null assert is added
295+
@SuppressWarnings("NullAway")
296+
private void setPackagesToScan(LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
297+
entityManagerFactoryBean.setPackagesToScan(this.packagesToScan);
298+
}
299+
289300
}
290301

291302
}

module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/autoconfigure/JpaBaseConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import jakarta.persistence.EntityManagerFactory;
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
28+
import org.jspecify.annotations.Nullable;
2829

2930
import org.springframework.beans.factory.BeanFactory;
3031
import org.springframework.beans.factory.ObjectProvider;
@@ -82,7 +83,7 @@ public abstract class JpaBaseConfiguration {
8283

8384
private final JpaProperties properties;
8485

85-
private final JtaTransactionManager jtaTransactionManager;
86+
private final @Nullable JtaTransactionManager jtaTransactionManager;
8687

8788
protected JpaBaseConfiguration(DataSource dataSource, JpaProperties properties,
8889
ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
@@ -163,7 +164,7 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManager
163164
protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
164165
}
165166

166-
private String[] getMappingResources() {
167+
private String @Nullable [] getMappingResources() {
167168
List<String> mappingResources = this.properties.getMappingResources();
168169
return (!ObjectUtils.isEmpty(mappingResources) ? StringUtils.toStringArray(mappingResources) : null);
169170
}
@@ -172,7 +173,7 @@ private String[] getMappingResources() {
172173
* Return the JTA transaction manager.
173174
* @return the transaction manager or {@code null}
174175
*/
175-
protected JtaTransactionManager getJtaTransactionManager() {
176+
protected @Nullable JtaTransactionManager getJtaTransactionManager() {
176177
return this.jtaTransactionManager;
177178
}
178179

module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/autoconfigure/JpaProperties.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.jspecify.annotations.Nullable;
25+
2426
import org.springframework.boot.context.properties.ConfigurationProperties;
2527
import org.springframework.orm.jpa.vendor.Database;
2628

@@ -51,13 +53,13 @@ public class JpaProperties {
5153
* Name of the target database to operate on, auto-detected by default. Can be
5254
* alternatively set using the "Database" enum.
5355
*/
54-
private String databasePlatform;
56+
private @Nullable String databasePlatform;
5557

5658
/**
5759
* Target database to operate on, auto-detected by default. Can be alternatively set
5860
* using the "databasePlatform" property.
5961
*/
60-
private Database database;
62+
private @Nullable Database database;
6163

6264
/**
6365
* Whether to initialize the schema on startup.
@@ -73,7 +75,7 @@ public class JpaProperties {
7375
* Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the
7476
* thread for the entire processing of the request.
7577
*/
76-
private Boolean openInView;
78+
private @Nullable Boolean openInView;
7779

7880
public Map<String, String> getProperties() {
7981
return this.properties;
@@ -87,19 +89,19 @@ public List<String> getMappingResources() {
8789
return this.mappingResources;
8890
}
8991

90-
public String getDatabasePlatform() {
92+
public @Nullable String getDatabasePlatform() {
9193
return this.databasePlatform;
9294
}
9395

94-
public void setDatabasePlatform(String databasePlatform) {
96+
public void setDatabasePlatform(@Nullable String databasePlatform) {
9597
this.databasePlatform = databasePlatform;
9698
}
9799

98-
public Database getDatabase() {
100+
public @Nullable Database getDatabase() {
99101
return this.database;
100102
}
101103

102-
public void setDatabase(Database database) {
104+
public void setDatabase(@Nullable Database database) {
103105
this.database = database;
104106
}
105107

@@ -119,11 +121,11 @@ public void setShowSql(boolean showSql) {
119121
this.showSql = showSql;
120122
}
121123

122-
public Boolean getOpenInView() {
124+
public @Nullable Boolean getOpenInView() {
123125
return this.openInView;
124126
}
125127

126-
public void setOpenInView(Boolean openInView) {
128+
public void setOpenInView(@Nullable Boolean openInView) {
127129
this.openInView = openInView;
128130
}
129131

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

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

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

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

0 commit comments

Comments
 (0)