Skip to content

Commit 353736d

Browse files
authored
Serializable refactor legacy eclipse steps (#1952)
2 parents 33093c7 + 6b3e692 commit 353736d

File tree

4 files changed

+46
-75
lines changed

4 files changed

+46
-75
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/EclipseBasedStepBuilder.java

+29-23
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
import com.diffplug.spotless.FormatterStep;
3434
import com.diffplug.spotless.JarState;
3535
import com.diffplug.spotless.Provisioner;
36-
import com.diffplug.spotless.ThrowingEx;
36+
import com.diffplug.spotless.SerializedFunction;
3737

3838
/**
3939
* Generic Eclipse based formatter step {@link State} builder.
4040
*/
4141
public class EclipseBasedStepBuilder {
4242
private final String formatterName;
4343
private final String formatterStepExt;
44-
private final ThrowingEx.Function<State, FormatterFunc> stateToFormatter;
44+
private final SerializedFunction<State, FormatterFunc> stateToFormatter;
4545
private final Provisioner jarProvisioner;
4646
private String formatterVersion;
4747

@@ -63,12 +63,12 @@ public class EclipseBasedStepBuilder {
6363
private Iterable<File> settingsFiles = new ArrayList<>();
6464

6565
/** Initialize valid default configuration, taking latest version */
66-
public EclipseBasedStepBuilder(String formatterName, Provisioner jarProvisioner, ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
66+
public EclipseBasedStepBuilder(String formatterName, Provisioner jarProvisioner, SerializedFunction<State, FormatterFunc> stateToFormatter) {
6767
this(formatterName, "", jarProvisioner, stateToFormatter);
6868
}
6969

7070
/** Initialize valid default configuration, taking latest version */
71-
public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Provisioner jarProvisioner, ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
71+
public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Provisioner jarProvisioner, SerializedFunction<State, FormatterFunc> stateToFormatter) {
7272
this.formatterName = Objects.requireNonNull(formatterName, "formatterName");
7373
this.formatterStepExt = Objects.requireNonNull(formatterStepExt, "formatterStepExt");
7474
this.jarProvisioner = Objects.requireNonNull(jarProvisioner, "jarProvisioner");
@@ -78,7 +78,11 @@ public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Pr
7878

7979
/** Returns the FormatterStep (whose state will be calculated lazily). */
8080
public FormatterStep build() {
81-
return FormatterStep.createLazy(formatterName + formatterStepExt, this::get, stateToFormatter);
81+
var roundtrippableState = new EclipseStep(formatterVersion, formatterStepExt, FileSignature.promise(settingsFiles), JarState.promise(() -> {
82+
return JarState.withoutTransitives(dependencies, jarProvisioner);
83+
}));
84+
return FormatterStep.create(formatterName + formatterStepExt, roundtrippableState,
85+
EclipseStep::state, stateToFormatter);
8286
}
8387

8488
/** Set dependencies for the corresponding Eclipse version */
@@ -122,21 +126,23 @@ public void setPreferences(Iterable<File> settingsFiles) {
122126
this.settingsFiles = settingsFiles;
123127
}
124128

125-
/** Creates the state of the configuration. */
126-
EclipseBasedStepBuilder.State get() throws IOException {
127-
/*
128-
* The current use case is tailored for Gradle.
129-
* Gradle calls this method only once per execution
130-
* and compares the State with the one of a previous run
131-
* for incremental building.
132-
* Hence a lazy construction is not required.
133-
*/
134-
return new State(
135-
formatterVersion,
136-
formatterStepExt,
137-
jarProvisioner,
138-
dependencies,
139-
settingsFiles);
129+
static class EclipseStep implements Serializable {
130+
private static final long serialVersionUID = 1;
131+
private final String semanticVersion;
132+
private final String formatterStepExt;
133+
private final FileSignature.Promised settingsPromise;
134+
private final JarState.Promised jarPromise;
135+
136+
EclipseStep(String semanticVersion, String formatterStepExt, FileSignature.Promised settingsPromise, JarState.Promised jarPromise) {
137+
this.semanticVersion = semanticVersion;
138+
this.formatterStepExt = formatterStepExt;
139+
this.settingsPromise = settingsPromise;
140+
this.jarPromise = jarPromise;
141+
}
142+
143+
private State state() {
144+
return new State(semanticVersion, formatterStepExt, jarPromise.get(), settingsPromise.get());
145+
}
140146
}
141147

142148
/**
@@ -155,9 +161,9 @@ public static class State implements Serializable {
155161
private final FileSignature settingsFiles;
156162

157163
/** State constructor expects that all passed items are not modified afterwards */
158-
protected State(String formatterVersion, String formatterStepExt, Provisioner jarProvisioner, List<String> dependencies, Iterable<File> settingsFiles) throws IOException {
159-
this.jarState = JarState.withoutTransitives(dependencies, jarProvisioner);
160-
this.settingsFiles = FileSignature.signAsList(settingsFiles);
164+
protected State(String formatterVersion, String formatterStepExt, JarState jarState, FileSignature settingsFiles) {
165+
this.jarState = jarState;
166+
this.settingsFiles = settingsFiles;
161167
this.formatterStepExt = formatterStepExt;
162168
semanticVersion = convertEclipseVersion(formatterVersion);
163169
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 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.
@@ -21,8 +21,8 @@
2121
import java.util.Arrays;
2222

2323
import com.diffplug.spotless.FormatterStep;
24-
import com.diffplug.spotless.LineEnding;
2524
import com.diffplug.spotless.ResourceHarness;
25+
import com.diffplug.spotless.StepHarnessWithFile;
2626
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2727

2828
/**
@@ -43,57 +43,19 @@
4343
*/
4444
public class EclipseResourceHarness extends ResourceHarness {
4545
private final EclipseBasedStepBuilder stepBuilder;
46-
private final String fileName;
47-
private final String input;
48-
private final String expected;
4946

5047
/**
5148
* Create harness to be used for several versions of the formatter step
5249
* @param builder Eclipse Formatter step builder
53-
* @param unformatted Simple unformatted input
54-
* @param formatted Expected formatted output
5550
*/
56-
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String unformatted, String formatted) {
57-
this(builder, "someSourceFile", unformatted, formatted);
51+
public EclipseResourceHarness(EclipseBasedStepBuilder builder) {
52+
this.stepBuilder = builder;
5853
}
5954

60-
/**
61-
* Create harness to be used for several versions of the formatter step
62-
* @param builder Eclipse Formatter step builder
63-
* @param sourceFileName File name of the source file
64-
* @param unformatted Simple unformatted input
65-
* @param formatted Expected formatted output
66-
*/
67-
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String sourceFileName, String unformatted, String formatted) {
68-
stepBuilder = builder;
69-
fileName = sourceFileName;
70-
input = unformatted;
71-
expected = formatted;
72-
}
73-
74-
/**
75-
* Assert that formatting input results in expected output
76-
* @param formatterVersion Formatter version
77-
* @param settingsFiles Formatter settings
78-
* @return Formatted string
79-
*/
80-
protected String assertFormatted(String formatterVersion, File... settingsFiles) throws Exception {
81-
String output = format(formatterVersion, settingsFiles);
82-
assertThat(output).isEqualTo(expected);
83-
return output;
84-
}
85-
86-
/**
87-
* Formatting input results and returns output
88-
* @param formatterVersion Formatter version
89-
* @param settingsFiles Formatter settings
90-
* @return Formatted string
91-
*/
92-
protected String format(String formatterVersion, File... settingsFiles) throws Exception {
93-
File inputFile = setFile(fileName).toContent(input);
55+
protected StepHarnessWithFile harnessFor(String formatterVersion, File... settingsFiles) throws Exception {
9456
stepBuilder.setVersion(formatterVersion);
9557
stepBuilder.setPreferences(Arrays.asList(settingsFiles));
9658
FormatterStep step = stepBuilder.build();
97-
return LineEnding.toUnix(step.format(input, inputFile));
59+
return StepHarnessWithFile.forStep(this, step);
9860
}
9961
}

lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 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.
@@ -15,8 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.extra.wtp;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
2018
import java.io.File;
2119
import java.io.FileOutputStream;
2220
import java.io.IOException;
@@ -38,14 +36,18 @@ public class EclipseWtpFormatterStepTest {
3836
private final static Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support("Oldest Version").add(8, "4.8.0");
3937

4038
private static class NestedTests extends EclipseResourceHarness {
39+
private final String unformatted, formatted;
40+
4141
public NestedTests(String unformatted, String formatted, EclipseWtpFormatterStep kind) {
42-
super(kind.createBuilder(TestProvisioner.mavenCentral()), unformatted, formatted);
42+
super(kind.createBuilder(TestProvisioner.mavenCentral()));
43+
this.unformatted = unformatted;
44+
this.formatted = formatted;
4345
}
4446

4547
@ParameterizedTest
4648
@MethodSource
4749
void formatWithVersion(String version) throws Exception {
48-
assertFormatted(version);
50+
harnessFor(version).test("someFilename", unformatted, formatted);
4951
}
5052

5153
private static Stream<String> formatWithVersion() {
@@ -67,8 +69,8 @@ void multipleConfigurations() throws Exception {
6769
config.setProperty("indentationChar", "space");
6870
config.setProperty("indentationSize", "5");
6971
});
70-
String defaultFormatted = assertFormatted(EclipseWtpFormatterStep.defaultVersion(), tabPropertyFile);
71-
assertThat(format(EclipseWtpFormatterStep.defaultVersion(), spacePropertyFile)).as("Space formatting output unexpected").isEqualTo(defaultFormatted.replace("\t", " "));
72+
harnessFor(EclipseWtpFormatterStep.defaultVersion(), tabPropertyFile).test("someFilename", unformatted, formatted);
73+
harnessFor(EclipseWtpFormatterStep.defaultVersion(), spacePropertyFile).test("someFilename", unformatted, formatted.replace("\t", " "));
7274
}
7375

7476
private File createPropertyFile(Consumer<Properties> config) throws IOException {

testlib/src/main/java/com/diffplug/spotless/StepHarnessBase.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import java.util.Locale;
1819
import java.util.Objects;
1920

2021
import org.assertj.core.api.Assertions;
@@ -43,7 +44,7 @@ protected StepHarnessBase(Formatter formatter) {
4344
supportsRoundTrip = true;
4445
} else if (onlyStepName.equals("diktat")) {
4546
supportsRoundTrip = true;
46-
} else if (onlyStepName.equals("eclipse jdt formatter") || onlyStepName.equals("eclipse cdt formatter") || onlyStepName.equals("eclipse groovy formatter")) {
47+
} else if (onlyStepName.toLowerCase(Locale.ROOT).contains("eclipse")) {
4748
supportsRoundTrip = true;
4849
}
4950
}

0 commit comments

Comments
 (0)