Skip to content

Commit 04d5f28

Browse files
authored
FormatterStep now extends AutoCloseable, instead of having a bunch of ad-hoc cleanup methods. (#2091)
2 parents 7262511 + 2c4d883 commit 04d5f28

File tree

7 files changed

+32
-17
lines changed

7 files changed

+32
-17
lines changed

lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2024 DiffPlug
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.
@@ -29,4 +29,9 @@ abstract class DelegateFormatterStep implements FormatterStep {
2929
public final String getName() {
3030
return delegateStep.getName();
3131
}
32+
33+
@Override
34+
public void close() throws Exception {
35+
delegateStep.close();
36+
}
3237
}

lib/src/main/java/com/diffplug/spotless/Formatter.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535

3636
import javax.annotation.Nullable;
3737

38-
import com.diffplug.spotless.generic.FenceStep;
39-
4038
/** Formatter which performs the full formatting. */
4139
public final class Formatter implements Serializable, AutoCloseable {
4240
private static final long serialVersionUID = 1L;
@@ -304,15 +302,10 @@ public boolean equals(Object obj) {
304302
@Override
305303
public void close() {
306304
for (FormatterStep step : steps) {
307-
if (step instanceof DelegateFormatterStep) {
308-
step = ((DelegateFormatterStep) step).delegateStep;
309-
}
310-
if (step instanceof FormatterStepImpl.Standard) {
311-
((FormatterStepImpl.Standard) step).cleanupFormatterFunc();
312-
} else if (step instanceof FormatterStepEqualityOnStateSerialization) {
313-
((FormatterStepEqualityOnStateSerialization) step).cleanupFormatterFunc();
314-
} else if (step instanceof FenceStep.BaseStep) {
315-
((FenceStep.BaseStep) step).cleanup();
305+
try {
306+
step.close();
307+
} catch (Exception e) {
308+
throw ThrowingEx.asRuntime(e);
316309
}
317310
}
318311
}

lib/src/main/java/com/diffplug/spotless/FormatterStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* The input is guaranteed to have unix-style newlines, and the output is required
2828
* to not introduce any windows-style newlines as well.
2929
*/
30-
public interface FormatterStep extends Serializable {
30+
public interface FormatterStep extends Serializable, AutoCloseable {
3131
/** The name of the step, for debugging purposes. */
3232
String getName();
3333

lib/src/main/java/com/diffplug/spotless/FormatterStepEqualityOnStateSerialization.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public int hashCode() {
6464
return Arrays.hashCode(serializedState());
6565
}
6666

67-
void cleanupFormatterFunc() {
67+
@Override
68+
public void close() {
6869
if (formatter instanceof FormatterFunc.Closeable) {
6970
((FormatterFunc.Closeable) formatter).close();
7071
formatter = null;

lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
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.
@@ -82,7 +82,8 @@ protected String format(State state, String rawUnix, File file) throws Exception
8282
return formatter.apply(rawUnix, file);
8383
}
8484

85-
void cleanupFormatterFunc() {
85+
@Override
86+
public void close() throws Exception {
8687
if (formatter instanceof FormatterFunc.Closeable) {
8788
((FormatterFunc.Closeable) formatter).close();
8889
formatter = null;
@@ -114,6 +115,14 @@ protected String format(Integer state, String rawUnix, File file) throws Excepti
114115
}
115116
return formatter.apply(rawUnix, file);
116117
}
118+
119+
@Override
120+
public void close() throws Exception {
121+
if (formatter instanceof FormatterFunc.Closeable) {
122+
((FormatterFunc.Closeable) formatter).close();
123+
formatter = null;
124+
}
125+
}
117126
}
118127

119128
static void checkNotSentinel(File file) {

lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ public int hashCode() {
247247
return Objects.hash(name, regex.pattern(), regex.flags(), steps);
248248
}
249249

250-
public void cleanup() {
250+
@Override
251+
public void close() {
251252
if (formatter != null) {
252253
formatter.close();
253254
formatter = null;

testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,11 @@ public String format(String rawUnix, File file) throws Exception {
151151
return formatterFunc.apply(rawUnix, file);
152152
}
153153

154+
@Override
155+
public void close() throws Exception {
156+
if (formatterFunc instanceof FormatterFunc.Closeable) {
157+
((FormatterFunc.Closeable) formatterFunc).close();
158+
}
159+
}
154160
}
155161
}

0 commit comments

Comments
 (0)