Skip to content

Commit fbc7590

Browse files
committed
Refine null-safety in the spring-jdbc module
Closes gh-34147
1 parent 0c6f5d7 commit fbc7590

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,10 +45,8 @@ public class ArgumentTypePreparedStatementSetter implements PreparedStatementSet
4545
* @param args the arguments to set
4646
* @param argTypes the corresponding SQL types of the arguments
4747
*/
48-
@SuppressWarnings("NullAway")
4948
public ArgumentTypePreparedStatementSetter(@Nullable Object @Nullable [] args, int @Nullable [] argTypes) {
50-
if ((args != null && argTypes == null) || (args == null && argTypes != null) ||
51-
(args != null && args.length != argTypes.length)) {
49+
if ((args == null && argTypes != null) || (args != null && (argTypes == null || args.length != argTypes.length))) {
5250
throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match");
5351
}
5452
this.args = args;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void setAutoCommit(boolean autoCommit) {
179179

180180

181181
@Override
182-
@SuppressWarnings("NullAway")
182+
@SuppressWarnings("NullAway") // Lazy initialization
183183
public Connection getConnection() throws SQLException {
184184
this.connectionLock.lock();
185185
try {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void setDatabasePopulator(DatabasePopulator populator) {
159159
* Factory method that returns the {@linkplain EmbeddedDatabase embedded database}
160160
* instance, which is also a {@link DataSource}.
161161
*/
162-
@SuppressWarnings("NullAway")
162+
@SuppressWarnings("NullAway") // Lazy initialization
163163
public EmbeddedDatabase getDatabase() {
164164
if (this.dataSource == null) {
165165
initDatabase();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,7 +76,7 @@ public void setDataSource(DataSource dataSource) {
7676
/**
7777
* Return the data source to retrieve the value from.
7878
*/
79-
@SuppressWarnings("NullAway")
79+
@SuppressWarnings("NullAway") // Lazy initialization
8080
public DataSource getDataSource() {
8181
return this.dataSource;
8282
}
@@ -91,7 +91,7 @@ public void setIncrementerName(String incrementerName) {
9191
/**
9292
* Return the name of the sequence/table.
9393
*/
94-
@SuppressWarnings("NullAway")
94+
@SuppressWarnings("NullAway") // Lazy initialization
9595
public String getIncrementerName() {
9696
return this.incrementerName;
9797
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,10 +23,13 @@
2323

2424
import javax.sql.DataSource;
2525

26+
import org.jspecify.annotations.Nullable;
27+
2628
import org.springframework.dao.DataAccessException;
2729
import org.springframework.dao.DataAccessResourceFailureException;
2830
import org.springframework.jdbc.datasource.DataSourceUtils;
2931
import org.springframework.jdbc.support.JdbcUtils;
32+
import org.springframework.util.Assert;
3033

3134
/**
3235
* Abstract base class for {@link DataFieldMaxValueIncrementer} implementations
@@ -41,7 +44,7 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract
4144
private boolean deleteSpecificValues = false;
4245

4346
/** The current cache of values. */
44-
private long[] valueCache;
47+
private long @Nullable [] valueCache;
4548

4649
/** The next id to serve from the value cache. */
4750
private int nextValueIndex = -1;
@@ -53,11 +56,9 @@ public abstract class AbstractIdentityColumnMaxValueIncrementer extends Abstract
5356
* @see #setIncrementerName
5457
* @see #setColumnName
5558
*/
56-
@SuppressWarnings("NullAway")
5759
public AbstractIdentityColumnMaxValueIncrementer() {
5860
}
5961

60-
@SuppressWarnings("NullAway")
6162
public AbstractIdentityColumnMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) {
6263
super(dataSource, incrementerName, columnName);
6364
}
@@ -120,6 +121,7 @@ protected synchronized long getNextKey() throws DataAccessException {
120121
DataSourceUtils.releaseConnection(con, getDataSource());
121122
}
122123
}
124+
Assert.state(this.valueCache != null, "The cache of values can't be null");
123125
return this.valueCache[this.nextValueIndex++];
124126
}
125127

0 commit comments

Comments
 (0)