diff --git a/CHANGES.md b/CHANGES.md index 07b81a8fc5..32c7a7f5a5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * The default list of type annotations used by `formatAnnotations` has had 8 more annotations from the Checker Framework added [#1494](https://github.com/diffplug/spotless/pull/1494) ### Changes * Rename `YamlJacksonStep` into `JacksonYamlStep` while normalizing Jackson usage ([#1492](https://github.com/diffplug/spotless/pull/1492)) +* Convert `gson` integration to use a compile-only source set ([#1510](https://github.com/diffplug/spotless/pull/1510)). ## [2.32.0] - 2023-01-13 ### Added diff --git a/lib/build.gradle b/lib/build.gradle index 30c8fe8b15..bb1a389993 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -15,7 +15,8 @@ def NEEDS_GLUE = [ 'flexmark', 'diktat', 'scalafmt', - 'jackson' + 'jackson', + 'gson' ] for (glue in NEEDS_GLUE) { sourceSets.register(glue) { @@ -108,6 +109,8 @@ dependencies { // used for markdown formatting flexmarkCompileOnly 'com.vladsch.flexmark:flexmark-all:0.62.2' + + gsonCompileOnly 'com.google.code.gson:gson:2.10.1' } // we'll hold the core lib to a high standard diff --git a/lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java b/lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java new file mode 100644 index 0000000000..b19476a1a8 --- /dev/null +++ b/lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java @@ -0,0 +1,92 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.glue.gson; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Collections; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.stream.JsonWriter; + +import com.diffplug.spotless.FormatterFunc; +import com.diffplug.spotless.ThrowingEx; +import com.diffplug.spotless.json.gson.GsonConfig; + +public class GsonFormatterFunc implements FormatterFunc { + + private static final String FAILED_TO_PARSE_ERROR_MESSAGE = "Unable to format JSON"; + + private final Gson gson; + private final GsonConfig gsonConfig; + private final String generatedIndent; + + public GsonFormatterFunc(GsonConfig gsonConfig) { + GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls(); + if (!gsonConfig.isEscapeHtml()) { + gsonBuilder = gsonBuilder.disableHtmlEscaping(); + } + this.gson = gsonBuilder.create(); + this.gsonConfig = gsonConfig; + this.generatedIndent = generateIndent(gsonConfig.getIndentSpaces()); + } + + @Override + public String apply(String inputString) { + String result; + if (inputString.isEmpty()) { + result = ""; + } else { + JsonElement jsonElement = gson.fromJson(inputString, JsonElement.class); + if (jsonElement == null) { + throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE); + } + if (gsonConfig.isSortByKeys() && jsonElement.isJsonObject()) { + jsonElement = sortByKeys(jsonElement.getAsJsonObject()); + } + try (StringWriter stringWriter = new StringWriter()) { + JsonWriter jsonWriter = new JsonWriter(stringWriter); + jsonWriter.setIndent(this.generatedIndent); + gson.toJson(jsonElement, jsonWriter); + result = stringWriter + "\n"; + } catch (IOException ioException) { + throw ThrowingEx.asRuntime(ioException); + } + } + return result; + } + + private JsonElement sortByKeys(JsonObject jsonObject) { + JsonObject result = new JsonObject(); + jsonObject.keySet().stream().sorted() + .forEach(key -> { + JsonElement element = jsonObject.get(key); + if (element.isJsonObject()) { + element = sortByKeys(element.getAsJsonObject()); + } + result.add(key, element); + }); + return result; + } + + private String generateIndent(int indentSpaces) { + return String.join("", Collections.nCopies(indentSpaces, " ")); + } + +} diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonBuilderWrapper.java b/lib/src/main/java/com/diffplug/spotless/json/gson/GsonBuilderWrapper.java deleted file mode 100644 index c2e56f39b8..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonBuilderWrapper.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2022 DiffPlug - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.diffplug.spotless.json.gson; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; - -import com.diffplug.spotless.JarState; - -class GsonBuilderWrapper extends GsonWrapperBase { - - private final Constructor constructor; - private final Method serializeNullsMethod; - private final Method disableHtmlEscapingMethod; - private final Method createMethod; - - GsonBuilderWrapper(JarState jarState) { - Class clazz = loadClass(jarState.getClassLoader(), "com.google.gson.GsonBuilder"); - this.constructor = getConstructor(clazz); - this.serializeNullsMethod = getMethod(clazz, "serializeNulls"); - this.disableHtmlEscapingMethod = getMethod(clazz, "disableHtmlEscaping"); - this.createMethod = getMethod(clazz, "create"); - } - - Object createGsonBuilder() { - return newInstance(constructor); - } - - Object serializeNulls(Object gsonBuilder) { - return invoke(serializeNullsMethod, gsonBuilder); - } - - Object disableHtmlEscaping(Object gsonBuilder) { - return invoke(disableHtmlEscapingMethod, gsonBuilder); - } - - Object create(Object gsonBuilder) { - return invoke(createMethod, gsonBuilder); - } - -} diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonConfig.java b/lib/src/main/java/com/diffplug/spotless/json/gson/GsonConfig.java new file mode 100644 index 0000000000..a11c1ee296 --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/json/gson/GsonConfig.java @@ -0,0 +1,66 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.json.gson; + +import java.io.Serializable; + +public class GsonConfig implements Serializable { + private static final long serialVersionUID = 6039715618937332633L; + + private boolean sortByKeys; + private boolean escapeHtml; + private int indentSpaces; + private String version; + + public GsonConfig(boolean sortByKeys, boolean escapeHtml, int indentSpaces, String version) { + this.sortByKeys = sortByKeys; + this.escapeHtml = escapeHtml; + this.indentSpaces = indentSpaces; + this.version = version; + } + + public boolean isSortByKeys() { + return sortByKeys; + } + + public void setSortByKeys(boolean sortByKeys) { + this.sortByKeys = sortByKeys; + } + + public boolean isEscapeHtml() { + return escapeHtml; + } + + public void setEscapeHtml(boolean escapeHtml) { + this.escapeHtml = escapeHtml; + } + + public int getIndentSpaces() { + return indentSpaces; + } + + public void setIndentSpaces(int indentSpaces) { + this.indentSpaces = indentSpaces; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } +} diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.java b/lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.java index 06519ae39d..ec90255b77 100644 --- a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.java +++ b/lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,8 @@ import java.io.IOException; import java.io.Serializable; -import java.io.StringWriter; -import java.util.Collections; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.Objects; import com.diffplug.spotless.FormatterFunc; @@ -28,78 +28,38 @@ public class GsonStep { private static final String MAVEN_COORDINATES = "com.google.code.gson:gson"; + private static final String INCOMPATIBLE_ERROR_MESSAGE = "There was a problem interacting with Gson; maybe you set an incompatible version?"; + @Deprecated public static FormatterStep create(int indentSpaces, boolean sortByKeys, boolean escapeHtml, String version, Provisioner provisioner) { + return create(new GsonConfig(sortByKeys, escapeHtml, indentSpaces, version), provisioner); + } + + public static FormatterStep create(GsonConfig gsonConfig, Provisioner provisioner) { Objects.requireNonNull(provisioner, "provisioner cannot be null"); - return FormatterStep.createLazy("gson", () -> new State(indentSpaces, sortByKeys, escapeHtml, version, provisioner), State::toFormatter); + return FormatterStep.createLazy("gson", () -> new State(gsonConfig, provisioner), State::toFormatter); } private static final class State implements Serializable { - private static final long serialVersionUID = -1493479043249379485L; + private static final long serialVersionUID = -3240568265160440420L; - private final int indentSpaces; - private final boolean sortByKeys; - private final boolean escapeHtml; private final JarState jarState; + private final GsonConfig gsonConfig; - private State(int indentSpaces, boolean sortByKeys, boolean escapeHtml, String version, Provisioner provisioner) throws IOException { - this.indentSpaces = indentSpaces; - this.sortByKeys = sortByKeys; - this.escapeHtml = escapeHtml; - this.jarState = JarState.from(MAVEN_COORDINATES + ":" + version, provisioner); + private State(GsonConfig gsonConfig, Provisioner provisioner) throws IOException { + this.gsonConfig = gsonConfig; + this.jarState = JarState.from(MAVEN_COORDINATES + ":" + gsonConfig.getVersion(), provisioner); } FormatterFunc toFormatter() { - JsonWriterWrapper jsonWriterWrapper = new JsonWriterWrapper(jarState); - JsonElementWrapper jsonElementWrapper = new JsonElementWrapper(jarState); - JsonObjectWrapper jsonObjectWrapper = new JsonObjectWrapper(jarState, jsonElementWrapper); - GsonBuilderWrapper gsonBuilderWrapper = new GsonBuilderWrapper(jarState); - GsonWrapper gsonWrapper = new GsonWrapper(jarState, jsonElementWrapper, jsonWriterWrapper); - - Object gsonBuilder = gsonBuilderWrapper.serializeNulls(gsonBuilderWrapper.createGsonBuilder()); - if (!escapeHtml) { - gsonBuilder = gsonBuilderWrapper.disableHtmlEscaping(gsonBuilder); + try { + Class formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.gson.GsonFormatterFunc"); + Constructor constructor = formatterFunc.getConstructor(GsonConfig.class); + return (FormatterFunc) constructor.newInstance(gsonConfig); + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException + | InstantiationException | IllegalAccessException | NoClassDefFoundError cause) { + throw new IllegalStateException(INCOMPATIBLE_ERROR_MESSAGE, cause); } - Object gson = gsonBuilderWrapper.create(gsonBuilder); - - return inputString -> { - String result; - if (inputString.isEmpty()) { - result = ""; - } else { - Object jsonElement = gsonWrapper.fromJson(gson, inputString, jsonElementWrapper.getWrappedClass()); - if (jsonElement == null) { - throw new AssertionError(GsonWrapperBase.FAILED_TO_PARSE_ERROR_MESSAGE); - } - if (sortByKeys && jsonElementWrapper.isJsonObject(jsonElement)) { - jsonElement = sortByKeys(jsonObjectWrapper, jsonElementWrapper, jsonElement); - } - try (StringWriter stringWriter = new StringWriter()) { - Object jsonWriter = jsonWriterWrapper.createJsonWriter(stringWriter); - jsonWriterWrapper.setIndent(jsonWriter, generateIndent(indentSpaces)); - gsonWrapper.toJson(gson, jsonElement, jsonWriter); - result = stringWriter + "\n"; - } - } - return result; - }; - } - - private Object sortByKeys(JsonObjectWrapper jsonObjectWrapper, JsonElementWrapper jsonElementWrapper, Object jsonObject) { - Object result = jsonObjectWrapper.createJsonObject(); - jsonObjectWrapper.keySet(jsonObject).stream().sorted() - .forEach(key -> { - Object element = jsonObjectWrapper.get(jsonObject, key); - if (jsonElementWrapper.isJsonObject(element)) { - element = sortByKeys(jsonObjectWrapper, jsonElementWrapper, element); - } - jsonObjectWrapper.add(result, key, element); - }); - return result; - } - - private String generateIndent(int indentSpaces) { - return String.join("", Collections.nCopies(indentSpaces, " ")); } } diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapper.java b/lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapper.java deleted file mode 100644 index eaca499eed..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapper.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2022 DiffPlug - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.diffplug.spotless.json.gson; - -import java.lang.reflect.Method; - -import com.diffplug.spotless.JarState; - -class GsonWrapper extends GsonWrapperBase { - - private final Method fromJsonMethod; - private final Method toJsonMethod; - - GsonWrapper(JarState jarState, JsonElementWrapper jsonElementWrapper, JsonWriterWrapper jsonWriterWrapper) { - Class clazz = loadClass(jarState.getClassLoader(), "com.google.gson.Gson"); - this.fromJsonMethod = getMethod(clazz, "fromJson", String.class, Class.class); - this.toJsonMethod = getMethod(clazz, "toJson", jsonElementWrapper.getWrappedClass(), jsonWriterWrapper.getWrappedClass()); - } - - Object fromJson(Object gson, String json, Class type) { - return invoke(fromJsonMethod, gson, json, type); - } - - void toJson(Object gson, Object jsonElement, Object jsonWriter) { - invoke(toJsonMethod, gson, jsonElement, jsonWriter); - } - -} diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapperBase.java b/lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapperBase.java deleted file mode 100644 index 24d12a5722..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapperBase.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2022 DiffPlug - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.diffplug.spotless.json.gson; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -abstract class GsonWrapperBase { - - static final String INCOMPATIBLE_ERROR_MESSAGE = "There was a problem interacting with Gson; maybe you set an incompatible version?"; - static final String FAILED_TO_PARSE_ERROR_MESSAGE = "Unable to format JSON"; - - protected final Class loadClass(ClassLoader classLoader, String className) { - try { - return classLoader.loadClass(className); - } catch (ClassNotFoundException cause) { - throw new IllegalStateException(INCOMPATIBLE_ERROR_MESSAGE, cause); - } - } - - protected final Constructor getConstructor(Class clazz, Class... argumentTypes) { - try { - return clazz.getConstructor(argumentTypes); - } catch (NoSuchMethodException cause) { - throw new IllegalStateException(INCOMPATIBLE_ERROR_MESSAGE, cause); - } - } - - protected final Method getMethod(Class clazz, String name, Class... argumentTypes) { - try { - return clazz.getMethod(name, argumentTypes); - } catch (NoSuchMethodException cause) { - throw new IllegalStateException(INCOMPATIBLE_ERROR_MESSAGE, cause); - } - } - - protected final T newInstance(Constructor constructor, Object... args) { - try { - return constructor.newInstance(args); - } catch (InstantiationException | IllegalAccessException cause) { - throw new IllegalStateException(INCOMPATIBLE_ERROR_MESSAGE, cause); - } catch (InvocationTargetException cause) { - throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE, cause.getCause()); - } - } - - protected Object invoke(Method method, Object targetObject, Object... args) { - try { - return method.invoke(targetObject, args); - } catch (IllegalAccessException cause) { - throw new IllegalStateException(INCOMPATIBLE_ERROR_MESSAGE, cause); - } catch (InvocationTargetException cause) { - throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE, cause.getCause()); - } - } - -} diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/JsonElementWrapper.java b/lib/src/main/java/com/diffplug/spotless/json/gson/JsonElementWrapper.java deleted file mode 100644 index ffdfa649ce..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/json/gson/JsonElementWrapper.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2022 DiffPlug - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.diffplug.spotless.json.gson; - -import java.lang.reflect.Method; - -import com.diffplug.spotless.JarState; - -class JsonElementWrapper extends GsonWrapperBase { - - private final Class clazz; - private final Method isJsonObjectMethod; - - JsonElementWrapper(JarState jarState) { - this.clazz = loadClass(jarState.getClassLoader(), "com.google.gson.JsonElement"); - this.isJsonObjectMethod = getMethod(clazz, "isJsonObject"); - } - - boolean isJsonObject(Object jsonElement) { - return (boolean) invoke(isJsonObjectMethod, jsonElement); - } - - Class getWrappedClass() { - return clazz; - } - -} diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/JsonObjectWrapper.java b/lib/src/main/java/com/diffplug/spotless/json/gson/JsonObjectWrapper.java deleted file mode 100644 index 35ec0d876b..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/json/gson/JsonObjectWrapper.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2022 DiffPlug - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.diffplug.spotless.json.gson; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.Set; - -import com.diffplug.spotless.JarState; - -class JsonObjectWrapper extends GsonWrapperBase { - - private final Constructor constructor; - private final Method keySetMethod; - private final Method getMethod; - private final Method addMethod; - - JsonObjectWrapper(JarState jarState, JsonElementWrapper jsonElementWrapper) { - Class clazz = loadClass(jarState.getClassLoader(), "com.google.gson.JsonObject"); - this.constructor = getConstructor(clazz); - this.keySetMethod = getMethod(clazz, "keySet"); - this.getMethod = getMethod(clazz, "get", String.class); - this.addMethod = getMethod(clazz, "add", String.class, jsonElementWrapper.getWrappedClass()); - } - - Object createJsonObject() { - return newInstance(constructor); - } - - @SuppressWarnings("unchecked") - Set keySet(Object jsonObject) { - return (Set) invoke(keySetMethod, jsonObject); - } - - Object get(Object jsonObject, String key) { - return invoke(getMethod, jsonObject, key); - } - - void add(Object jsonObject, String key, Object element) { - invoke(addMethod, jsonObject, key, element); - } - -} diff --git a/lib/src/main/java/com/diffplug/spotless/json/gson/JsonWriterWrapper.java b/lib/src/main/java/com/diffplug/spotless/json/gson/JsonWriterWrapper.java deleted file mode 100644 index c9d682e2c2..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/json/gson/JsonWriterWrapper.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2022 DiffPlug - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.diffplug.spotless.json.gson; - -import java.io.Writer; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; - -import com.diffplug.spotless.JarState; - -class JsonWriterWrapper extends GsonWrapperBase { - - private final Class clazz; - private final Constructor constructor; - private final Method setIndentMethod; - - JsonWriterWrapper(JarState jarState) { - this.clazz = loadClass(jarState.getClassLoader(), "com.google.gson.stream.JsonWriter"); - this.constructor = getConstructor(clazz, Writer.class); - this.setIndentMethod = getMethod(clazz, "setIndent", String.class); - } - - Object createJsonWriter(Writer writer) { - return newInstance(constructor, writer); - } - - void setIndent(Object jsonWriter, String indent) { - invoke(setIndentMethod, jsonWriter, indent); - } - - Class getWrappedClass() { - return clazz; - } - -} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java index 1e9de2e831..39b158ce1e 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java @@ -27,7 +27,7 @@ public class JsonExtension extends FormatExtension { private static final int DEFAULT_INDENTATION = 4; - private static final String DEFAULT_GSON_VERSION = "2.8.9"; + private static final String DEFAULT_GSON_VERSION = "2.10.1"; static final String NAME = "json"; @Inject @@ -112,7 +112,7 @@ public GsonConfig version(String version) { } private FormatterStep createStep() { - return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, provisioner()); + return GsonStep.create(new com.diffplug.spotless.json.gson.GsonConfig(sortByKeys, escapeHtml, indentSpaces, version), provisioner()); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java index 7962ecb1f7..fb5a38e890 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java @@ -18,12 +18,13 @@ import org.apache.maven.plugins.annotations.Parameter; import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.json.gson.GsonConfig; import com.diffplug.spotless.json.gson.GsonStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; public class Gson implements FormatterStepFactory { - private static final String DEFAULT_GSON_VERSION = "2.8.9"; + private static final String DEFAULT_GSON_VERSION = "2.10.1"; @Parameter int indentSpaces = Json.DEFAULT_INDENTATION; @@ -40,6 +41,6 @@ public class Gson implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { int indentSpaces = this.indentSpaces; - return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, stepConfig.getProvisioner()); + return GsonStep.create(new GsonConfig(sortByKeys, escapeHtml, indentSpaces, version), stepConfig.getProvisioner()); } } diff --git a/testlib/src/test/java/com/diffplug/spotless/json/gson/GsonStepTest.java b/testlib/src/test/java/com/diffplug/spotless/json/gson/GsonStepTest.java index 8f1d758836..3f4d2a84e0 100644 --- a/testlib/src/test/java/com/diffplug/spotless/json/gson/GsonStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/json/gson/GsonStepTest.java @@ -28,7 +28,7 @@ public class GsonStepTest extends JsonFormatterStepCommonTests { - private static final String DEFAULT_VERSION = "2.8.9"; + private static final String DEFAULT_VERSION = "2.10.1"; @Test void handlesComplexNestedObject() { @@ -52,34 +52,34 @@ void handlesNotJson() { @Test void handlesSortingWhenSortByKeyEnabled() { - FormatterStep step = GsonStep.create(INDENT, true, false, DEFAULT_VERSION, TestProvisioner.mavenCentral()); + FormatterStep step = GsonStep.create(new GsonConfig(true, false, INDENT, DEFAULT_VERSION), TestProvisioner.mavenCentral()); StepHarness.forStep(step).testResource("json/sortByKeysBefore.json", "json/sortByKeysAfter.json"); } @Test void doesNoSortingWhenSortByKeyDisabled() { - FormatterStep step = GsonStep.create(INDENT, false, false, DEFAULT_VERSION, TestProvisioner.mavenCentral()); + FormatterStep step = GsonStep.create(new GsonConfig(false, false, INDENT, DEFAULT_VERSION), TestProvisioner.mavenCentral()); StepHarness.forStep(step) .testResource("json/sortByKeysBefore.json", "json/sortByKeysAfterDisabled.json"); } @Test void handlesHtmlEscapeWhenEnabled() { - FormatterStep step = GsonStep.create(INDENT, false, true, DEFAULT_VERSION, TestProvisioner.mavenCentral()); + FormatterStep step = GsonStep.create(new GsonConfig(false, true, INDENT, DEFAULT_VERSION), TestProvisioner.mavenCentral()); StepHarness.forStep(step) .testResource("json/escapeHtmlGsonBefore.json", "json/escapeHtmlGsonAfter.json"); } @Test void writesRawHtmlWhenHtmlEscapeDisabled() { - FormatterStep step = GsonStep.create(INDENT, false, false, DEFAULT_VERSION, TestProvisioner.mavenCentral()); + FormatterStep step = GsonStep.create(new GsonConfig(false, false, INDENT, DEFAULT_VERSION), TestProvisioner.mavenCentral()); StepHarness.forStep(step) .testResource("json/escapeHtmlGsonBefore.json", "json/escapeHtmlGsonAfterDisabled.json"); } @Test void handlesVersionIncompatibility() { - FormatterStep step = GsonStep.create(INDENT, false, false, "1.7", TestProvisioner.mavenCentral()); + FormatterStep step = GsonStep.create(new GsonConfig(false, false, INDENT, "1.7"), TestProvisioner.mavenCentral()); Assertions.assertThatThrownBy(() -> step.format("", new File(""))) .isInstanceOf(IllegalStateException.class) .hasMessage("There was a problem interacting with Gson; maybe you set an incompatible version?"); @@ -87,6 +87,6 @@ void handlesVersionIncompatibility() { @Override protected FormatterStep createFormatterStep(int indent, Provisioner provisioner) { - return GsonStep.create(indent, false, false, DEFAULT_VERSION, provisioner); + return GsonStep.create(new GsonConfig(false, false, indent, DEFAULT_VERSION), provisioner); } }