Browse Source

Refine null-safety in the spring-jdbc module

Closes gh-34147
pull/34171/head
Sébastien Deleuze 1 year ago
parent
commit
fbc759077c
  1. 6
      spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentTypePreparedStatementSetter.java
  2. 2
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/SingleConnectionDataSource.java
  3. 2
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java
  4. 6
      spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/AbstractDataFieldMaxValueIncrementer.java
  5. 10
      spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/AbstractIdentityColumnMaxValueIncrementer.java

6
spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentTypePreparedStatementSetter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -45,10 +45,8 @@ public class ArgumentTypePreparedStatementSetter implements PreparedStatementSet @@ -45,10 +45,8 @@ public class ArgumentTypePreparedStatementSetter implements PreparedStatementSet
* @param args the arguments to set
* @param argTypes the corresponding SQL types of the arguments
*/
@SuppressWarnings("NullAway")
public ArgumentTypePreparedStatementSetter(@Nullable Object @Nullable [] args, int @Nullable [] argTypes) {
if ((args != null && argTypes == null) || (args == null && argTypes != null) ||
(args != null && args.length != argTypes.length)) {
if ((args == null && argTypes != null) || (args != null && (argTypes == null || args.length != argTypes.length))) {
throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match");
}
this.args = args;

2
spring-jdbc/src/main/java/org/springframework/jdbc/datasource/SingleConnectionDataSource.java

@ -179,7 +179,7 @@ public class SingleConnectionDataSource extends DriverManagerDataSource @@ -179,7 +179,7 @@ public class SingleConnectionDataSource extends DriverManagerDataSource
@Override
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Lazy initialization
public Connection getConnection() throws SQLException {
this.connectionLock.lock();
try {

2
spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java

@ -159,7 +159,7 @@ public class EmbeddedDatabaseFactory { @@ -159,7 +159,7 @@ public class EmbeddedDatabaseFactory {
* Factory method that returns the {@linkplain EmbeddedDatabase embedded database}
* instance, which is also a {@link DataSource}.
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Lazy initialization
public EmbeddedDatabase getDatabase() {
if (this.dataSource == null) {
initDatabase();

6
spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/AbstractDataFieldMaxValueIncrementer.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -76,7 +76,7 @@ public abstract class AbstractDataFieldMaxValueIncrementer implements DataFieldM @@ -76,7 +76,7 @@ public abstract class AbstractDataFieldMaxValueIncrementer implements DataFieldM
/**
* Return the data source to retrieve the value from.
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Lazy initialization
public DataSource getDataSource() {
return this.dataSource;
}
@ -91,7 +91,7 @@ public abstract class AbstractDataFieldMaxValueIncrementer implements DataFieldM @@ -91,7 +91,7 @@ public abstract class AbstractDataFieldMaxValueIncrementer implements DataFieldM
/**
* Return the name of the sequence/table.
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Lazy initialization
public String getIncrementerName() {
return this.incrementerName;
}

10
spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/AbstractIdentityColumnMaxValueIncrementer.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,10 +23,13 @@ import java.sql.Statement; @@ -23,10 +23,13 @@ import java.sql.Statement;
import javax.sql.DataSource;
import org.jspecify.annotations.Nullable;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.util.Assert;
/**
* Abstract base class for {@link DataFieldMaxValueIncrementer} implementations
@ -41,7 +44,7 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract @@ -41,7 +44,7 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract
private boolean deleteSpecificValues = false;
/** The current cache of values. */
private long[] valueCache;
private long @Nullable [] valueCache;
/** The next id to serve from the value cache. */
private int nextValueIndex = -1;
@ -53,11 +56,9 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract @@ -53,11 +56,9 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract
* @see #setIncrementerName
* @see #setColumnName
*/
@SuppressWarnings("NullAway")
public AbstractIdentityColumnMaxValueIncrementer() {
}
@SuppressWarnings("NullAway")
public AbstractIdentityColumnMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) {
super(dataSource, incrementerName, columnName);
}
@ -120,6 +121,7 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract @@ -120,6 +121,7 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
Assert.state(this.valueCache != null, "The cache of values can't be null");
return this.valueCache[this.nextValueIndex++];
}

Loading…
Cancel
Save