Skip to content

Commit 7bd9c59

Browse files
committed
Remove the name property from Formatter, and route it in a different way.
1 parent b7f559a commit 7bd9c59

File tree

8 files changed

+29
-54
lines changed

8 files changed

+29
-54
lines changed

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public final class Formatter implements Serializable, AutoCloseable {
3333
private static final long serialVersionUID = 1L;
3434

3535
// The name is used for logging purpose. It does not convey any applicative purpose
36-
private String name;
3736
private LineEnding.Policy lineEndingsPolicy;
3837
private Charset encoding;
3938
private List<FormatterStep> steps;
@@ -48,7 +47,6 @@ private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, List<Fo
4847

4948
// override serialize output
5049
private void writeObject(ObjectOutputStream out) throws IOException {
51-
out.writeObject(name);
5250
out.writeObject(lineEndingsPolicy);
5351
out.writeObject(encoding.name());
5452
out.writeObject(steps);
@@ -58,7 +56,6 @@ private void writeObject(ObjectOutputStream out) throws IOException {
5856
// override serialize input
5957
@SuppressWarnings("unchecked")
6058
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
61-
name = (String) in.readObject();
6259
lineEndingsPolicy = (LineEnding.Policy) in.readObject();
6360
encoding = Charset.forName((String) in.readObject());
6461
steps = (List<FormatterStep>) in.readObject();
@@ -71,10 +68,6 @@ private void readObjectNoData() throws ObjectStreamException {
7168
throw new UnsupportedOperationException();
7269
}
7370

74-
public String getName() {
75-
return name;
76-
}
77-
7871
public LineEnding.Policy getLineEndingsPolicy() {
7972
return lineEndingsPolicy;
8073
}
@@ -96,8 +89,6 @@ public static Formatter.Builder builder() {
9689
}
9790

9891
public static class Builder {
99-
// optional parameters
100-
private String name = "unnamed";
10192
// required parameters
10293
private LineEnding.Policy lineEndingsPolicy;
10394
private Charset encoding;
@@ -106,11 +97,6 @@ public static class Builder {
10697

10798
private Builder() {}
10899

109-
public Builder name(String name) {
110-
this.name = name;
111-
return this;
112-
}
113-
114100
public Builder lineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
115101
this.lineEndingsPolicy = lineEndingsPolicy;
116102
return this;
@@ -183,7 +169,6 @@ public String compute(String unix, File file) {
183169
public int hashCode() {
184170
final int prime = 31;
185171
int result = 1;
186-
result = prime * result + name.hashCode();
187172
result = prime * result + encoding.hashCode();
188173
result = prime * result + lineEndingsPolicy.hashCode();
189174
result = prime * result + steps.hashCode();
@@ -203,8 +188,7 @@ public boolean equals(Object obj) {
203188
return false;
204189
}
205190
Formatter other = (Formatter) obj;
206-
return name.equals(other.name) &&
207-
encoding.equals(other.encoding) &&
191+
return encoding.equals(other.encoding) &&
208192
lineEndingsPolicy.equals(other.lineEndingsPolicy) &&
209193
steps.equals(other.steps) &&
210194
exceptionPolicy.equals(other.exceptionPolicy);

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ String formatName() {
175175

176176
Formatter buildFormatter() {
177177
return Formatter.builder()
178-
.name(formatName())
179178
.lineEndingsPolicy(getLineEndingsPolicy().get())
180179
.encoding(Charset.forName(encoding))
181180
.steps(steps)

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.LinkedHashMap;
2828
import java.util.List;
2929
import java.util.Map;
30-
import java.util.Map.Entry;
3130
import java.util.Objects;
3231
import java.util.Optional;
3332
import java.util.Set;
@@ -208,7 +207,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
208207
@Parameter(defaultValue = "false")
209208
protected boolean m2eEnableForIncrementalBuild;
210209

211-
protected abstract void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException;
210+
protected abstract void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException;
212211

213212
private static final int MINIMUM_JRE = 11;
214213

@@ -237,11 +236,11 @@ public final void execute() throws MojoExecutionException {
237236
}
238237

239238
try (FormattersHolder formattersHolder = FormattersHolder.create(formatterFactoryToFiles, config);
240-
UpToDateChecker upToDateChecker = createUpToDateChecker(formattersHolder.getFormatters())) {
241-
for (Entry<Formatter, Supplier<Iterable<File>>> entry : formattersHolder.getFormattersWithFiles().entrySet()) {
242-
Formatter formatter = entry.getKey();
243-
Iterable<File> files = entry.getValue().get();
244-
process(files, formatter, upToDateChecker);
239+
UpToDateChecker upToDateChecker = createUpToDateChecker(formattersHolder.openFormatters.values())) {
240+
for (FormatterFactory factory : formattersHolder.openFormatters.keySet()) {
241+
Formatter formatter = formattersHolder.openFormatters.get(factory);
242+
Iterable<File> files = formattersHolder.factoryToFiles.get(factory).get();
243+
process(formattersHolder.nameFor(factory), files, formatter, upToDateChecker);
245244
}
246245
} catch (PluginException e) {
247246
throw e.asMojoExecutionException();

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ public final Formatter newFormatter(Supplier<Iterable<File>> filesToFormat, Form
100100
formatterSteps = List.of(toggle.createFence().preserveWithin(formatterStepsBeforeToggle));
101101
}
102102

103-
String formatterName = this.getClass().getSimpleName();
104103
return Formatter.builder()
105-
.name(formatterName)
106104
.encoding(formatterEncoding)
107105
.lineEndingsPolicy(formatterLineEndingPolicy)
108106
.exceptionPolicy(new FormatExceptionPolicyStrict())

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormattersHolder.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 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.
@@ -16,62 +16,58 @@
1616
package com.diffplug.spotless.maven;
1717

1818
import java.io.File;
19+
import java.util.Collection;
1920
import java.util.LinkedHashMap;
2021
import java.util.Map;
2122
import java.util.Map.Entry;
22-
import java.util.Set;
2323
import java.util.function.Supplier;
2424

2525
import com.diffplug.spotless.Formatter;
2626

2727
class FormattersHolder implements AutoCloseable {
28+
final Map<FormatterFactory, Formatter> openFormatters;
29+
final Map<FormatterFactory, Supplier<Iterable<File>>> factoryToFiles;
2830

29-
private final Map<Formatter, Supplier<Iterable<File>>> formatterToFiles;
31+
FormattersHolder(Map<FormatterFactory, Formatter> openFormatters, Map<FormatterFactory, Supplier<Iterable<File>>> factoryToFiles) {
32+
this.openFormatters = openFormatters;
33+
this.factoryToFiles = factoryToFiles;
34+
}
3035

31-
FormattersHolder(Map<Formatter, Supplier<Iterable<File>>> formatterToFiles) {
32-
this.formatterToFiles = formatterToFiles;
36+
public String nameFor(FormatterFactory factory) {
37+
return factory.getClass().getSimpleName();
3338
}
3439

3540
static FormattersHolder create(Map<FormatterFactory, Supplier<Iterable<File>>> formatterFactoryToFiles, FormatterConfig config) {
36-
Map<Formatter, Supplier<Iterable<File>>> formatterToFiles = new LinkedHashMap<>();
41+
Map<FormatterFactory, Formatter> openFormatters = new LinkedHashMap<>();
3742
try {
3843
for (Entry<FormatterFactory, Supplier<Iterable<File>>> entry : formatterFactoryToFiles.entrySet()) {
3944
FormatterFactory formatterFactory = entry.getKey();
4045
Supplier<Iterable<File>> files = entry.getValue();
41-
4246
Formatter formatter = formatterFactory.newFormatter(files, config);
43-
formatterToFiles.put(formatter, files);
47+
openFormatters.put(formatterFactory, formatter);
4448
}
4549
} catch (RuntimeException openError) {
4650
try {
47-
close(formatterToFiles.keySet());
51+
close(openFormatters.values());
4852
} catch (Exception closeError) {
4953
openError.addSuppressed(closeError);
5054
}
5155
throw openError;
5256
}
5357

54-
return new FormattersHolder(formatterToFiles);
55-
}
56-
57-
Iterable<Formatter> getFormatters() {
58-
return formatterToFiles.keySet();
59-
}
60-
61-
Map<Formatter, Supplier<Iterable<File>>> getFormattersWithFiles() {
62-
return formatterToFiles;
58+
return new FormattersHolder(openFormatters, formatterFactoryToFiles);
6359
}
6460

6561
@Override
6662
public void close() {
6763
try {
68-
close(formatterToFiles.keySet());
64+
close(openFormatters.values());
6965
} catch (Exception e) {
7066
throw new RuntimeException("Unable to close formatters", e);
7167
}
7268
}
7369

74-
private static void close(Set<Formatter> formatters) throws Exception {
70+
private static void close(Collection<Formatter> formatters) throws Exception {
7571
Exception error = null;
7672
for (Formatter formatter : formatters) {
7773
try {

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class SpotlessApplyMojo extends AbstractSpotlessMojo {
4242
private boolean spotlessIdeHookUseStdOut;
4343

4444
@Override
45-
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
45+
protected void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
4646
if (isIdeHook()) {
4747
IdeHook.performHook(files, formatter, spotlessIdeHook, spotlessIdeHookUseStdIn, spotlessIdeHookUseStdOut);
4848
return;
@@ -79,9 +79,9 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
7979
// We print the number of considered files which is useful when ratchetFrom is setup
8080
if (counter.getTotal() > 0) {
8181
getLog().info(String.format("Spotless.%s is keeping %s files clean - %s were changed to be clean, %s were already clean, %s were skipped because caching determined they were already clean",
82-
formatter.getName(), counter.getTotal(), counter.getCleaned(), counter.getCheckedButAlreadyClean(), counter.getSkippedAsCleanCache()));
82+
name, counter.getTotal(), counter.getCleaned(), counter.getCheckedButAlreadyClean(), counter.getSkippedAsCleanCache()));
8383
} else {
84-
getLog().debug(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", formatter.getName()));
84+
getLog().debug(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", name));
8585
}
8686
}
8787

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public int getSeverity() {
6464
private MessageSeverity m2eIncrementalBuildMessageSeverity;
6565

6666
@Override
67-
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
67+
protected void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
6868
ImpactedFilesTracker counter = new ImpactedFilesTracker();
6969

7070
List<File> problemFiles = new ArrayList<>();
@@ -98,9 +98,9 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
9898
// We print the number of considered files which is useful when ratchetFrom is setup
9999
if (counter.getTotal() > 0) {
100100
getLog().info(String.format("Spotless.%s is keeping %s files clean - %s needs changes to be clean, %s were already clean, %s were skipped because caching determined they were already clean",
101-
formatter.getName(), counter.getTotal(), counter.getCleaned(), counter.getCheckedButAlreadyClean(), counter.getSkippedAsCleanCache()));
101+
name, counter.getTotal(), counter.getCleaned(), counter.getCheckedButAlreadyClean(), counter.getSkippedAsCleanCache()));
102102
} else {
103-
getLog().debug(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", formatter.getName()));
103+
getLog().debug(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", name));
104104
}
105105

106106
if (!problemFiles.isEmpty()) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ private StepHarnessWithFile(ResourceHarness harness, Formatter formatter, RoundT
3737
/** Creates a harness for testing steps which do depend on the file. */
3838
public static StepHarnessWithFile forStep(ResourceHarness harness, FormatterStep step) {
3939
return forFormatter(harness, Formatter.builder()
40-
.name(step.getName())
4140
.encoding(StandardCharsets.UTF_8)
4241
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
4342
.steps(Collections.singletonList(step))

0 commit comments

Comments
 (0)