Skip to content

Commit 723b2b1

Browse files
committed
Consistent checks in DataAccessUtils (plus nullability hints in javadoc)
Issue: SPR-16225
1 parent f09e252 commit 723b2b1

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public interface JdbcOperations {
137137
* {@code null} as argument array.
138138
* @param sql SQL query to execute
139139
* @param rowMapper object that will map one object per row
140-
* @return the single mapped object
140+
* @return the single mapped object (may be {@code null} if the given
141+
* {@link RowMapper} returned {@code} null)
141142
* @throws IncorrectResultSizeDataAccessException if the query does not
142143
* return exactly one row
143144
* @throws DataAccessException if there is any problem executing the query
@@ -359,6 +360,7 @@ public interface JdbcOperations {
359360
* only the argument value but also the SQL type and optionally the scale
360361
* @return an arbitrary result object, as returned by the ResultSetExtractor
361362
* @throws DataAccessException if the query fails
363+
* @since 3.0.1
362364
*/
363365
<T> T query(String sql, ResultSetExtractor<T> rse, Object... args) throws DataAccessException;
364366

@@ -428,6 +430,7 @@ public interface JdbcOperations {
428430
* may also contain {@link SqlParameterValue} objects which indicate not
429431
* only the argument value but also the SQL type and optionally the scale
430432
* @throws DataAccessException if the query fails
433+
* @since 3.0.1
431434
*/
432435
void query(String sql, RowCallbackHandler rch, Object... args) throws DataAccessException;
433436

@@ -501,6 +504,7 @@ public interface JdbcOperations {
501504
* only the argument value but also the SQL type and optionally the scale
502505
* @return the result List, containing mapped objects
503506
* @throws DataAccessException if the query fails
507+
* @since 3.0.1
504508
*/
505509
<T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException;
506510

@@ -514,7 +518,8 @@ public interface JdbcOperations {
514518
* @param argTypes SQL types of the arguments
515519
* (constants from {@code java.sql.Types})
516520
* @param rowMapper object that will map one object per row
517-
* @return the single mapped object
521+
* @return the single mapped object (may be {@code null} if the given
522+
* {@link RowMapper} returned {@code} null)
518523
* @throws IncorrectResultSizeDataAccessException if the query does not
519524
* return exactly one row
520525
* @throws DataAccessException if the query fails
@@ -532,7 +537,8 @@ <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> row
532537
* may also contain {@link SqlParameterValue} objects which indicate not
533538
* only the argument value but also the SQL type and optionally the scale
534539
* @param rowMapper object that will map one object per row
535-
* @return the single mapped object
540+
* @return the single mapped object (may be {@code null} if the given
541+
* {@link RowMapper} returned {@code} null)
536542
* @throws IncorrectResultSizeDataAccessException if the query does not
537543
* return exactly one row
538544
* @throws DataAccessException if the query fails
@@ -549,10 +555,12 @@ <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> row
549555
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
550556
* may also contain {@link SqlParameterValue} objects which indicate not
551557
* only the argument value but also the SQL type and optionally the scale
552-
* @return the single mapped object
558+
* @return the single mapped object (may be {@code null} if the given
559+
* {@link RowMapper} returned {@code} null)
553560
* @throws IncorrectResultSizeDataAccessException if the query does not
554561
* return exactly one row
555562
* @throws DataAccessException if the query fails
563+
* @since 3.0.1
556564
*/
557565
<T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException;
558566

@@ -610,6 +618,7 @@ <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> require
610618
* @throws IncorrectResultSizeDataAccessException if the query does not return
611619
* exactly one row, or does not return exactly one column in that row
612620
* @throws DataAccessException if the query fails
621+
* @since 3.0.1
613622
* @see #queryForObject(String, Class)
614623
*/
615624
<T> T queryForObject(String sql, Class<T> requiredType, Object... args) throws DataAccessException;
@@ -709,6 +718,7 @@ <T>List<T> queryForList(String sql, Object[] args, int[] argTypes, Class<T> elem
709718
* only the argument value but also the SQL type and optionally the scale
710719
* @return a List of objects that match the specified element type
711720
* @throws DataAccessException if the query fails
721+
* @since 3.0.1
712722
* @see #queryForList(String, Class)
713723
* @see SingleColumnRowMapper
714724
*/

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
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -53,7 +53,7 @@ public interface RowMapper<T> {
5353
* the ResultSet; it is only supposed to map values of the current row.
5454
* @param rs the ResultSet to map (pre-initialized for the current row)
5555
* @param rowNum the number of the current row
56-
* @return the result object for the current row
56+
* @return the result object for the current row (may be {@code null})
5757
* @throws SQLException if a SQLException is encountered getting
5858
* column values (that is, there's no need to catch SQLException)
5959
*/

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcOperations.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -226,7 +226,8 @@ <T> List<T> query(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper)
226226
* @param sql SQL query to execute
227227
* @param paramSource container of arguments to bind to the query
228228
* @param rowMapper object that will map one object per row
229-
* @return the single mapped object
229+
* @return the single mapped object (may be {@code null} if the given
230+
* {@link RowMapper} returned {@code} null)
230231
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException
231232
* if the query does not return exactly one row, or does not return exactly
232233
* one column in that row
@@ -243,7 +244,8 @@ <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> ro
243244
* @param paramMap map of parameters to bind to the query
244245
* (leaving it to the PreparedStatement to guess the corresponding SQL type)
245246
* @param rowMapper object that will map one object per row
246-
* @return the single mapped object
247+
* @return the single mapped object (may be {@code null} if the given
248+
* {@link RowMapper} returned {@code} null)
247249
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException
248250
* if the query does not return exactly one row, or does not return exactly
249251
* one column in that row
@@ -396,7 +398,7 @@ <T> List<T> queryForList(String sql, Map<String, ?> paramMap, Class<T> elementTy
396398
* list of arguments to bind to the query, expecting a SqlRowSet.
397399
* <p>The results will be mapped to an SqlRowSet which holds the data in a
398400
* disconnected fashion. This wrapper will translate any SQLExceptions thrown.
399-
* <p>Note that that, for the default implementation, JDBC RowSet support needs to
401+
* <p>Note that, for the default implementation, JDBC RowSet support needs to
400402
* be available at runtime: by default, Sun's {@code com.sun.rowset.CachedRowSetImpl}
401403
* class is used, which is part of JDK 1.5+ and also available separately as part of
402404
* Sun's JDBC RowSet Implementations download (rowset.jar).
@@ -416,7 +418,7 @@ <T> List<T> queryForList(String sql, Map<String, ?> paramMap, Class<T> elementTy
416418
* list of arguments to bind to the query, expecting a SqlRowSet.
417419
* <p>The results will be mapped to an SqlRowSet which holds the data in a
418420
* disconnected fashion. This wrapper will translate any SQLExceptions thrown.
419-
* <p>Note that that, for the default implementation, JDBC RowSet support needs to
421+
* <p>Note that, for the default implementation, JDBC RowSet support needs to
420422
* be available at runtime: by default, Sun's {@code com.sun.rowset.CachedRowSetImpl}
421423
* class is used, which is part of JDK 1.5+ and also available separately as part of
422424
* Sun's JDBC RowSet Implementations download (rowset.jar).

spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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,12 +45,11 @@ public abstract class DataAccessUtils {
4545
* element has been found in the given Collection
4646
*/
4747
public static <T> T singleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
48-
int size = (results != null ? results.size() : 0);
49-
if (size == 0) {
48+
if (CollectionUtils.isEmpty(results)) {
5049
return null;
5150
}
5251
if (results.size() > 1) {
53-
throw new IncorrectResultSizeDataAccessException(1, size);
52+
throw new IncorrectResultSizeDataAccessException(1, results.size());
5453
}
5554
return results.iterator().next();
5655
}
@@ -66,12 +65,11 @@ public static <T> T singleResult(Collection<T> results) throws IncorrectResultSi
6665
* has been found in the given Collection
6766
*/
6867
public static <T> T requiredSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
69-
int size = (results != null ? results.size() : 0);
70-
if (size == 0) {
68+
if (CollectionUtils.isEmpty(results)) {
7169
throw new EmptyResultDataAccessException(1);
7270
}
7371
if (results.size() > 1) {
74-
throw new IncorrectResultSizeDataAccessException(1, size);
72+
throw new IncorrectResultSizeDataAccessException(1, results.size());
7573
}
7674
return results.iterator().next();
7775
}
@@ -87,12 +85,11 @@ public static <T> T requiredSingleResult(Collection<T> results) throws Incorrect
8785
* @see org.springframework.util.CollectionUtils#hasUniqueObject
8886
*/
8987
public static <T> T uniqueResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
90-
int size = (results != null ? results.size() : 0);
91-
if (size == 0) {
88+
if (CollectionUtils.isEmpty(results)) {
9289
return null;
9390
}
9491
if (!CollectionUtils.hasUniqueObject(results)) {
95-
throw new IncorrectResultSizeDataAccessException(1, size);
92+
throw new IncorrectResultSizeDataAccessException(1, results.size());
9693
}
9794
return results.iterator().next();
9895
}
@@ -109,12 +106,11 @@ public static <T> T uniqueResult(Collection<T> results) throws IncorrectResultSi
109106
* @see org.springframework.util.CollectionUtils#hasUniqueObject
110107
*/
111108
public static <T> T requiredUniqueResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
112-
int size = (results != null ? results.size() : 0);
113-
if (size == 0) {
109+
if (CollectionUtils.isEmpty(results)) {
114110
throw new EmptyResultDataAccessException(1);
115111
}
116112
if (!CollectionUtils.hasUniqueObject(results)) {
117-
throw new IncorrectResultSizeDataAccessException(1, size);
113+
throw new IncorrectResultSizeDataAccessException(1, results.size());
118114
}
119115
return results.iterator().next();
120116
}
@@ -200,11 +196,11 @@ public static long longResult(Collection<?> results)
200196

201197
/**
202198
* Return a translated exception if this is appropriate,
203-
* otherwise return the input exception.
204-
* @param rawException exception we may wish to translate
199+
* otherwise return the given exception as-is.
200+
* @param rawException an exception that we may wish to translate
205201
* @param pet PersistenceExceptionTranslator to use to perform the translation
206-
* @return a translated exception if translation is possible, or
207-
* the raw exception if it is not
202+
* @return a translated persistence exception if translation is possible,
203+
* or the raw exception if it is not
208204
*/
209205
public static RuntimeException translateIfNecessary(
210206
RuntimeException rawException, PersistenceExceptionTranslator pet) {

0 commit comments

Comments
 (0)