Skip to content

Commit 4dd6db4

Browse files
committed
Make GsonStep round-trippable
1 parent d026161 commit 4dd6db4

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 DiffPlug
2+
* Copyright 2022-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,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.json.gson;
1717

18-
import java.io.IOException;
1918
import java.io.Serializable;
2019
import java.lang.reflect.Constructor;
2120
import java.lang.reflect.InvocationTargetException;
@@ -25,19 +24,32 @@
2524
import com.diffplug.spotless.FormatterStep;
2625
import com.diffplug.spotless.JarState;
2726
import com.diffplug.spotless.Provisioner;
27+
import com.diffplug.spotless.RoundedStep;
2828

29-
public class GsonStep {
29+
public class GsonStep implements RoundedStep {
30+
private static final long serialVersionUID = 1L;
3031
private static final String MAVEN_COORDINATES = "com.google.code.gson:gson";
3132
private static final String INCOMPATIBLE_ERROR_MESSAGE = "There was a problem interacting with Gson; maybe you set an incompatible version?";
33+
public static final String NAME = "gson";
3234

33-
@Deprecated
34-
public static FormatterStep create(int indentSpaces, boolean sortByKeys, boolean escapeHtml, String version, Provisioner provisioner) {
35-
return create(new GsonConfig(sortByKeys, escapeHtml, indentSpaces, version), provisioner);
35+
private final JarState.Promised jarState;
36+
private final GsonConfig gsonConfig;
37+
38+
private GsonStep(JarState.Promised jarState, GsonConfig gsonConfig) {
39+
this.gsonConfig = gsonConfig;
40+
this.jarState = jarState;
3641
}
3742

3843
public static FormatterStep create(GsonConfig gsonConfig, Provisioner provisioner) {
3944
Objects.requireNonNull(provisioner, "provisioner cannot be null");
40-
return FormatterStep.createLazy("gson", () -> new State(gsonConfig, provisioner), State::toFormatter);
45+
return FormatterStep.create(NAME,
46+
new GsonStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATES + ":" + gsonConfig.getVersion(), provisioner)), gsonConfig),
47+
GsonStep::equalityState,
48+
State::toFormatter);
49+
}
50+
51+
private State equalityState() {
52+
return new State(jarState.get(), gsonConfig);
4153
}
4254

4355
private static final class State implements Serializable {
@@ -46,9 +58,9 @@ private static final class State implements Serializable {
4658
private final JarState jarState;
4759
private final GsonConfig gsonConfig;
4860

49-
private State(GsonConfig gsonConfig, Provisioner provisioner) throws IOException {
61+
private State(JarState jarState, GsonConfig gsonConfig) {
62+
this.jarState = jarState;
5063
this.gsonConfig = gsonConfig;
51-
this.jarState = JarState.from(MAVEN_COORDINATES + ":" + gsonConfig.getVersion(), provisioner);
5264
}
5365

5466
FormatterFunc toFormatter() {
@@ -62,5 +74,4 @@ FormatterFunc toFormatter() {
6274
}
6375
}
6476
}
65-
6677
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ protected StepHarnessBase(Formatter formatter) {
5151
supportsRoundTrip = true;
5252
} else if (Set.of("black", "buf", "clang", "ktlint", "ktfmt", "scalafmt", "palantir-java-format", "google-java-format",
5353
"removeUnusedImports", "cleanthat", "No line break between type annotation and type",
54+
"gson",
5455
"importOrder", "Remove unnecessary semicolons").contains(onlyStepName)) {
5556
supportsRoundTrip = true;
5657
}

0 commit comments

Comments
 (0)