Skip to content

Commit ecf9891

Browse files
committed
Fix that ExitStatus#setExitException breaks immutability contract
Close GH-5366 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent d8632a3 commit ecf9891

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

spring-batch-core/src/main/java/org/springframework/batch/core/ExitStatus.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Dave Syer
3131
* @author Mahmoud Ben Hassine
3232
* @author JiWon Seo
33+
* @author Yanming Zhou
3334
*
3435
*/
3536
public class ExitStatus implements Serializable, Comparable<ExitStatus> {
@@ -73,7 +74,7 @@ public class ExitStatus implements Serializable, Comparable<ExitStatus> {
7374

7475
private final String exitDescription;
7576

76-
@Nullable private Throwable exitException;
77+
private final @Nullable Throwable exitException;
7778

7879
/**
7980
* Constructor that accepts the exit code and sets the exit description to an empty
@@ -91,9 +92,7 @@ public ExitStatus(String exitCode) {
9192
* @param exitDescription The exit description to be used for the {@link ExitStatus}.
9293
*/
9394
public ExitStatus(String exitCode, String exitDescription) {
94-
super();
95-
this.exitCode = exitCode;
96-
this.exitDescription = exitDescription == null ? "" : exitDescription;
95+
this(exitCode, exitDescription, null);
9796
}
9897

9998
/**
@@ -104,8 +103,9 @@ public ExitStatus(String exitCode, String exitDescription) {
104103
* @param exitException The exit exception to the {@link ExitStatus}.
105104
* @since 6.0.3
106105
*/
107-
public ExitStatus(String exitCode, String exitDescription, Throwable exitException) {
108-
this(exitCode, exitDescription);
106+
public ExitStatus(String exitCode, @Nullable String exitDescription, @Nullable Throwable exitException) {
107+
this.exitCode = exitCode;
108+
this.exitDescription = exitDescription == null ? "" : exitDescription;
109109
this.exitException = exitException;
110110
}
111111

@@ -157,14 +157,14 @@ public String getExitDescription() {
157157
* @return a new {@link ExitStatus} combining the current value and the argument
158158
* provided.
159159
*/
160-
public ExitStatus and(ExitStatus status) {
160+
public ExitStatus and(@Nullable ExitStatus status) {
161161
if (status == null) {
162162
return this;
163163
}
164164
ExitStatus result = addExitDescription(status.exitDescription);
165165
Throwable exitException = status.exitException;
166166
if (exitException != null) {
167-
result.setExitException(exitException);
167+
result = result.withExitException(exitException);
168168
}
169169
if (compareTo(status) < 0) {
170170
result = result.replaceExitCode(status.exitCode);
@@ -287,13 +287,12 @@ public ExitStatus addExitDescription(String description) {
287287
}
288288

289289
/**
290-
* Public setter for the exit exception.
290+
* Create new instance with the exit exception.
291291
* @param exitException the last exception that caused the step to exit
292-
* @since 6.0.3
292+
* @since 6.0.4
293293
*/
294-
public ExitStatus setExitException(Throwable exitException) {
295-
this.exitException = exitException;
296-
return this;
294+
public ExitStatus withExitException(Throwable exitException) {
295+
return new ExitStatus(this.exitCode, this.exitDescription, exitException);
297296
}
298297

299298
/**
@@ -314,9 +313,8 @@ public ExitStatus addExitDescription(Throwable throwable) {
314313
* evaluated.
315314
* @return {@code true} if the value matches a known exit code.
316315
*/
317-
public static boolean isNonDefaultExitStatus(ExitStatus status) {
318-
return status == null || status.getExitCode() == null
319-
|| status.getExitCode().equals(ExitStatus.COMPLETED.getExitCode())
316+
public static boolean isNonDefaultExitStatus(@Nullable ExitStatus status) {
317+
return status == null || status.getExitCode().equals(ExitStatus.COMPLETED.getExitCode())
320318
|| status.getExitCode().equals(ExitStatus.EXECUTING.getExitCode())
321319
|| status.getExitCode().equals(ExitStatus.FAILED.getExitCode())
322320
|| status.getExitCode().equals(ExitStatus.NOOP.getExitCode())

spring-batch-samples/src/main/java/org/springframework/batch/samples/chunking/local/LocalChunkingJobConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public ChunkProcessor<Vet> chunkProcessor(DataSource dataSource, TransactionTemp
105105
catch (Exception e) {
106106
transactionStatus.setRollbackOnly();
107107
contribution.incrementWriteSkipCount(chunk.size());
108-
contribution.setExitStatus(ExitStatus.FAILED.setExitException(e));
108+
contribution.setExitStatus(ExitStatus.FAILED.withExitException(e));
109109
}
110110
});
111111
}

0 commit comments

Comments
 (0)