Skip to content

Commit a4f8e2b

Browse files
committed
FenceStep can't use serialization for equality on its delegate steps anymore, it has to use "real" equality now.
1 parent de7732d commit a4f8e2b

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

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

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

3636
import javax.annotation.Nullable;
3737

38+
import com.diffplug.spotless.generic.FenceStep;
39+
3840
/** Formatter which performs the full formatting. */
3941
public final class Formatter implements Serializable, AutoCloseable {
4042
private static final long serialVersionUID = 1L;
@@ -306,6 +308,8 @@ public void close() {
306308
((FormatterStepImpl.Standard) step).cleanupFormatterFunc();
307309
} else if (step instanceof FormatterStepEqualityOnStateSerialization) {
308310
((FormatterStepEqualityOnStateSerialization) step).cleanupFormatterFunc();
311+
} else if (step instanceof FenceStep.Apply) {
312+
((FenceStep.Apply) step).cleanup();
309313
}
310314
}
311315
}

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
import java.util.regex.Matcher;
2626
import java.util.regex.Pattern;
2727

28+
import javax.annotation.Nullable;
29+
2830
import com.diffplug.spotless.Formatter;
2931
import com.diffplug.spotless.FormatterFunc;
3032
import com.diffplug.spotless.FormatterStep;
3133
import com.diffplug.spotless.LineEnding;
32-
import com.diffplug.spotless.SerializedFunction;
3334

3435
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3536

@@ -81,10 +82,7 @@ private void assertRegexSet() {
8182
/** Returns a step which will apply the given steps but preserve the content selected by the regex / openClose pair. */
8283
public FormatterStep preserveWithin(List<FormatterStep> steps) {
8384
assertRegexSet();
84-
return FormatterStep.createLazy(name,
85-
() -> new PreserveWithin(regex, steps),
86-
SerializedFunction.identity(),
87-
state -> FormatterFunc.Closeable.of(state.buildFormatter(), state));
85+
return new PreserveWithin(name, regex, steps);
8886
}
8987

9088
/**
@@ -93,17 +91,14 @@ public FormatterStep preserveWithin(List<FormatterStep> steps) {
9391
*/
9492
public FormatterStep applyWithin(List<FormatterStep> steps) {
9593
assertRegexSet();
96-
return FormatterStep.createLazy(name,
97-
() -> new ApplyWithin(regex, steps),
98-
SerializedFunction.identity(),
99-
state -> FormatterFunc.Closeable.of(state.buildFormatter(), state));
94+
return new ApplyWithin(name, regex, steps);
10095
}
10196

10297
static class ApplyWithin extends Apply implements FormatterFunc.Closeable.ResourceFuncNeedsFile<Formatter> {
10398
private static final long serialVersionUID = 17061466531957339L;
10499

105-
ApplyWithin(Pattern regex, List<FormatterStep> steps) {
106-
super(regex, steps);
100+
ApplyWithin(String name, Pattern regex, List<FormatterStep> steps) {
101+
super(name, regex, steps);
107102
}
108103

109104
@Override
@@ -122,8 +117,8 @@ public String apply(Formatter formatter, String unix, File file) throws Exceptio
122117
static class PreserveWithin extends Apply implements FormatterFunc.Closeable.ResourceFuncNeedsFile<Formatter> {
123118
private static final long serialVersionUID = -8676786492305178343L;
124119

125-
PreserveWithin(Pattern regex, List<FormatterStep> steps) {
126-
super(regex, steps);
120+
PreserveWithin(String name, Pattern regex, List<FormatterStep> steps) {
121+
super(name, regex, steps);
127122
}
128123

129124
private void storeGroups(String unix) {
@@ -144,15 +139,17 @@ public String apply(Formatter formatter, String unix, File file) throws Exceptio
144139
}
145140

146141
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
147-
static class Apply implements Serializable {
142+
public static abstract class Apply implements Serializable, FormatterStep, FormatterFunc.Closeable.ResourceFuncNeedsFile<Formatter> {
143+
final String name;
148144
private static final long serialVersionUID = -2301848328356559915L;
149145
final Pattern regex;
150146
final List<FormatterStep> steps;
151147

152148
transient ArrayList<String> groups = new ArrayList<>();
153149
transient StringBuilder builderInternal;
154150

155-
public Apply(Pattern regex, List<FormatterStep> steps) {
151+
public Apply(String name, Pattern regex, List<FormatterStep> steps) {
152+
this.name = name;
156153
this.regex = regex;
157154
this.steps = steps;
158155
}
@@ -218,5 +215,43 @@ protected String assembleGroups(String unix) {
218215
throw new Error("An intermediate step removed a match of " + pattern);
219216
}
220217
}
218+
219+
@Override
220+
public String getName() {
221+
return name;
222+
}
223+
224+
private transient Formatter formatter;
225+
226+
@Nullable
227+
@Override
228+
public String format(String rawUnix, File file) throws Exception {
229+
if (formatter == null) {
230+
formatter = buildFormatter();
231+
}
232+
return this.apply(formatter, rawUnix, file);
233+
}
234+
235+
@Override
236+
public boolean equals(Object o) {
237+
if (this == o)
238+
return true;
239+
if (o == null || getClass() != o.getClass())
240+
return false;
241+
Apply step = (Apply) o;
242+
return name.equals(step.name) && regex.pattern().equals(step.regex.pattern()) && regex.flags() == step.regex.flags() && steps.equals(step.steps);
243+
}
244+
245+
@Override
246+
public int hashCode() {
247+
return Objects.hash(name, regex.pattern(), regex.flags(), steps);
248+
}
249+
250+
public void cleanup() {
251+
if (formatter != null) {
252+
formatter.close();
253+
formatter = null;
254+
}
255+
}
221256
}
222257
}

0 commit comments

Comments
 (0)