Skip to content

Commit

Permalink
FormatterStep now extends AutoCloseable, instead of having a bunch …
Browse files Browse the repository at this point in the history
…of ad-hoc cleanup methods. (#2091)
  • Loading branch information
nedtwigg committed Apr 26, 2024
2 parents 7262511 + 2c4d883 commit 04d5f28
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
@@ -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.
Expand Down Expand Up @@ -29,4 +29,9 @@ abstract class DelegateFormatterStep implements FormatterStep {
public final String getName() {
return delegateStep.getName();
}

@Override
public void close() throws Exception {
delegateStep.close();
}
}
15 changes: 4 additions & 11 deletions lib/src/main/java/com/diffplug/spotless/Formatter.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/com/diffplug/spotless/FormatterStep.java
Expand Up @@ -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();

Expand Down
Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions 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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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();
}
}
}
}

0 comments on commit 04d5f28

Please sign in to comment.