Skip to content

Commit d280556

Browse files
hpoettkerfmbenhassine
authored andcommitted
Let fluent setters of SimpleStepBuilder return proper type
Let fluent setters of `SimpleStepBuilder` return `this` with type `SimpleStepBuilder`. This makes the required order of some fluent setters a bit more lenient and prevents that `AbstractTaskletStepBuilder::listener` for parameters of type `Object` is invoked although an overloaded method specific for `SimpleStepBuilder` is intended. Fixes #773 and #1098.
1 parent 8fef41b commit d280556

File tree

10 files changed

+185
-86
lines changed

10 files changed

+185
-86
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/AbstractTaskletStepBuilder.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -51,8 +51,7 @@
5151
* @since 2.2
5252
* @param <B> the type of builder represented
5353
*/
54-
public abstract class AbstractTaskletStepBuilder<B extends AbstractTaskletStepBuilder<B>>
55-
extends StepBuilderHelper<AbstractTaskletStepBuilder<B>> {
54+
public abstract class AbstractTaskletStepBuilder<B extends AbstractTaskletStepBuilder<B>> extends StepBuilderHelper<B> {
5655

5756
protected Set<ChunkListener> chunkListeners = new LinkedHashSet<>();
5857

@@ -137,9 +136,9 @@ protected void registerStepListenerAsChunkListener() {
137136
* @param listener the listener to register
138137
* @return this for fluent chaining
139138
*/
140-
public AbstractTaskletStepBuilder<B> listener(ChunkListener listener) {
139+
public B listener(ChunkListener listener) {
141140
chunkListeners.add(listener);
142-
return this;
141+
return self();
143142
}
144143

145144
/**
@@ -162,19 +161,17 @@ public B listener(Object listener) {
162161
this.listener((ChunkListener) factory.getObject());
163162
}
164163

165-
@SuppressWarnings("unchecked")
166-
B result = (B) this;
167-
return result;
164+
return self();
168165
}
169166

170167
/**
171168
* Register a stream for callbacks that manage restart data.
172169
* @param stream the stream to register
173170
* @return this for fluent chaining
174171
*/
175-
public AbstractTaskletStepBuilder<B> stream(ItemStream stream) {
172+
public B stream(ItemStream stream) {
176173
streams.add(stream);
177-
return this;
174+
return self();
178175
}
179176

180177
/**
@@ -183,9 +180,9 @@ public AbstractTaskletStepBuilder<B> stream(ItemStream stream) {
183180
* @param taskExecutor the task executor to register
184181
* @return this for fluent chaining
185182
*/
186-
public AbstractTaskletStepBuilder<B> taskExecutor(TaskExecutor taskExecutor) {
183+
public B taskExecutor(TaskExecutor taskExecutor) {
187184
this.taskExecutor = taskExecutor;
188-
return this;
185+
return self();
189186
}
190187

191188
/**
@@ -196,9 +193,9 @@ public AbstractTaskletStepBuilder<B> taskExecutor(TaskExecutor taskExecutor) {
196193
* @param throttleLimit maximum number of concurrent tasklet executions allowed
197194
* @return this for fluent chaining
198195
*/
199-
public AbstractTaskletStepBuilder<B> throttleLimit(int throttleLimit) {
196+
public B throttleLimit(int throttleLimit) {
200197
this.throttleLimit = throttleLimit;
201-
return this;
198+
return self();
202199
}
203200

204201
/**
@@ -207,9 +204,9 @@ public AbstractTaskletStepBuilder<B> throttleLimit(int throttleLimit) {
207204
* @param exceptionHandler the exception handler
208205
* @return this for fluent chaining
209206
*/
210-
public AbstractTaskletStepBuilder<B> exceptionHandler(ExceptionHandler exceptionHandler) {
207+
public B exceptionHandler(ExceptionHandler exceptionHandler) {
211208
this.exceptionHandler = exceptionHandler;
212-
return this;
209+
return self();
213210
}
214211

215212
/**
@@ -218,9 +215,9 @@ public AbstractTaskletStepBuilder<B> exceptionHandler(ExceptionHandler exception
218215
* @param repeatTemplate a repeat template with rules for iterating
219216
* @return this for fluent chaining
220217
*/
221-
public AbstractTaskletStepBuilder<B> stepOperations(RepeatOperations repeatTemplate) {
218+
public B stepOperations(RepeatOperations repeatTemplate) {
222219
this.stepOperations = repeatTemplate;
223-
return this;
220+
return self();
224221
}
225222

226223
/**
@@ -230,9 +227,9 @@ public AbstractTaskletStepBuilder<B> stepOperations(RepeatOperations repeatTempl
230227
* @param transactionAttribute a transaction attribute set
231228
* @return this for fluent chaining
232229
*/
233-
public AbstractTaskletStepBuilder<B> transactionAttribute(TransactionAttribute transactionAttribute) {
230+
public B transactionAttribute(TransactionAttribute transactionAttribute) {
234231
this.transactionAttribute = transactionAttribute;
235-
return this;
232+
return self();
236233
}
237234

238235
/**

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ public FaultTolerantStepBuilder<I, O> listener(ChunkListener listener) {
224224
}
225225

226226
@Override
227-
public AbstractTaskletStepBuilder<SimpleStepBuilder<I, O>> transactionAttribute(
228-
TransactionAttribute transactionAttribute) {
227+
public SimpleStepBuilder<I, O> transactionAttribute(TransactionAttribute transactionAttribute) {
229228
return super.transactionAttribute(getTransactionAttribute(transactionAttribute));
230229
}
231230

@@ -394,7 +393,7 @@ public FaultTolerantStepBuilder<I, O> processorNonTransactional() {
394393
}
395394

396395
@Override
397-
public AbstractTaskletStepBuilder<SimpleStepBuilder<I, O>> stream(ItemStream stream) {
396+
public SimpleStepBuilder<I, O> stream(ItemStream stream) {
398397
if (stream instanceof ItemReader<?>) {
399398
if (!streamIsReader) {
400399
streamIsReader = true;

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FlowStepBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2011 the original author or authors.
2+
* Copyright 2006-2022 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.
@@ -69,4 +69,9 @@ public Step build() {
6969
return step;
7070
}
7171

72+
@Override
73+
protected FlowStepBuilder self() {
74+
return this;
75+
}
76+
7277
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/JobStepBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2011 the original author or authors.
2+
* Copyright 2006-2022 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.
@@ -114,4 +114,9 @@ public Step build() {
114114

115115
}
116116

117+
@Override
118+
protected JobStepBuilder self() {
119+
return this;
120+
}
121+
117122
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/PartitionStepBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 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.
@@ -222,6 +222,11 @@ public Step build() {
222222

223223
}
224224

225+
@Override
226+
protected PartitionStepBuilder self() {
227+
return this;
228+
}
229+
225230
protected TaskExecutor getTaskExecutor() {
226231
return taskExecutor;
227232
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/SimpleStepBuilder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ public SimpleStepBuilder<I, O> readerIsTransactionalQueue() {
246246
* @param listener the object that has a method configured with listener annotation
247247
* @return this for fluent chaining
248248
*/
249-
@SuppressWarnings("unchecked")
250249
@Override
251250
public SimpleStepBuilder<I, O> listener(Object listener) {
252251
super.listener(listener);
@@ -268,9 +267,7 @@ public SimpleStepBuilder<I, O> listener(Object listener) {
268267
itemListeners.add((StepListener) factory.getObject());
269268
}
270269

271-
@SuppressWarnings("unchecked")
272-
SimpleStepBuilder<I, O> result = this;
273-
return result;
270+
return this;
274271
}
275272

276273
/**
@@ -315,6 +312,11 @@ public SimpleStepBuilder<I, O> chunkOperations(RepeatOperations repeatTemplate)
315312
return this;
316313
}
317314

315+
@Override
316+
protected SimpleStepBuilder<I, O> self() {
317+
return this;
318+
}
319+
318320
protected RepeatOperations createChunkOperations() {
319321
RepeatOperations repeatOperations = chunkOperations;
320322
if (repeatOperations == null) {

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2022 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.
@@ -124,4 +124,9 @@ public FlowStepBuilder flow(Flow flow) {
124124
return new FlowStepBuilder(this).flow(flow);
125125
}
126126

127+
@Override
128+
protected StepBuilder self() {
129+
return this;
130+
}
131+
127132
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2014 the original author or authors.
2+
* Copyright 2006-2022 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.
@@ -64,23 +64,17 @@ protected StepBuilderHelper(StepBuilderHelper<?> parent) {
6464

6565
public B repository(JobRepository jobRepository) {
6666
properties.jobRepository = jobRepository;
67-
@SuppressWarnings("unchecked")
68-
B result = (B) this;
69-
return result;
67+
return self();
7068
}
7169

7270
public B transactionManager(PlatformTransactionManager transactionManager) {
7371
properties.transactionManager = transactionManager;
74-
@SuppressWarnings("unchecked")
75-
B result = (B) this;
76-
return result;
72+
return self();
7773
}
7874

7975
public B startLimit(int startLimit) {
8076
properties.startLimit = startLimit;
81-
@SuppressWarnings("unchecked")
82-
B result = (B) this;
83-
return result;
77+
return self();
8478
}
8579

8680
/**
@@ -99,25 +93,21 @@ public B listener(Object listener) {
9993
properties.addStepExecutionListener((StepExecutionListener) factory.getObject());
10094
}
10195

102-
@SuppressWarnings("unchecked")
103-
B result = (B) this;
104-
return result;
96+
return self();
10597
}
10698

10799
public B listener(StepExecutionListener listener) {
108100
properties.addStepExecutionListener(listener);
109-
@SuppressWarnings("unchecked")
110-
B result = (B) this;
111-
return result;
101+
return self();
112102
}
113103

114104
public B allowStartIfComplete(boolean allowStartIfComplete) {
115105
properties.allowStartIfComplete = allowStartIfComplete;
116-
@SuppressWarnings("unchecked")
117-
B result = (B) this;
118-
return result;
106+
return self();
119107
}
120108

109+
protected abstract B self();
110+
121111
protected String getName() {
122112
return properties.name;
123113
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/TaskletStepBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2011 the original author or authors.
2+
* Copyright 2006-2022 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,6 +45,11 @@ public TaskletStepBuilder tasklet(Tasklet tasklet) {
4545
return this;
4646
}
4747

48+
@Override
49+
protected TaskletStepBuilder self() {
50+
return this;
51+
}
52+
4853
@Override
4954
protected Tasklet createTasklet() {
5055
return tasklet;

0 commit comments

Comments
 (0)