Skip to content

Commit add4c1e

Browse files
authored
Fixup the last of the stragglers (#2146)
2 parents e9962c5 + 5473eb1 commit add4c1e

File tree

20 files changed

+162
-263
lines changed

20 files changed

+162
-263
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,10 @@ public void close() {
312312

313313
/** This Sentinel reference may be used to pass string content to a Formatter or FormatterStep when there is no actual File to format */
314314
public static final File NO_FILE_SENTINEL = new File("NO_FILE_SENTINEL");
315+
316+
static void checkNotSentinel(File file) {
317+
if (file == Formatter.NO_FILE_SENTINEL) {
318+
throw new IllegalArgumentException("This step requires the underlying file. If this is a test, use StepHarnessWithFile");
319+
}
320+
}
315321
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void close() {
115115

116116
@Override
117117
public String apply(String unix, File file) throws Exception {
118-
FormatterStepImpl.checkNotSentinel(file);
118+
Formatter.checkNotSentinel(file);
119119
return function.apply(resource, unix, file);
120120
}
121121

@@ -144,7 +144,7 @@ interface NeedsFile extends FormatterFunc {
144144

145145
@Override
146146
default String apply(String unix, File file) throws Exception {
147-
FormatterStepImpl.checkNotSentinel(file);
147+
Formatter.checkNotSentinel(file);
148148
return applyWithFile(unix, file);
149149
}
150150

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

+3-25
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,6 @@ default FormatterStep filterByFile(SerializableFileFilter filter) {
7070
return new FilterByFileFormatterStep(this, filter);
7171
}
7272

73-
/**
74-
* Implements a FormatterStep in a strict way which guarantees correct and lazy implementation
75-
* of up-to-date checks. This maximizes performance for cases where the FormatterStep is not
76-
* actually needed (e.g. don't load eclipse setting file unless this step is actually running)
77-
* while also ensuring that Gradle can detect changes in a step's settings to determine that
78-
* it needs to rerun a format.
79-
*/
80-
abstract class Strict<State extends Serializable> extends LazyForwardingEquality<State> implements FormatterStep {
81-
private static final long serialVersionUID = 1L;
82-
83-
/**
84-
* Implements the formatting function strictly in terms
85-
* of the input data and the result of {@link #calculateState()}.
86-
*/
87-
protected abstract String format(State state, String rawUnix, File file) throws Exception;
88-
89-
@Override
90-
public final String format(String rawUnix, File file) throws Exception {
91-
return format(state(), rawUnix, file);
92-
}
93-
}
94-
9573
/**
9674
* @param name
9775
* The name of the formatter step.
@@ -151,8 +129,8 @@ static <RoundtripState extends Serializable, EqualityState extends Serializable>
151129
static <State extends Serializable> FormatterStep createLazy(
152130
String name,
153131
ThrowingEx.Supplier<State> stateSupplier,
154-
ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
155-
return new FormatterStepImpl.Standard<>(name, stateSupplier, stateToFormatter);
132+
SerializedFunction<State, FormatterFunc> stateToFormatter) {
133+
return createLazy(name, stateSupplier, SerializedFunction.identity(), stateToFormatter);
156134
}
157135

158136
/**
@@ -168,7 +146,7 @@ static <State extends Serializable> FormatterStep createLazy(
168146
static <State extends Serializable> FormatterStep create(
169147
String name,
170148
State state,
171-
ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
149+
SerializedFunction<State, FormatterFunc> stateToFormatter) {
172150
Objects.requireNonNull(state, "state");
173151
return createLazy(name, () -> state, stateToFormatter);
174152
}

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

-133
This file was deleted.

lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public ClangFormatStep withPathToExe(String pathToExe) {
6464
}
6565

6666
public FormatterStep create() {
67-
return FormatterStep.createLazy(name(), this::createState, RoundtripState::state, State::toFunc);
67+
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
6868
}
6969

70-
private RoundtripState createState() throws IOException, InterruptedException {
70+
private RoundtripState createRoundtrip() throws IOException, InterruptedException {
7171
String howToInstall = "" +
7272
"You can download clang-format from https://releases.llvm.org and " +
7373
"then point Spotless to it with {@code pathToExe('/path/to/clang-format')} " +
@@ -98,13 +98,13 @@ static class RoundtripState implements Serializable {
9898
this.exe = exe;
9999
}
100100

101-
private State state() {
102-
return new State(version, style, exe);
101+
private EqualityState toEquality() {
102+
return new EqualityState(version, style, exe);
103103
}
104104
}
105105

106106
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
107-
static class State implements Serializable {
107+
static class EqualityState implements Serializable {
108108
private static final long serialVersionUID = -1825662356883926318L;
109109
// used for up-to-date checks and caching
110110
final String version;
@@ -113,7 +113,7 @@ static class State implements Serializable {
113113
// used for executing
114114
private transient @Nullable List<String> args;
115115

116-
State(String version, @Nullable String style, ForeignExe pathToExe) {
116+
EqualityState(String version, @Nullable String style, ForeignExe pathToExe) {
117117
this.version = version;
118118
this.style = style;
119119
this.exe = Objects.requireNonNull(pathToExe);

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

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-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.
@@ -35,24 +35,37 @@ private NativeCmdStep() {}
3535
public static FormatterStep create(String name, File pathToExe, List<String> arguments) {
3636
Objects.requireNonNull(name, "name");
3737
Objects.requireNonNull(pathToExe, "pathToExe");
38-
return FormatterStep.createLazy(name, () -> new State(FileSignature.signAsList(pathToExe), arguments), State::toFunc);
38+
return FormatterStep.createLazy(name, () -> new State(FileSignature.promise(pathToExe), arguments), State::toRuntime, Runtime::toFunc);
3939
}
4040

4141
static class State implements Serializable {
42-
private static final long serialVersionUID = 1L;
42+
private static final long serialVersionUID = 2L;
43+
final FileSignature.Promised pathToExe;
44+
final List<String> arguments;
4345

44-
final FileSignature pathToExe;
46+
State(FileSignature.Promised pathToExe, List<String> arguments) {
47+
this.pathToExe = pathToExe;
48+
this.arguments = arguments;
49+
}
50+
51+
Runtime toRuntime() {
52+
return new Runtime(pathToExe.get().getOnlyFile(), arguments);
53+
}
54+
}
4555

56+
static class Runtime implements Serializable {
57+
private static final long serialVersionUID = 2L;
58+
final File pathToExe;
4659
final List<String> arguments;
4760

48-
State(FileSignature pathToExe, List<String> arguments) {
61+
Runtime(File pathToExe, List<String> arguments) {
4962
this.pathToExe = pathToExe;
5063
this.arguments = arguments;
5164
}
5265

5366
String format(ProcessRunner runner, String input) throws IOException, InterruptedException {
5467
List<String> argumentsWithPathToExe = new ArrayList<>();
55-
argumentsWithPathToExe.add(pathToExe.getOnlyFile().getAbsolutePath());
68+
argumentsWithPathToExe.add(pathToExe.getAbsolutePath());
5669
if (arguments != null) {
5770
argumentsWithPathToExe.addAll(arguments);
5871
}

lib/src/main/java/com/diffplug/spotless/go/GofmtFormatStep.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public GofmtFormatStep withGoExecutable(String pathToExe) {
6363
}
6464

6565
public FormatterStep create() {
66-
return FormatterStep.createLazy(name(), this::createState, GofmtFormatStep.State::toFunc);
66+
return FormatterStep.createLazy(name(), this::createRountrip, RoundtripState::toEquality, EqualityState::toFunc);
6767
}
6868

69-
private State createState() throws IOException, InterruptedException {
69+
private RoundtripState createRountrip() throws IOException, InterruptedException {
7070
String howToInstall = "gofmt is a part of standard go distribution. If spotless can't discover it automatically, " +
7171
"you can point Spotless to the go binary with {@code pathToExe('/path/to/go')}";
7272
final ForeignExe exe = ForeignExe.nameAndVersion("go", version)
@@ -76,18 +76,34 @@ private State createState() throws IOException, InterruptedException {
7676
.fixWrongVersion(
7777
"You can tell Spotless to use the version you already have with {@code gofmt('{versionFound}')}" +
7878
"or you can install the currently specified Go version, {version}.\n" + howToInstall);
79-
return new State(this, exe);
79+
return new RoundtripState(version, exe);
80+
}
81+
82+
static class RoundtripState implements Serializable {
83+
private static final long serialVersionUID = 1L;
84+
85+
final String version;
86+
final ForeignExe exe;
87+
88+
RoundtripState(String version, ForeignExe exe) {
89+
this.version = version;
90+
this.exe = exe;
91+
}
92+
93+
private EqualityState toEquality() {
94+
return new EqualityState(version, exe);
95+
}
8096
}
8197

8298
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
83-
static class State implements Serializable {
99+
static class EqualityState implements Serializable {
84100
private static final long serialVersionUID = -1825662355363926318L;
85101
// used for up-to-date checks and caching
86102
final String version;
87103
final transient ForeignExe exe;
88104

89-
public State(GofmtFormatStep step, ForeignExe goExecutable) {
90-
this.version = step.version;
105+
public EqualityState(String version, ForeignExe goExecutable) {
106+
this.version = version;
91107
this.exe = Objects.requireNonNull(goExecutable);
92108
}
93109

lib/src/main/java/com/diffplug/spotless/python/BlackStep.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public BlackStep withPathToExe(String pathToExe) {
5656
}
5757

5858
public FormatterStep create() {
59-
return FormatterStep.createLazy(name(), this::createState, RoundtripState::state, State::toFunc);
59+
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
6060
}
6161

62-
private RoundtripState createState() {
62+
private RoundtripState createRoundtrip() {
6363
String trackingIssue = "\n github issue to handle this better: https://github.com/diffplug/spotless/issues/674";
6464
ForeignExe exeAbsPath = ForeignExe.nameAndVersion("black", version)
6565
.pathToExe(pathToExe)
@@ -80,21 +80,21 @@ static class RoundtripState implements Serializable {
8080
this.exe = exe;
8181
}
8282

83-
private State state() {
84-
return new State(version, exe);
83+
private EqualityState toEquality() {
84+
return new EqualityState(version, exe);
8585
}
8686
}
8787

8888
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
89-
static class State implements Serializable {
89+
static class EqualityState implements Serializable {
9090
private static final long serialVersionUID = -1825662356883926318L;
9191
// used for up-to-date checks and caching
9292
final String version;
9393
final transient ForeignExe exe;
9494
// used for executing
9595
private transient @Nullable String[] args;
9696

97-
State(String version, ForeignExe exeAbsPath) {
97+
EqualityState(String version, ForeignExe exeAbsPath) {
9898
this.version = version;
9999
this.exe = Objects.requireNonNull(exeAbsPath);
100100
}

0 commit comments

Comments
 (0)