Skip to content

Commit bf88e70

Browse files
authored
Explain why we need to hash the palantir-java-format jar as part of SpotlessTask's state. (#1327)
Explain why we need to hash the palantir-java-format jar as part of SpotlessTask's state
1 parent 92132b6 commit bf88e70

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/spotless/NativePalantirJavaFormatStep.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static class State implements Serializable {
4646
private static final long serialVersionUID = 1L;
4747

4848
// Kept for state serialization purposes.
49+
// For more details, see PalantirJavaFormatStep.State#jarSignature
4950
@SuppressWarnings({"unused", "FieldCanBeLocal"})
5051
private FileSignature execSignature;
5152

gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/spotless/PalantirJavaFormatStep.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,18 @@ static final class State implements Serializable {
4949
@SuppressWarnings("unused")
5050
private final String stepName = NAME;
5151

52-
// Kept for state serialization purposes.
52+
// Spotless' `FormatterStepImpl` implements Java's `Serializable` interface in a weird way:
53+
// It serializes this `State` class[1].
54+
//
55+
// Gradle understands Java's `Serializable`, and uses it to invalidate `@Input`s to tasks.
56+
//
57+
// Since FormatterStepImpl is an input to `SpotlessTask`[2], anything serialized as part of this `State`
58+
// is used for up-to-date checking for the `SpotlessTask`
59+
//
60+
// [1]
61+
// https://github.com/diffplug/spotless/blob/52654ef8c4a6191d983b10a2370d53b1ca023f7d/lib/src/main/java/com/diffplug/spotless/LazyForwardingEquality.java#L68
62+
// [2]
63+
// https://github.com/diffplug/spotless/blob/f32701212bf8d327c67d10c35316cb80dcdf577b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java#L163
5364
@SuppressWarnings({"unused", "FieldCanBeLocal"})
5465
private FileSignature jarsSignature;
5566

@@ -75,7 +86,11 @@ FormatterFunc createFormat() {
7586
try {
7687
// Only resolve the jars and compute the signature at execution time!
7788
Iterable<File> jars = jarsSupplier.get();
89+
90+
// Not a performance issue, as Spotless caches this
91+
// https://github.com/diffplug/spotless/blob/228eb10af382b19e130d8d9479f7a95238cb4358/lib/src/main/java/com/diffplug/spotless/FileSignature.java#L138-L143
7892
this.jarsSignature = FileSignature.signAsSet(jars);
93+
7994
return memoizedFormatter.get().formatSourceReflowStringsAndFixImports(input);
8095
} catch (IOException e) {
8196
throw new RuntimeException(e);

gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatSpotlessPluginTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class PalantirJavaFormatSpotlessPluginTest extends IntegrationTestKitSpec {
5858
classpath 'com.palantir.baseline:gradle-baseline-java:6.21.0'
5959
classpath 'com.palantir.gradle.jdks:gradle-jdks:0.62.0'
6060
classpath 'com.palantir.gradle.jdkslatest:gradle-jdks-latest:0.17.0'
61+
62+
constraints {
63+
classpath 'com.diffplug.spotless:6.22.0'
64+
}
6165
}
6266
}
6367

0 commit comments

Comments
 (0)