Browse Source

Add nullability annotations to module/spring-boot-jpa

See gh-46587
pull/46651/head
Moritz Halbritter 5 months ago
parent
commit
ac8db5d4e5
  1. 1
      config/checkstyle/checkstyle-suppressions.xml
  2. 43
      module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/EntityManagerFactoryBuilder.java
  3. 7
      module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/autoconfigure/JpaBaseConfiguration.java
  4. 20
      module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/autoconfigure/JpaProperties.java
  5. 3
      module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/autoconfigure/package-info.java
  6. 3
      module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/package-info.java

1
config/checkstyle/checkstyle-suppressions.xml

@ -91,4 +91,5 @@
<suppress files="Bindable\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/> <suppress files="Bindable\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<suppress files="LambdaSafe\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/> <suppress files="LambdaSafe\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<suppress files="ConditionMessage\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/> <suppress files="ConditionMessage\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
<suppress files="EntityManagerFactoryBuilder\.java" checks="NoWhitespaceBefore" message="'...' is preceded with whitespace"/>
</suppressions> </suppressions>

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

@ -26,6 +26,8 @@ import java.util.function.Function;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.jspecify.annotations.Nullable;
import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -53,15 +55,15 @@ public class EntityManagerFactoryBuilder {
private final JpaVendorAdapter jpaVendorAdapter; private final JpaVendorAdapter jpaVendorAdapter;
private final PersistenceUnitManager persistenceUnitManager; private final @Nullable PersistenceUnitManager persistenceUnitManager;
private final Function<DataSource, Map<String, ?>> jpaPropertiesFactory; private final Function<DataSource, Map<String, ?>> jpaPropertiesFactory;
private final URL persistenceUnitRootLocation; private final @Nullable URL persistenceUnitRootLocation;
private AsyncTaskExecutor bootstrapExecutor; private @Nullable AsyncTaskExecutor bootstrapExecutor;
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors; private PersistenceUnitPostProcessor @Nullable [] persistenceUnitPostProcessors;
/** /**
* Create a new instance passing in the common pieces that will be shared if multiple * Create a new instance passing in the common pieces that will be shared if multiple
@ -74,7 +76,8 @@ public class EntityManagerFactoryBuilder {
* @since 3.4.4 * @since 3.4.4
*/ */
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter, public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
Function<DataSource, Map<String, ?>> jpaPropertiesFactory, PersistenceUnitManager persistenceUnitManager) { Function<DataSource, Map<String, ?>> jpaPropertiesFactory,
@Nullable PersistenceUnitManager persistenceUnitManager) {
this(jpaVendorAdapter, jpaPropertiesFactory, persistenceUnitManager, null); this(jpaVendorAdapter, jpaPropertiesFactory, persistenceUnitManager, null);
} }
@ -91,8 +94,8 @@ public class EntityManagerFactoryBuilder {
* @since 3.4.4 * @since 3.4.4
*/ */
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter, public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
Function<DataSource, Map<String, ?>> jpaPropertiesFactory, PersistenceUnitManager persistenceUnitManager, Function<DataSource, Map<String, ?>> jpaPropertiesFactory,
URL persistenceUnitRootLocation) { @Nullable PersistenceUnitManager persistenceUnitManager, @Nullable URL persistenceUnitRootLocation) {
this.jpaVendorAdapter = jpaVendorAdapter; this.jpaVendorAdapter = jpaVendorAdapter;
this.persistenceUnitManager = persistenceUnitManager; this.persistenceUnitManager = persistenceUnitManager;
this.jpaPropertiesFactory = jpaPropertiesFactory; this.jpaPropertiesFactory = jpaPropertiesFactory;
@ -137,15 +140,15 @@ public class EntityManagerFactoryBuilder {
private final DataSource dataSource; private final DataSource dataSource;
private PersistenceManagedTypes managedTypes; private @Nullable PersistenceManagedTypes managedTypes;
private String[] packagesToScan; private String @Nullable [] packagesToScan;
private String persistenceUnit; private @Nullable String persistenceUnit;
private final Map<String, Object> properties = new HashMap<>(); private final Map<String, Object> properties = new HashMap<>();
private String[] mappingResources; private String @Nullable [] mappingResources;
private boolean jta; private boolean jta;
@ -159,7 +162,7 @@ public class EntityManagerFactoryBuilder {
* @param managedTypes managed types. * @param managedTypes managed types.
* @return the builder for fluent usage * @return the builder for fluent usage
*/ */
public Builder managedTypes(PersistenceManagedTypes managedTypes) { public Builder managedTypes(@Nullable PersistenceManagedTypes managedTypes) {
this.managedTypes = managedTypes; this.managedTypes = managedTypes;
return this; return this;
} }
@ -170,7 +173,7 @@ public class EntityManagerFactoryBuilder {
* @return the builder for fluent usage * @return the builder for fluent usage
* @see #managedTypes(PersistenceManagedTypes) * @see #managedTypes(PersistenceManagedTypes)
*/ */
public Builder packages(String... packagesToScan) { public Builder packages(String @Nullable ... packagesToScan) {
this.packagesToScan = packagesToScan; this.packagesToScan = packagesToScan;
return this; return this;
} }
@ -197,7 +200,7 @@ public class EntityManagerFactoryBuilder {
* @param persistenceUnit the name of the persistence unit * @param persistenceUnit the name of the persistence unit
* @return the builder for fluent usage * @return the builder for fluent usage
*/ */
public Builder persistenceUnit(String persistenceUnit) { public Builder persistenceUnit(@Nullable String persistenceUnit) {
this.persistenceUnit = persistenceUnit; this.persistenceUnit = persistenceUnit;
return this; return this;
} }
@ -223,7 +226,7 @@ public class EntityManagerFactoryBuilder {
* @param mappingResources the mapping resources to use * @param mappingResources the mapping resources to use
* @return the builder for fluent usage * @return the builder for fluent usage
*/ */
public Builder mappingResources(String... mappingResources) { public Builder mappingResources(String @Nullable ... mappingResources) {
this.mappingResources = mappingResources; this.mappingResources = mappingResources;
return this; return this;
} }
@ -264,7 +267,7 @@ public class EntityManagerFactoryBuilder {
entityManagerFactoryBean.setManagedTypes(this.managedTypes); entityManagerFactoryBean.setManagedTypes(this.managedTypes);
} }
else { else {
entityManagerFactoryBean.setPackagesToScan(this.packagesToScan); setPackagesToScan(entityManagerFactoryBean);
} }
Map<String, ?> jpaProperties = EntityManagerFactoryBuilder.this.jpaPropertiesFactory.apply(this.dataSource); Map<String, ?> jpaProperties = EntityManagerFactoryBuilder.this.jpaPropertiesFactory.apply(this.dataSource);
entityManagerFactoryBean.getJpaPropertyMap().putAll(new LinkedHashMap<>(jpaProperties)); entityManagerFactoryBean.getJpaPropertyMap().putAll(new LinkedHashMap<>(jpaProperties));
@ -286,6 +289,14 @@ public class EntityManagerFactoryBuilder {
return entityManagerFactoryBean; return entityManagerFactoryBean;
} }
// TODO: Review this. The test
// HibernateJpaAutoConfigurationTests.usesManuallyDefinedLocalContainerEntityManagerFactoryBeanUsingBuilder
// fails if an non-null assert is added
@SuppressWarnings("NullAway")
private void setPackagesToScan(LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
entityManagerFactoryBean.setPackagesToScan(this.packagesToScan);
}
} }
} }

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

@ -25,6 +25,7 @@ import javax.sql.DataSource;
import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.EntityManagerFactory;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
@ -82,7 +83,7 @@ public abstract class JpaBaseConfiguration {
private final JpaProperties properties; private final JpaProperties properties;
private final JtaTransactionManager jtaTransactionManager; private final @Nullable JtaTransactionManager jtaTransactionManager;
protected JpaBaseConfiguration(DataSource dataSource, JpaProperties properties, protected JpaBaseConfiguration(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManager) { ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
@ -163,7 +164,7 @@ public abstract class JpaBaseConfiguration {
protected void customizeVendorProperties(Map<String, Object> vendorProperties) { protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
} }
private String[] getMappingResources() { private String @Nullable [] getMappingResources() {
List<String> mappingResources = this.properties.getMappingResources(); List<String> mappingResources = this.properties.getMappingResources();
return (!ObjectUtils.isEmpty(mappingResources) ? StringUtils.toStringArray(mappingResources) : null); return (!ObjectUtils.isEmpty(mappingResources) ? StringUtils.toStringArray(mappingResources) : null);
} }
@ -172,7 +173,7 @@ public abstract class JpaBaseConfiguration {
* Return the JTA transaction manager. * Return the JTA transaction manager.
* @return the transaction manager or {@code null} * @return the transaction manager or {@code null}
*/ */
protected JtaTransactionManager getJtaTransactionManager() { protected @Nullable JtaTransactionManager getJtaTransactionManager() {
return this.jtaTransactionManager; return this.jtaTransactionManager;
} }

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

@ -21,6 +21,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.orm.jpa.vendor.Database; import org.springframework.orm.jpa.vendor.Database;
@ -51,13 +53,13 @@ public class JpaProperties {
* Name of the target database to operate on, auto-detected by default. Can be * Name of the target database to operate on, auto-detected by default. Can be
* alternatively set using the "Database" enum. * alternatively set using the "Database" enum.
*/ */
private String databasePlatform; private @Nullable String databasePlatform;
/** /**
* Target database to operate on, auto-detected by default. Can be alternatively set * Target database to operate on, auto-detected by default. Can be alternatively set
* using the "databasePlatform" property. * using the "databasePlatform" property.
*/ */
private Database database; private @Nullable Database database;
/** /**
* Whether to initialize the schema on startup. * Whether to initialize the schema on startup.
@ -73,7 +75,7 @@ public class JpaProperties {
* Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the * Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the
* thread for the entire processing of the request. * thread for the entire processing of the request.
*/ */
private Boolean openInView; private @Nullable Boolean openInView;
public Map<String, String> getProperties() { public Map<String, String> getProperties() {
return this.properties; return this.properties;
@ -87,19 +89,19 @@ public class JpaProperties {
return this.mappingResources; return this.mappingResources;
} }
public String getDatabasePlatform() { public @Nullable String getDatabasePlatform() {
return this.databasePlatform; return this.databasePlatform;
} }
public void setDatabasePlatform(String databasePlatform) { public void setDatabasePlatform(@Nullable String databasePlatform) {
this.databasePlatform = databasePlatform; this.databasePlatform = databasePlatform;
} }
public Database getDatabase() { public @Nullable Database getDatabase() {
return this.database; return this.database;
} }
public void setDatabase(Database database) { public void setDatabase(@Nullable Database database) {
this.database = database; this.database = database;
} }
@ -119,11 +121,11 @@ public class JpaProperties {
this.showSql = showSql; this.showSql = showSql;
} }
public Boolean getOpenInView() { public @Nullable Boolean getOpenInView() {
return this.openInView; return this.openInView;
} }
public void setOpenInView(Boolean openInView) { public void setOpenInView(@Nullable Boolean openInView) {
this.openInView = openInView; this.openInView = openInView;
} }

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

@ -17,4 +17,7 @@
/** /**
* Base Auto-configuration for JPA and Spring ORM. * Base Auto-configuration for JPA and Spring ORM.
*/ */
@NullMarked
package org.springframework.boot.jpa.autoconfigure; package org.springframework.boot.jpa.autoconfigure;
import org.jspecify.annotations.NullMarked;

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

@ -17,4 +17,7 @@
/** /**
* JPA Support classes. * JPA Support classes.
*/ */
@NullMarked
package org.springframework.boot.jpa; package org.springframework.boot.jpa;
import org.jspecify.annotations.NullMarked;

Loading…
Cancel
Save