Skip to content

Commit 08c86dc

Browse files
committed
Polishing.
Fix nullability issues. See #2217
1 parent e892849 commit 08c86dc

File tree

6 files changed

+55
-61
lines changed

6 files changed

+55
-61
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Map;
2020
import java.util.Map.Entry;
2121

22-
import org.jspecify.annotations.Nullable;
2322
import org.springframework.core.convert.TypeDescriptor;
2423
import org.springframework.core.convert.converter.ConditionalConverter;
2524
import org.springframework.core.convert.converter.Converter;
@@ -32,16 +31,14 @@
3231
*/
3332
class IterableOfEntryToMapConverter implements ConditionalConverter, Converter<Iterable<?>, Map<?, ?>> {
3433

35-
@SuppressWarnings("unchecked")
36-
@Nullable
3734
@Override
3835
public Map<?, ?> convert(Iterable<?> source) {
3936

40-
Map result = new HashMap();
37+
Map<Object, Object> result = new HashMap<>();
4138

4239
source.forEach(element -> {
4340

44-
if (element instanceof Entry entry) {
41+
if (element instanceof Entry<?, ?> entry) {
4542
result.put(entry.getKey(), entry.getValue());
4643
return;
4744
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ private Class<?> doGetColumnType(RelationalPersistentProperty property) {
200200
* @return
201201
*/
202202
@Override
203-
@Nullable
204-
public Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
203+
public @Nullable Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
205204

206205
if (null == value) {
207206
return null;
@@ -573,12 +572,12 @@ private record ResolvingConversionContext(ConversionContext delegate, AggregateP
573572
Identifier identifier) implements ConversionContext {
574573

575574
@Override
576-
public <S> S convert(Object source, TypeInformation<? extends S> typeHint) {
575+
public <S> @Nullable S convert(Object source, TypeInformation<? extends S> typeHint) {
577576
return delegate.convert(source, typeHint);
578577
}
579578

580579
@Override
581-
public <S> S convert(Object source, TypeInformation<? extends S> typeHint, ConversionContext context) {
580+
public <S> @Nullable S convert(Object source, TypeInformation<? extends S> typeHint, ConversionContext context) {
582581
return delegate.convert(source, typeHint, context);
583582
}
584583

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/convert/R2dbcConverters.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ public static Collection<Object> getConvertersToRegister() {
7070
*
7171
* @author Hebert Coelho
7272
*/
73-
public enum RowToBooleanConverter implements Converter<Row, Boolean> {
73+
public enum RowToBooleanConverter implements Converter<Row, @Nullable Boolean> {
7474

7575
INSTANCE;
7676

7777
@Override
78-
public @Nullable Boolean convert(Row row) {
78+
public Boolean convert(Row row) {
7979
return row.get(0, Boolean.class);
8080
}
8181
}
@@ -85,12 +85,12 @@ public enum RowToBooleanConverter implements Converter<Row, Boolean> {
8585
*
8686
* @author Hebert Coelho
8787
*/
88-
public enum RowToLocalDateConverter implements Converter<Row, LocalDate> {
88+
public enum RowToLocalDateConverter implements Converter<Row, @Nullable LocalDate> {
8989

9090
INSTANCE;
9191

9292
@Override
93-
public @Nullable LocalDate convert(Row row) {
93+
public LocalDate convert(Row row) {
9494
return row.get(0, LocalDate.class);
9595
}
9696
}
@@ -100,12 +100,12 @@ public enum RowToLocalDateConverter implements Converter<Row, LocalDate> {
100100
*
101101
* @author Hebert Coelho
102102
*/
103-
public enum RowToLocalDateTimeConverter implements Converter<Row, LocalDateTime> {
103+
public enum RowToLocalDateTimeConverter implements Converter<Row, @Nullable LocalDateTime> {
104104

105105
INSTANCE;
106106

107107
@Override
108-
public @Nullable LocalDateTime convert(Row row) {
108+
public LocalDateTime convert(Row row) {
109109
return row.get(0, LocalDateTime.class);
110110
}
111111
}
@@ -115,12 +115,12 @@ public enum RowToLocalDateTimeConverter implements Converter<Row, LocalDateTime>
115115
*
116116
* @author Hebert Coelho
117117
*/
118-
public enum RowToLocalTimeConverter implements Converter<Row, LocalTime> {
118+
public enum RowToLocalTimeConverter implements Converter<Row, @Nullable LocalTime> {
119119

120120
INSTANCE;
121121

122122
@Override
123-
public @Nullable LocalTime convert(Row row) {
123+
public LocalTime convert(Row row) {
124124
return row.get(0, LocalTime.class);
125125
}
126126
}
@@ -141,20 +141,21 @@ public enum RowToLocalTimeConverter implements Converter<Row, LocalTime> {
141141
* @see java.math.BigDecimal
142142
* @author Hebert Coelho
143143
*/
144-
public enum RowToNumberConverterFactory implements ConverterFactory<Row, Number> {
144+
public enum RowToNumberConverterFactory implements ConverterFactory<Row, @Nullable Number> {
145145

146146
INSTANCE;
147147

148148
@Override
149-
public <T extends Number> Converter<Row, T> getConverter(Class<T> targetType) {
149+
public <T extends @Nullable Number> Converter<Row, T> getConverter(Class<T> targetType) {
150150
Assert.notNull(targetType, "Target type must not be null");
151151
return new RowToNumber<>(targetType);
152152
}
153153

154-
record RowToNumber<T extends Number>(Class<T> targetType) implements Converter<Row, T> {
154+
@SuppressWarnings("NullAway")
155+
record RowToNumber<T extends @Nullable Number>(Class<T> targetType) implements Converter<Row, @Nullable T> {
155156

156157
@Override
157-
public @Nullable T convert(Row source) {
158+
public T convert(Row source) {
158159

159160
Object object = source.get(0);
160161

@@ -168,12 +169,12 @@ record RowToNumber<T extends Number>(Class<T> targetType) implements Converter<R
168169
*
169170
* @author Hebert Coelho
170171
*/
171-
public enum RowToOffsetDateTimeConverter implements Converter<Row, OffsetDateTime> {
172+
public enum RowToOffsetDateTimeConverter implements Converter<Row, @Nullable OffsetDateTime> {
172173

173174
INSTANCE;
174175

175176
@Override
176-
public @Nullable OffsetDateTime convert(Row row) {
177+
public OffsetDateTime convert(Row row) {
177178
return row.get(0, OffsetDateTime.class);
178179
}
179180
}
@@ -183,12 +184,12 @@ public enum RowToOffsetDateTimeConverter implements Converter<Row, OffsetDateTim
183184
*
184185
* @author Hebert Coelho
185186
*/
186-
public enum RowToStringConverter implements Converter<Row, String> {
187+
public enum RowToStringConverter implements Converter<Row, @Nullable String> {
187188

188189
INSTANCE;
189190

190191
@Override
191-
public @Nullable String convert(Row row) {
192+
public String convert(Row row) {
192193
return row.get(0, String.class);
193194
}
194195
}
@@ -198,12 +199,12 @@ public enum RowToStringConverter implements Converter<Row, String> {
198199
*
199200
* @author Hebert Coelho
200201
*/
201-
public enum RowToUuidConverter implements Converter<Row, UUID> {
202+
public enum RowToUuidConverter implements Converter<Row, @Nullable UUID> {
202203

203204
INSTANCE;
204205

205206
@Override
206-
public @Nullable UUID convert(Row row) {
207+
public UUID convert(Row row) {
207208
return row.get(0, UUID.class);
208209
}
209210
}
@@ -213,12 +214,12 @@ public enum RowToUuidConverter implements Converter<Row, UUID> {
213214
*
214215
* @author Hebert Coelho
215216
*/
216-
public enum RowToZonedDateTimeConverter implements Converter<Row, ZonedDateTime> {
217+
public enum RowToZonedDateTimeConverter implements Converter<Row, @Nullable ZonedDateTime> {
217218

218219
INSTANCE;
219220

220221
@Override
221-
public @Nullable ZonedDateTime convert(Row row) {
222+
public ZonedDateTime convert(Row row) {
222223
return row.get(0, ZonedDateTime.class);
223224
}
224225
}

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/dialect/MySqlDialect.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ public Collection<Object> getConverters() {
8080
* @author Michael Berry
8181
*/
8282
@ReadingConverter
83-
public enum ByteToBooleanConverter implements Converter<Byte, Boolean> {
83+
@SuppressWarnings("NullAway")
84+
public enum ByteToBooleanConverter implements Converter<Byte, @Nullable Boolean> {
8485

8586
INSTANCE;
8687

8788
@Override
88-
public @Nullable Boolean convert(@Nullable Byte s) {
89+
public Boolean convert(@Nullable Byte s) {
8990

9091
if (s == null) {
9192
return null;

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,7 @@ protected <S> S readAggregate(ConversionContext context, RowDocumentAccessor doc
351351
if (getConversions().hasCustomReadTarget(RowDocument.class, rawType)) {
352352

353353
S converted = doConvert(documentAccessor.getDocument(), rawType, typeHint.getType());
354-
355354
Assert.state(converted != null, "Converted must not be null");
356-
357355
return converted;
358356
}
359357

@@ -362,7 +360,10 @@ protected <S> S readAggregate(ConversionContext context, RowDocumentAccessor doc
362360
}
363361

364362
if (typeHint.isMap()) {
365-
return context.convert(documentAccessor, typeHint);
363+
364+
S converted = context.convert(documentAccessor, typeHint);
365+
Assert.state(converted != null, "Converted must not be null");
366+
return converted;
366367
}
367368

368369
RelationalPersistentEntity<?> entity = getMappingContext().getPersistentEntity(typeHint);
@@ -424,7 +425,7 @@ protected <S> S readAggregate(ConversionContext context, RowDocumentAccessor doc
424425
* @param targetType the {@link Map} {@link TypeInformation} to be used to unmarshall this {@link RowDocument}.
425426
* @return the converted {@link Collection} or array, will never be {@literal null}.
426427
*/
427-
protected @Nullable Object readCollectionOrArray(ConversionContext context, Collection<?> source,
428+
protected Object readCollectionOrArray(ConversionContext context, Collection<?> source,
428429
TypeInformation<?> targetType) {
429430

430431
Assert.notNull(targetType, "Target type must not be null");
@@ -443,14 +444,14 @@ protected <S> S readAggregate(ConversionContext context, RowDocumentAccessor doc
443444
: CollectionFactory.createCollection(collectionType, rawComponentType, source.size());
444445

445446
if (source.isEmpty()) {
446-
return getPotentiallyConvertedSimpleRead(items, targetType);
447+
return getRequiredPotentiallyConvertedSimpleRead(items, targetType);
447448
}
448449

449450
for (Object element : source) {
450451
items.add(element != null ? context.convert(element, componentType) : element);
451452
}
452453

453-
return getPotentiallyConvertedSimpleRead(items, targetType);
454+
return getRequiredPotentiallyConvertedSimpleRead(items, targetType);
454455
}
455456

456457
private <T> @Nullable T doConvert(Object value, Class<? extends T> target) {
@@ -636,17 +637,17 @@ private boolean shouldReadEmbeddable(ConversionContext context, RelationalPersis
636637
*
637638
* @param value a value as it is returned by the driver accessing the persistence store. May be {@literal null}.
638639
* @param targetType {@link TypeInformation} into which the value is to be converted. Must not be {@literal null}.
639-
* @return The converted value. May be {@literal null}.
640+
* @return the converted value, can be {@literal null}.
640641
*/
641642
@Override
642-
@Nullable
643-
public Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
644-
645-
if (null == value) {
646-
return null;
647-
}
643+
public @Nullable Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
644+
return null == value ? null : getPotentiallyConvertedSimpleRead(value, targetType);
645+
}
648646

649-
return getPotentiallyConvertedSimpleRead(value, targetType);
647+
private Object getRequiredPotentiallyConvertedSimpleRead(Object value, TypeInformation<?> type) {
648+
Object result = getPotentiallyConvertedSimpleRead(value, type);
649+
Assert.state(result != null, "Converted must not be null");
650+
return result;
650651
}
651652

652653
/**
@@ -864,7 +865,7 @@ protected DefaultConversionContext(RelationalConverter sourceConverter,
864865

865866
@SuppressWarnings("unchecked")
866867
@Override
867-
public <S> S convert(Object source, TypeInformation<? extends S> typeHint, ConversionContext context) {
868+
public <S> @Nullable S convert(Object source, TypeInformation<? extends S> typeHint, ConversionContext context) {
868869

869870
Assert.notNull(source, "Source must not be null");
870871
Assert.notNull(typeHint, "TypeInformation must not be null");
@@ -874,7 +875,6 @@ public <S> S convert(Object source, TypeInformation<? extends S> typeHint, Conve
874875
}
875876

876877
if (source instanceof Collection<?> collection) {
877-
878878
if (typeHint.isCollectionLike() || typeHint.getType().isAssignableFrom(Collection.class)) {
879879
return (S) collectionConverter.convert(context, collection, typeHint);
880880
}
@@ -1010,9 +1010,9 @@ protected interface ConversionContext {
10101010
*
10111011
* @param source must not be {@literal null}.
10121012
* @param typeHint must not be {@literal null}.
1013-
* @return the converted object.
1013+
* @return the converted object, can be {@literal null}.
10141014
*/
1015-
default <S> S convert(Object source, TypeInformation<? extends S> typeHint) {
1015+
default <S> @Nullable S convert(Object source, TypeInformation<? extends S> typeHint) {
10161016
return convert(source, typeHint, this);
10171017
}
10181018

@@ -1022,9 +1022,9 @@ default <S> S convert(Object source, TypeInformation<? extends S> typeHint) {
10221022
* @param source must not be {@literal null}.
10231023
* @param typeHint must not be {@literal null}.
10241024
* @param context must not be {@literal null}.
1025-
* @return the converted object.
1025+
* @return the converted object, can be {@literal null}.
10261026
*/
1027-
<S> S convert(Object source, TypeInformation<? extends S> typeHint, ConversionContext context);
1027+
<S> @Nullable S convert(Object source, TypeInformation<? extends S> typeHint, ConversionContext context);
10281028

10291029
/**
10301030
* Obtain a {@link ConversionContext} for the given property {@code name}.
@@ -1185,9 +1185,8 @@ private DocumentValueProvider(ConversionContext context, RowDocumentAccessor acc
11851185
}
11861186

11871187
@Override
1188-
@Nullable
11891188
@SuppressWarnings("unchecked")
1190-
public <T> T getPropertyValue(RelationalPersistentProperty property) {
1189+
public <T> @Nullable T getPropertyValue(RelationalPersistentProperty property) {
11911190

11921191
String expression = property.getSpelExpression();
11931192
Object value = expression != null ? evaluator.evaluate(expression) : accessor.get(property);
@@ -1318,7 +1317,7 @@ public ConverterAwareExpressionParameterValueProvider(ConversionContext context,
13181317
}
13191318

13201319
@Override
1321-
protected <T> T potentiallyConvertExpressionValue(Object object,
1320+
protected <T> @Nullable T potentiallyConvertExpressionValue(Object object,
13221321
Parameter<T, RelationalPersistentProperty> parameter) {
13231322
return context.convert(object, parameter.getType());
13241323
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/TypedSubtreeVisitor.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.relational.core.sql.render;
1717

18+
import java.util.Objects;
1819
import java.util.function.Predicate;
1920

2021
import org.jspecify.annotations.Nullable;
@@ -50,23 +51,19 @@ abstract class TypedSubtreeVisitor<T extends Visitable> extends DelegatingVisito
5051
private final ResolvableType type;
5152
private @Nullable Visitable currentSegment;
5253

53-
enum Assignable {
54-
YES, NO,
55-
}
56-
5754
/**
5855
* Creates a new {@link TypedSubtreeVisitor}.
5956
*/
6057
TypedSubtreeVisitor() {
61-
this.type = refCache.computeIfAbsent(this.getClass(),
62-
key -> ResolvableType.forClass(key).as(TypedSubtreeVisitor.class).getGeneric(0));
58+
this.type = Objects.requireNonNull(refCache.computeIfAbsent(this.getClass(),
59+
key -> ResolvableType.forClass(key).as(TypedSubtreeVisitor.class).getGeneric(0)));
6360
}
6461

6562
/**
6663
* Creates a new {@link TypedSubtreeVisitor} with an explicitly provided type.
6764
*/
6865
TypedSubtreeVisitor(Class<T> type) {
69-
this.type = refCache.computeIfAbsent(type, key -> ResolvableType.forClass(type));
66+
this.type = Objects.requireNonNull(refCache.computeIfAbsent(type, key -> ResolvableType.forClass(type)));
7067
}
7168

7269
/**

0 commit comments

Comments
 (0)