Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc7affb

Browse files
committed
Polishing.
Refine nullability annotation formatting, reduce warnings (final fields, isEmpty vs. length() == null), introduce getRequired...() methods to avoid Assert sprawl. Convert internal classes to records. Fix generic types (refactoring leftovers). Replace package-info imports with fully-qualified annotation name to align formatting. See #1980 Original pull request: #2126
1 parent c15e3b5 commit dc7affb

File tree

113 files changed

+394
-515
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+394
-515
lines changed

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/AggregateReader.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public String keyColumn(AggregatePath path) {
9494
* @return the found aggregate root, or {@literal null} if not found.
9595
* @param <T> aggregator type.
9696
*/
97-
@Nullable
98-
public <T> T findById(Object id, RelationalPersistentEntity<T> entity) {
97+
public <T> @Nullable T findById(Object id, RelationalPersistentEntity<T> entity) {
9998

10099
Query query = Query.query(Criteria.where(entity.getRequiredIdProperty().getName()).is(id)).limit(1);
101100

@@ -111,8 +110,7 @@ public <T> T findById(Object id, RelationalPersistentEntity<T> entity) {
111110
* @param <T> aggregator type.
112111
*/
113112
@SuppressWarnings("NullAway") // NullAway complains about the ResultSetExtractor returning null, which should be ok.
114-
@Nullable
115-
public <T> T findOne(Query query, RelationalPersistentEntity<T> entity) {
113+
public <T> @Nullable T findOne(Query query, RelationalPersistentEntity<T> entity) {
116114
return doFind(query, entity, rs -> extractZeroOrOne(rs, entity));
117115
}
118116

@@ -159,7 +157,8 @@ public <T> List<T> findAll(Query query, RelationalPersistentEntity<T> entity) {
159157
}
160158

161159
@SuppressWarnings("ConstantConditions")
162-
private <T, R> R doFind(Query query, RelationalPersistentEntity<T> entity, ResultSetExtractor<R> extractor) {
160+
private <T, R extends @Nullable Object> R doFind(Query query, RelationalPersistentEntity<T> entity,
161+
ResultSetExtractor<R> extractor) {
163162

164163
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
165164
Condition condition = createCondition(query, parameterSource, entity);

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BatchInsertStrategy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
1921

2022
/**
@@ -32,5 +34,6 @@ interface BatchInsertStrategy {
3234
* elements will be {@code null}.
3335
* @since 2.4
3436
*/
37+
@Nullable
3538
Object[] execute(String sql, SqlParameterSource[] sqlParameterSources);
3639
}

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/CascadingDataAccessStrategy.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.function.Function;
2525
import java.util.stream.Stream;
2626

27+
import org.jspecify.annotations.Nullable;
28+
2729
import org.springframework.data.domain.Pageable;
2830
import org.springframework.data.domain.Sort;
2931
import org.springframework.data.mapping.PersistentPropertyPath;
@@ -74,12 +76,14 @@ public NamedParameterJdbcOperations getJdbcOperations() {
7476
}
7577

7678
@Override
77-
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource) {
79+
public <T> @Nullable Object insert(T instance, Class<T> domainType, Identifier identifier,
80+
IdValueSource idValueSource) {
7881
return collect(das -> das.insert(instance, domainType, identifier, idValueSource));
7982
}
8083

8184
@Override
82-
public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) {
85+
public <T> @Nullable Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType,
86+
IdValueSource idValueSource) {
8387
return collect(das -> das.insert(insertSubjects, domainType, idValueSource));
8488
}
8589

@@ -144,7 +148,7 @@ public long count(Class<?> domainType) {
144148
}
145149

146150
@Override
147-
public <T> T findById(Object id, Class<T> domainType) {
151+
public <Textends@NullableObject> T findById(Object id, Class<T> domainType) {
148152
return collect(das -> das.findById(id, domainType));
149153
}
150154

@@ -224,8 +228,7 @@ public <T> long count(Query query, Class<T> domainType) {
224228
return collect(das -> das.count(query, domainType));
225229
}
226230

227-
private <T> T collect(Function<DataAccessStrategy, T> function) {
228-
231+
private <T extends @Nullable Object> T collect(Function<DataAccessStrategy, T> function) {
229232
return strategies.stream().collect(new FunctionCollector<>(function));
230233
}
231234

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DataAccessStrategy.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public interface DataAccessStrategy extends ReadingDataAccessStrategy, RelationR
7373
* @return the id generated by the database if any.
7474
* @since 2.4
7575
*/
76-
@Nullable
77-
<T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource);
76+
<T> @Nullable Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource);
7877

7978
/**
8079
* Inserts the data of multiple entities.
@@ -88,7 +87,8 @@ public interface DataAccessStrategy extends ReadingDataAccessStrategy, RelationR
8887
* elements will be {@code null}.
8988
* @since 2.4
9089
*/
91-
<T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource);
90+
<T> @Nullable Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType,
91+
IdValueSource idValueSource);
9292

9393
/**
9494
* Updates the data of a single entity in the database. Referenced entities don't get handled.
@@ -255,8 +255,7 @@ public interface DataAccessStrategy extends ReadingDataAccessStrategy, RelationR
255255
* @return Might return {@code null}.
256256
*/
257257
@Override
258-
@Nullable
259-
<T> T findById(Object id, Class<T> domainType);
258+
<T extends @Nullable Object> T findById(Object id, Class<T> domainType);
260259

261260
/**
262261
* Loads all entities of the given type.

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public NamedParameterJdbcOperations getJdbcOperations() {
130130
}
131131

132132
@Override
133-
public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) {
133+
public <T> @Nullable Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType,
134+
IdValueSource idValueSource) {
134135

135136
Assert.notEmpty(insertSubjects, "Batch insert must contain at least one InsertSubject");
136137

@@ -282,7 +283,8 @@ public long count(Class<?> domainType) {
282283
}
283284

284285
@Override
285-
public <T> @Nullable T findById(Object id, Class<T> domainType) {
286+
@SuppressWarnings("NullAway")
287+
public <T extends @Nullable Object> T findById(Object id, Class<T> domainType) {
286288

287289
String findOneSql = sql(domainType).getFindOne();
288290
SqlIdentifierParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType);
@@ -331,7 +333,6 @@ public <T> Stream<T> streamAllByIds(Iterable<?> ids, Class<T> domainType) {
331333
}
332334

333335
@Override
334-
@SuppressWarnings("unchecked")
335336
public List<Object> findAllByPath(Identifier identifier,
336337
PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath) {
337338

@@ -363,7 +364,7 @@ public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
363364
if (!path.hasIdProperty() && path.isQualified()) {
364365

365366
TableInfo tableInfo = path.getTableInfo();
366-
identifierToUse = identifierToUse.withPart(tableInfo.requiredQualifierColumnInfo().name(), rowNum,
367+
identifierToUse = identifierToUse.withPart(tableInfo.getRequiredQualifierColumnInfo().name(), rowNum,
367368
Object.class);
368369
}
369370

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DelegatingDataAccessStrategy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public NamedParameterJdbcOperations getJdbcOperations() {
7575
}
7676

7777
@Override
78-
public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) {
78+
public <T> @Nullable Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType,
79+
IdValueSource idValueSource) {
7980
return delegate.insert(insertSubjects, domainType, idValueSource);
8081
}
8182

@@ -141,7 +142,7 @@ public long count(Class<?> domainType) {
141142
}
142143

143144
@Override
144-
public <T> @Nullable T findById(Object id, Class<T> domainType) {
145+
public <Textends@NullableObject> T findById(Object id, Class<T> domainType) {
145146

146147
Assert.notNull(delegate, "Delegate is null");
147148

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/InsertStrategyFactory.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,22 @@ BatchInsertStrategy batchInsertStrategy(IdValueSource idValueSource, @Nullable S
7373
private record DefaultInsertStrategy(NamedParameterJdbcOperations jdbcOperations) implements InsertStrategy {
7474

7575
@Override
76-
public @Nullable Object execute(String sql, SqlParameterSource sqlParameterSource) {
76+
public @Nullable Object execute(String sql, SqlParameterSource sqlParameterSource) {
7777

78-
jdbcOperations.update(sql, sqlParameterSource);
79-
return null;
80-
}
78+
jdbcOperations.update(sql, sqlParameterSource);
79+
return null;
8180
}
81+
}
8282

8383
private record DefaultBatchInsertStrategy(
8484
NamedParameterJdbcOperations jdbcOperations) implements BatchInsertStrategy {
8585

8686
@Override
87-
public Object[] execute(String sql, SqlParameterSource[] sqlParameterSources) {
87+
public@Nullable Object[] execute(String sql, SqlParameterSource[] sqlParameterSources) {
8888

89-
jdbcOperations.batchUpdate(sql, sqlParameterSources);
90-
return new Object[sqlParameterSources.length];
91-
}
89+
jdbcOperations.batchUpdate(sql, sqlParameterSources);
90+
return new Object[sqlParameterSources.length];
9291
}
92+
}
9393

9494
}

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcIdentifierBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public JdbcIdentifierBuilder withQualifier(AggregatePath path, Object value) {
9393
Assert.notNull(value, "Value must not be null");
9494

9595
AggregatePath.TableInfo tableInfo = path.getTableInfo();
96-
identifier = identifier.withPart(tableInfo.requiredQualifierColumnInfo().name(), value,
97-
tableInfo.requiredQualifierColumnType());
96+
identifier = identifier.withPart(tableInfo.getRequiredQualifierColumnInfo().name(), value,
97+
tableInfo.getRequiredQualifierColumnType());
9898

9999
return this;
100100
}

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ protected RelationalPropertyValueProvider newValueProvider(RowDocumentAccessor d
374374
* @param ex the offending {@code SQLException}
375375
* @return a DataAccessException wrapping the {@code SQLException} (never {@code null})
376376
*/
377-
private DataAccessException translateException(String task, @org.jspecify.annotations.Nullable String sql,
377+
private DataAccessException translateException(String task, @Nullable String sql,
378378
SQLException ex) {
379379
DataAccessException dae = exceptionTranslator.translate(task, sql, ex);
380380
return (dae != null ? dae : new UncategorizedSQLException(task, sql, ex));
@@ -556,10 +556,7 @@ private static AggregatePath smartAppend(AggregatePath base, AggregatePath suffi
556556
if (owner.equals(base.getRequiredLeafEntity())) {
557557
return base.append(suffix);
558558
} else {
559-
AggregatePath tail = suffix.getTail();
560-
561-
Assert.state(tail != null, "Tail must not be null");
562-
559+
AggregatePath tail = suffix.getRequiredTail();
563560
return smartAppend(base, tail);
564561
}
565562
}

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/QueryMapper.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ Expression getMappedObject(Expression expression, @Nullable RelationalPersistent
126126
if (expression instanceof Column column) {
127127

128128
Field field = createPropertyField(entity, column.getName());
129-
TableLike table = column.getTable();
130-
131-
Assert.state(table != null, String.format("The column %s must have a table set", column));
129+
TableLike table = column.getRequiredTable();
132130

133131
Column columnFromTable = table.column(field.getMappedColumnName());
134132
return column instanceof Aliased aliased ? columnFromTable.as(aliased.getAlias()) : columnFromTable;
@@ -184,10 +182,7 @@ private Condition unroll(CriteriaDefinition criteria, Table table, @Nullable Rel
184182

185183
while (current.hasPrevious()) {
186184

187-
CriteriaDefinition previous = current.getPrevious();
188-
189-
Assert.state(previous != null, "Previous must not be null");
190-
185+
CriteriaDefinition previous = current.getRequiredPrevious();
191186
forwardChain.put(previous, current);
192187
current = previous;
193188
}
@@ -332,7 +327,7 @@ private Condition mapCondition(CriteriaDefinition criteria, MapSqlParameterSourc
332327
* @param property the property to which the value relates. It determines the type to convert to. Must not be
333328
* {@literal null}.
334329
* @param value the value to be converted.
335-
* @return a nonnull {@link JdbcValue} holding the converted value and the appropriate JDBC type information.
330+
* @return a non-null {@link JdbcValue} holding the converted value and the appropriate JDBC type information.
336331
*/
337332
private JdbcValue convertToJdbcValue(RelationalPersistentProperty property, @Nullable Object value) {
338333

@@ -348,15 +343,14 @@ private JdbcValue convertToJdbcValue(RelationalPersistentProperty property, @Nul
348343
Object secondValue = second.getValue();
349344

350345
Assert.state(firstValue != null, "First value must not be null");
351-
352346
Assert.state(secondValue != null, "Second value must not be null");
353347

354348
return JdbcValue.of(Pair.of(firstValue, secondValue), first.getJdbcType());
355349
}
356350

357351
if (value instanceof Iterable) {
358352

359-
List<Object> mapped = new ArrayList<>();
353+
List<@NullableObject> mapped = new ArrayList<>();
360354
SQLType jdbcType = null;
361355

362356
for (Object o : (Iterable<?>) value) {
@@ -375,7 +369,8 @@ private JdbcValue convertToJdbcValue(RelationalPersistentProperty property, @Nul
375369
if (value.getClass().isArray()) {
376370

377371
Object[] valueAsArray = (Object[]) value;
378-
Object[] mappedValueArray = new Object[valueAsArray.length];
372+
@Nullable
373+
Object[] mappedValueArray = new Object @Nullable [valueAsArray.length];
379374
SQLType jdbcType = null;
380375

381376
for (int i = 0; i < valueAsArray.length; i++) {
@@ -486,7 +481,6 @@ protected Object convertValue(@Nullable Object value, TypeInformation<?> typeInf
486481
: TypeInformation.OBJECT);
487482

488483
Assert.state(first != null, "First value must not be null");
489-
490484
Assert.state(second != null, "Second value must not be null");
491485

492486
return Pair.of(first, second);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /