Skip to content

Commit a708938

Browse files
committed
Fix NullAway findings
Signed-off-by: Stefano Cordio <[email protected]>
1 parent e87e1f5 commit a708938

24 files changed

+132
-93
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
<!-- Check JSpecify annotations -->
192192
-Xep:NullAway:ERROR
193193
-XepOpt:NullAway:OnlyNullMarked
194+
-XepOpt:NullAway:CustomContractAnnotations=org.springframework.lang.Contract
194195
</compilerArg>
195196
</compilerArgs>
196197
<annotationProcessorPaths>

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/Chunk.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.batch.item;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import java.io.Serializable;
2022
import java.util.ArrayList;
2123
import java.util.Arrays;
@@ -34,6 +36,7 @@
3436
* @author Dave Syer
3537
* @author Mahmoud Ben Hassine
3638
* @author Jinwoo Bae
39+
* @author Stefano Cordio
3740
* @since 2.0
3841
*/
3942
public class Chunk<W> implements Iterable<W>, Serializable {
@@ -44,7 +47,7 @@ public class Chunk<W> implements Iterable<W>, Serializable {
4447

4548
private final List<Exception> errors = new ArrayList<>();
4649

47-
private Object userData;
50+
private @Nullable Object userData;
4851

4952
private boolean end;
5053

@@ -64,8 +67,7 @@ public Chunk(List<? extends W> items) {
6467
this(items, null);
6568
}
6669

67-
public Chunk(List<? extends W> items, List<SkipWrapper<W>> skips) {
68-
super();
70+
public Chunk(@Nullable List<? extends W> items, @Nullable List<SkipWrapper<W>> skips) {
6971
if (items != null) {
7072
this.items.addAll(items);
7173
}
@@ -200,7 +202,7 @@ public void clearSkips() {
200202
skips.clear();
201203
}
202204

203-
public Object getUserData() {
205+
public @Nullable Object getUserData() {
204206
return userData;
205207
}
206208

@@ -247,9 +249,9 @@ public int hashCode() {
247249
*/
248250
public class ChunkIterator implements Iterator<W> {
249251

250-
final private Iterator<W> iterator;
252+
private final Iterator<W> iterator;
251253

252-
private W next;
254+
private @Nullable W next;
253255

254256
public ChunkIterator(List<W> items) {
255257
iterator = items.iterator();

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemStreamSupport.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.item;
1717

18+
import org.jspecify.annotations.Nullable;
1819
import org.springframework.batch.item.util.ExecutionContextUserSupport;
1920

2021
/**
@@ -23,6 +24,7 @@
2324
* @author Dave Syer
2425
* @author Dean de Bree
2526
* @author Mahmoud Ben Hassine
27+
* @author Stefano Cordio
2628
*
2729
*/
2830
public abstract class ItemStreamSupport implements ItemStream {
@@ -43,7 +45,7 @@ public void setName(String name) {
4345
* Get the name of the component
4446
* @return the name of the component
4547
*/
46-
public String getName() {
48+
public @Nullable String getName() {
4749
return executionContextUserSupport.getName();
4850
}
4951

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/KeyValueItemWriter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.springframework.batch.item;
1414

15+
import org.jspecify.annotations.Nullable;
1516
import org.springframework.beans.factory.InitializingBean;
1617
import org.springframework.core.convert.converter.Converter;
1718
import org.springframework.util.Assert;
@@ -22,21 +23,20 @@
2223
*
2324
* @author David Turanski
2425
* @author Mahmoud Ben Hassine
26+
* @author Stefano Cordio
2527
* @since 2.2
2628
*
2729
*/
2830
public abstract class KeyValueItemWriter<K, V> implements ItemWriter<V>, InitializingBean {
2931

30-
protected Converter<V, K> itemKeyMapper;
32+
protected @Nullable Converter<V, K> itemKeyMapper;
3133

3234
protected boolean delete;
3335

3436
@Override
35-
public void write(Chunk<? extends V> items) throws Exception {
36-
if (items == null) {
37-
return;
38-
}
39-
for (V item : items) {
37+
public void write(Chunk<? extends V> chunk) throws Exception {
38+
for (V item : chunk) {
39+
@SuppressWarnings({ "DataFlowIssue", "NullAway" })
4040
K key = itemKeyMapper.convert(item);
4141
writeKeyValue(key, item);
4242
}
@@ -55,7 +55,7 @@ protected void flush() throws Exception {
5555
* @param key the key
5656
* @param value the item
5757
*/
58-
protected abstract void writeKeyValue(K key, V value);
58+
protected abstract void writeKeyValue(@Nullable K key, V value);
5959

6060
/**
6161
* afterPropertiesSet() hook

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/SkipWrapper.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
*
2424
* @author Dave Syer
2525
* @author Mahmoud Ben Hassine
26+
* @author Stefano Cordio
2627
*
2728
*/
2829
public class SkipWrapper<T> {
2930

30-
final private Throwable exception;
31+
private final @Nullable Throwable exception;
3132

32-
final private T item;
33+
private final @Nullable T item;
3334

3435
/**
3536
* @param item the item being wrapped.
@@ -38,7 +39,7 @@ public SkipWrapper(T item) {
3839
this(item, null);
3940
}
4041

41-
public SkipWrapper(T item, @Nullable Throwable e) {
42+
public SkipWrapper(@Nullable T item, @Nullable Throwable e) {
4243
this.item = item;
4344
this.exception = e;
4445
}
@@ -55,7 +56,7 @@ public SkipWrapper(T item, @Nullable Throwable e) {
5556
* Public getter for the item.
5657
* @return the item
5758
*/
58-
public T getItem() {
59+
public @Nullable T getItem() {
5960
return item;
6061
}
6162

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/SpELItemKeyMapper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,29 @@
1212
*/
1313
package org.springframework.batch.item;
1414

15+
import org.jspecify.annotations.Nullable;
1516
import org.springframework.core.convert.converter.Converter;
1617
import org.springframework.expression.Expression;
17-
import org.springframework.expression.ExpressionParser;
1818
import org.springframework.expression.spel.standard.SpelExpressionParser;
1919

2020
/**
2121
* An implementation of {@link Converter} that uses SpEL to map a Value to a key
2222
*
2323
* @author David Turanski
24+
* @author Stefano Cordio
2425
* @since 2.2
2526
*/
2627
public class SpELItemKeyMapper<K, V> implements Converter<V, K> {
2728

28-
private final ExpressionParser parser = new SpelExpressionParser();
29-
3029
private final Expression parsedExpression;
3130

3231
public SpELItemKeyMapper(String keyExpression) {
33-
parsedExpression = parser.parseExpression(keyExpression);
32+
parsedExpression = new SpelExpressionParser().parseExpression(keyExpression);
3433
}
3534

3635
@SuppressWarnings("unchecked")
3736
@Override
38-
public K convert(V item) {
37+
public @Nullable K convert(V item) {
3938
return (K) parsedExpression.getValue(item);
4039
}
4140

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/AbstractMethodInvokingDelegator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.List;
2424

25+
import org.jspecify.annotations.Nullable;
2526
import org.springframework.beans.factory.InitializingBean;
2627
import org.springframework.util.Assert;
2728
import org.springframework.util.ClassUtils;
@@ -41,12 +42,13 @@
4142
* @author Robert Kasanicky
4243
* @author Mahmoud Ben Hassine
4344
* @author Glenn Renfro
45+
* @author Stefano Cordio
4446
*/
4547
public abstract class AbstractMethodInvokingDelegator<T> implements InitializingBean {
4648

47-
private Object targetObject;
49+
private @Nullable Object targetObject;
4850

49-
private String targetMethod;
51+
private @Nullable String targetMethod;
5052

5153
private Object[] arguments;
5254

@@ -88,7 +90,7 @@ protected T invokeDelegateMethodWithArguments(Object[] args) throws Exception {
8890
/**
8991
* Create a new configured instance of {@link MethodInvoker}.
9092
*/
91-
private MethodInvoker createMethodInvoker(Object targetObject, String targetMethod) {
93+
private MethodInvoker createMethodInvoker(@Nullable Object targetObject, @Nullable String targetMethod) {
9294
HippyMethodInvoker invoker = new HippyMethodInvoker();
9395
invoker.setTargetObject(targetObject);
9496
invoker.setTargetMethod(targetMethod);
@@ -220,15 +222,15 @@ protected Object[] getArguments() {
220222
* @return the object on which the method will be invoked.
221223
* @since 5.1
222224
*/
223-
protected Object getTargetObject() {
225+
protected @Nullable Object getTargetObject() {
224226
return targetObject;
225227
}
226228

227229
/**
228230
* @return the name of the method to be invoked.
229231
* @since 5.1
230232
*/
231-
protected String getTargetMethod() {
233+
protected @Nullable String getTargetMethod() {
232234
return targetMethod;
233235
}
234236

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/HippyMethodInvoker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
/**
2525
* A {@link MethodInvoker} that is a bit relaxed about its arguments. You can give it
26-
* arguments in the wrong order or you can give it too many arguments and it will try and
27-
* find a method that matches a subset.
26+
* arguments in the wrong order, or you can give it too many arguments, and it will try
27+
* and find a method that matches a subset.
2828
*
2929
* @author Dave Syer
3030
* @since 2.1

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/PropertyExtractingDelegatingItemWriter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.batch.item.adapter;
1818

19-
import java.util.Arrays;
20-
19+
import org.jspecify.annotations.Nullable;
2120
import org.springframework.batch.item.Chunk;
2221
import org.springframework.batch.item.ItemWriter;
2322
import org.springframework.beans.BeanWrapper;
@@ -41,7 +40,7 @@
4140
public class PropertyExtractingDelegatingItemWriter<T> extends AbstractMethodInvokingDelegator<T>
4241
implements ItemWriter<T> {
4342

44-
private String[] fieldsUsedAsTargetMethodArguments;
43+
private @Nullable String[] fieldsUsedAsTargetMethodArguments;
4544

4645
/**
4746
* Extracts values from item's fields named in fieldsUsedAsTargetMethodArguments and

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/AmqpItemReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@
3636
*
3737
* @author Chris Schaefer
3838
* @author Mahmoud Ben Hassine
39+
* @author Stefano Cordio
3940
*/
4041
public class AmqpItemReader<T> implements ItemReader<T> {
4142

4243
private final AmqpTemplate amqpTemplate;
4344

44-
private Class<? extends T> itemType;
45+
private @Nullable Class<? extends T> itemType;
4546

4647
/**
4748
* Initialize the AmqpItemReader.
4849
* @param amqpTemplate the template to be used. Must not be null.
4950
*/
50-
public AmqpItemReader(final AmqpTemplate amqpTemplate) {
51+
public AmqpItemReader(AmqpTemplate amqpTemplate) {
5152
Assert.notNull(amqpTemplate, "AmqpTemplate must not be null");
5253

5354
this.amqpTemplate = amqpTemplate;

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/builder/AmqpItemReaderBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.batch.item.amqp.builder;
1818

19+
import org.jspecify.annotations.Nullable;
1920
import org.springframework.amqp.core.AmqpTemplate;
2021
import org.springframework.batch.item.amqp.AmqpItemReader;
2122
import org.springframework.util.Assert;
@@ -24,14 +25,15 @@
2425
* A builder implementation for the {@link AmqpItemReader}
2526
*
2627
* @author Glenn Renfro
28+
* @author Stefano Cordio
2729
* @since 4.0
2830
* @see AmqpItemReader
2931
*/
3032
public class AmqpItemReaderBuilder<T> {
3133

32-
private AmqpTemplate amqpTemplate;
34+
private @Nullable AmqpTemplate amqpTemplate;
3335

34-
private Class<? extends T> itemType;
36+
private @Nullable Class<? extends T> itemType;
3537

3638
/**
3739
* Establish the amqpTemplate to be used by the AmqpItemReader.

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/builder/AmqpItemWriterBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.batch.item.amqp.builder;
1818

19+
import org.jspecify.annotations.Nullable;
1920
import org.springframework.amqp.core.AmqpTemplate;
2021
import org.springframework.batch.item.amqp.AmqpItemWriter;
2122
import org.springframework.util.Assert;
@@ -24,12 +25,13 @@
2425
* A builder implementation for the {@link AmqpItemWriter}
2526
*
2627
* @author Glenn Renfro
28+
* @author Stefano Cordio
2729
* @since 4.0
2830
* @see AmqpItemWriter
2931
*/
3032
public class AmqpItemWriterBuilder<T> {
3133

32-
private AmqpTemplate amqpTemplate;
34+
private @Nullable AmqpTemplate amqpTemplate;
3335

3436
/**
3537
* Establish the amqpTemplate to be used by the AmqpItemWriter.
@@ -50,9 +52,7 @@ public AmqpItemWriterBuilder<T> amqpTemplate(AmqpTemplate amqpTemplate) {
5052
public AmqpItemWriter<T> build() {
5153
Assert.notNull(this.amqpTemplate, "amqpTemplate is required.");
5254

53-
AmqpItemWriter<T> writer = new AmqpItemWriter<>(this.amqpTemplate);
54-
55-
return writer;
55+
return new AmqpItemWriter<>(this.amqpTemplate);
5656
}
5757

5858
}

0 commit comments

Comments
 (0)