Skip to content

Commit a61b297

Browse files
committed
Specify flexible generics nullness in spring-jdbc
This commit leverages flexible generics nullness at method and type level when relevant in spring-jdbc. Due to uber/NullAway#1075, some related `@SuppressWarnings("NullAway")` have been added. JdbcOperations Kotlin extensions have been refined accordingly. Closes gh-34911
1 parent 2b56b75 commit a61b297

33 files changed

+313
-220
lines changed

spring-core/src/main/java/org/springframework/util/CollectionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
110110
* @since 5.3
111111
* @see #newHashMap(int)
112112
*/
113-
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
113+
public static <K, V extends @Nullable Object> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
114114
return new LinkedHashMap<>(computeInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR);
115115
}
116116

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @see JdbcTemplate#execute(CallableStatementCreator, CallableStatementCallback)
4545
*/
4646
@FunctionalInterface
47-
public interface CallableStatementCallback<T> {
47+
public interface CallableStatementCallback<T extends @Nullable Object> {
4848

4949
/**
5050
* Gets called by {@code JdbcTemplate.execute} with an active JDBC
@@ -75,6 +75,6 @@ public interface CallableStatementCallback<T> {
7575
* into a DataAccessException by an SQLExceptionTranslator
7676
* @throws DataAccessException in case of custom exceptions
7777
*/
78-
@Nullable T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException;
78+
T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException;
7979

8080
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
* @see JdbcTemplate#queryForList(String)
4545
* @see JdbcTemplate#queryForMap(String)
4646
*/
47-
public class ColumnMapRowMapper implements RowMapper<Map<String, Object>> {
47+
public class ColumnMapRowMapper implements RowMapper<Map<String, @Nullable Object>> {
4848

4949
@Override
50-
public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
50+
public Map<String, @Nullable Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
5151
ResultSetMetaData rsmd = rs.getMetaData();
5252
int columnCount = rsmd.getColumnCount();
53-
Map<String, Object> mapOfColumnValues = createColumnMap(columnCount);
53+
Map<String, @Nullable Object> mapOfColumnValues = createColumnMap(columnCount);
5454
for (int i = 1; i <= columnCount; i++) {
5555
String column = JdbcUtils.lookupColumnName(rsmd, i);
5656
mapOfColumnValues.putIfAbsent(getColumnKey(column), getColumnValue(rs, i));
@@ -66,7 +66,7 @@ public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException
6666
* @return the new Map instance
6767
* @see org.springframework.util.LinkedCaseInsensitiveMap
6868
*/
69-
protected Map<String, Object> createColumnMap(int columnCount) {
69+
protected Map<String, @Nullable Object> createColumnMap(int columnCount) {
7070
return new LinkedCaseInsensitiveMap<>(columnCount);
7171
}
7272

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* @see JdbcTemplate#update
4242
*/
4343
@FunctionalInterface
44-
public interface ConnectionCallback<T> {
44+
public interface ConnectionCallback<T extends @Nullable Object> {
4545

4646
/**
4747
* Gets called by {@code JdbcTemplate.execute} with an active JDBC
@@ -65,6 +65,6 @@ public interface ConnectionCallback<T> {
6565
* @see JdbcTemplate#queryForObject(String, Class)
6666
* @see JdbcTemplate#queryForRowSet(String)
6767
*/
68-
@Nullable T doInConnection(Connection con) throws SQLException, DataAccessException;
68+
T doInConnection(Connection con) throws SQLException, DataAccessException;
6969

7070
}

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

Lines changed: 37 additions & 37 deletions
Large diffs are not rendered by default.

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

Lines changed: 64 additions & 52 deletions
Large diffs are not rendered by default.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.sql.PreparedStatement;
2020
import java.sql.SQLException;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
/**
2325
* Parameterized callback interface used by the {@link JdbcTemplate} class for
2426
* batch updates.
@@ -47,6 +49,6 @@ public interface ParameterizedPreparedStatementSetter<T> {
4749
* @param argument the object containing the values to be set
4850
* @throws SQLException if an SQLException is encountered (i.e. there is no need to catch SQLException)
4951
*/
50-
void setValues(PreparedStatement ps, T argument) throws SQLException;
52+
void setValues(PreparedStatement ps, @Nullable T argument) throws SQLException;
5153

5254
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @see JdbcTemplate#execute(PreparedStatementCreator, PreparedStatementCallback)
4545
*/
4646
@FunctionalInterface
47-
public interface PreparedStatementCallback<T> {
47+
public interface PreparedStatementCallback<T extends @Nullable Object> {
4848

4949
/**
5050
* Gets called by {@code JdbcTemplate.execute} with an active JDBC
@@ -75,6 +75,6 @@ public interface PreparedStatementCallback<T> {
7575
* @see JdbcTemplate#queryForObject(String, Class, Object...)
7676
* @see JdbcTemplate#queryForList(String, Object...)
7777
*/
78-
@Nullable T doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException;
78+
T doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException;
7979

8080
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* @see org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor
5151
*/
5252
@FunctionalInterface
53-
public interface ResultSetExtractor<T> {
53+
public interface ResultSetExtractor<T extends @Nullable Object> {
5454

5555
/**
5656
* Implementations must implement this method to process the entire ResultSet.
@@ -62,6 +62,6 @@ public interface ResultSetExtractor<T> {
6262
* values or navigating (that is, there's no need to catch SQLException)
6363
* @throws DataAccessException in case of custom exceptions
6464
*/
65-
@Nullable T extractData(ResultSet rs) throws SQLException, DataAccessException;
65+
T extractData(ResultSet rs) throws SQLException, DataAccessException;
6666

6767
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @see org.springframework.jdbc.object.MappingSqlQuery
5050
*/
5151
@FunctionalInterface
52-
public interface RowMapper<T> {
52+
public interface RowMapper<T extends @Nullable Object> {
5353

5454
/**
5555
* Implementations must implement this method to map each row of data in the
@@ -61,6 +61,6 @@ public interface RowMapper<T> {
6161
* @throws SQLException if an SQLException is encountered while getting
6262
* column values (that is, there's no need to catch SQLException)
6363
*/
64-
@Nullable T mapRow(ResultSet rs, int rowNum) throws SQLException;
64+
T mapRow(ResultSet rs, int rowNum) throws SQLException;
6565

6666
}

0 commit comments

Comments
 (0)