diff --git a/lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java index 829652a5d..18d64b475 100644 --- a/lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,4 +29,9 @@ abstract class DelegateFormatterStep implements FormatterStep { public final String getName() { return delegateStep.getName(); } + + @Override + public void close() throws Exception { + delegateStep.close(); + } } diff --git a/lib/src/main/java/com/diffplug/spotless/Formatter.java b/lib/src/main/java/com/diffplug/spotless/Formatter.java index 05b27c1cf..9400989dd 100644 --- a/lib/src/main/java/com/diffplug/spotless/Formatter.java +++ b/lib/src/main/java/com/diffplug/spotless/Formatter.java @@ -35,8 +35,6 @@ import javax.annotation.Nullable; -import com.diffplug.spotless.generic.FenceStep; - /** Formatter which performs the full formatting. */ public final class Formatter implements Serializable, AutoCloseable { private static final long serialVersionUID = 1L; @@ -304,15 +302,10 @@ public boolean equals(Object obj) { @Override public void close() { for (FormatterStep step : steps) { - if (step instanceof DelegateFormatterStep) { - step = ((DelegateFormatterStep) step).delegateStep; - } - if (step instanceof FormatterStepImpl.Standard) { - ((FormatterStepImpl.Standard) step).cleanupFormatterFunc(); - } else if (step instanceof FormatterStepEqualityOnStateSerialization) { - ((FormatterStepEqualityOnStateSerialization) step).cleanupFormatterFunc(); - } else if (step instanceof FenceStep.BaseStep) { - ((FenceStep.BaseStep) step).cleanup(); + try { + step.close(); + } catch (Exception e) { + throw ThrowingEx.asRuntime(e); } } } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java index 26bc1e56e..d2e87b867 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java @@ -27,7 +27,7 @@ * The input is guaranteed to have unix-style newlines, and the output is required * to not introduce any windows-style newlines as well. */ -public interface FormatterStep extends Serializable { +public interface FormatterStep extends Serializable, AutoCloseable { /** The name of the step, for debugging purposes. */ String getName(); diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepEqualityOnStateSerialization.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepEqualityOnStateSerialization.java index 0ff279c5f..52bf9fc76 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepEqualityOnStateSerialization.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepEqualityOnStateSerialization.java @@ -64,7 +64,8 @@ public int hashCode() { return Arrays.hashCode(serializedState()); } - void cleanupFormatterFunc() { + @Override + public void close() { if (formatter instanceof FormatterFunc.Closeable) { ((FormatterFunc.Closeable) formatter).close(); formatter = null; diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java index 869e7b796..8e2982d6d 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * 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 return formatter.apply(rawUnix, file); } - void cleanupFormatterFunc() { + @Override + public void close() throws Exception { if (formatter instanceof FormatterFunc.Closeable) { ((FormatterFunc.Closeable) formatter).close(); formatter = null; @@ -114,6 +115,14 @@ protected String format(Integer state, String rawUnix, File file) throws Excepti } return formatter.apply(rawUnix, file); } + + @Override + public void close() throws Exception { + if (formatter instanceof FormatterFunc.Closeable) { + ((FormatterFunc.Closeable) formatter).close(); + formatter = null; + } + } } static void checkNotSentinel(File file) { diff --git a/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java b/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java index e1b486bc2..1db94d088 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java @@ -247,7 +247,8 @@ public int hashCode() { return Objects.hash(name, regex.pattern(), regex.flags(), steps); } - public void cleanup() { + @Override + public void close() { if (formatter != null) { formatter.close(); formatter = null; diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java index 2f64dac0f..682855755 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java @@ -151,5 +151,11 @@ public String format(String rawUnix, File file) throws Exception { return formatterFunc.apply(rawUnix, file); } + @Override + public void close() throws Exception { + if (formatterFunc instanceof FormatterFunc.Closeable) { + ((FormatterFunc.Closeable) formatterFunc).close(); + } + } } }