From ecca2b8b413a2b7f360991d3cb8f269f47ba2bce Mon Sep 17 00:00:00 2001 From: Thomas Broyer Date: Sat, 4 Sep 2021 20:35:34 +0200 Subject: [PATCH 1/4] Fix google-java-format test when run with JDK 8 --- .../com/diffplug/spotless/java/GoogleJavaFormatStepTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java index 4c77801d9d..b4d893c888 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java @@ -36,7 +36,7 @@ public void suggestJre11() throws Exception { step.testResourceException("java/googlejavaformat/TextBlock.dirty", throwable -> { throwable.hasMessageStartingWith("You are running Spotless on JRE 8") .hasMessageEndingWith(", which limits you to google-java-format 1.7\n" - + "If you upgrade your build JVM to 11+, then you can use google-java-format 1.9, which may have fixed this problem."); + + "If you upgrade your build JVM to 11+, then you can use google-java-format 1.11.0, which may have fixed this problem."); }); } else if (JreVersion.thisVm() < 13) { step.testResourceException("java/googlejavaformat/TextBlock.dirty", throwable -> { From d79b5f31a00b2f85fbbec19c65250f0a890e0168 Mon Sep 17 00:00:00 2001 From: Thomas Broyer Date: Sat, 4 Sep 2021 20:22:24 +0200 Subject: [PATCH 2/4] Support google-java-format's skip-reflowing-long-strings option Add configuration option to switch the option of the googleJavaFormat step. Default to not reflowing for backwards compatibility. Fixes #924 --- .../spotless/java/GoogleJavaFormatStep.java | 41 ++++++++++++++++++- plugin-gradle/README.md | 6 +-- .../gradle/spotless/JavaExtension.java | 21 ++++++++-- plugin-maven/README.md | 8 ++-- .../spotless/maven/java/GoogleJavaFormat.java | 8 +++- .../googlejavaformat/JavaCodeFormatted.test | 3 +- .../googlejavaformat/JavaCodeFormatted18.test | 3 +- .../JavaCodeFormattedAOSP.test | 3 +- .../JavaCodeFormattedReflowLongStrings.test | 12 ++++++ .../googlejavaformat/JavaCodeUnformatted.test | 2 +- .../JavaCodeWithLicenseFormatted.test | 3 +- .../JavaCodeWithLicenseFormattedAOSP.test | 3 +- ...WithLicenseFormattedReflowLongStrings.test | 17 ++++++++ .../JavaCodeWithLicensePackageFormatted.test | 3 +- ...vaCodeWithLicensePackageFormattedAOSP.test | 3 +- ...ensePackageFormattedReflowLongStrings.test | 14 +++++++ ...JavaCodeWithLicensePackageUnformatted.test | 2 +- .../JavaCodeWithLicenseUnformatted.test | 2 +- .../JavaCodeWithPackageFormatted.test | 3 +- .../JavaCodeWithPackageFormattedAOSP.test | 3 +- ...WithPackageFormattedReflowLongStrings.test | 14 +++++++ .../JavaCodeWithPackageUnformatted.test | 2 +- .../java/GoogleJavaFormatStepTest.java | 26 +++++++++++- 23 files changed, 173 insertions(+), 29 deletions(-) create mode 100644 testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test create mode 100644 testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedReflowLongStrings.test create mode 100644 testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedReflowLongStrings.test create mode 100644 testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedReflowLongStrings.test diff --git a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java index fe5fa18358..dfa841695e 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java @@ -27,6 +27,7 @@ import com.diffplug.spotless.JarState; import com.diffplug.spotless.LineEnding; import com.diffplug.spotless.Provisioner; +import com.diffplug.spotless.ThrowingEx.BiFunction; import com.diffplug.spotless.ThrowingEx.Function; /** Wraps up google-java-format as a FormatterStep. */ @@ -35,6 +36,7 @@ public class GoogleJavaFormatStep { private GoogleJavaFormatStep() {} private static final String DEFAULT_STYLE = "GOOGLE"; + private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false; static final String NAME = "google-java-format"; static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format:"; static final String FORMATTER_CLASS = "com.google.googlejavaformat.java.Formatter"; @@ -56,6 +58,9 @@ private GoogleJavaFormatStep() {} private static final String IMPORT_ORDERER_CLASS = "com.google.googlejavaformat.java.ImportOrderer"; private static final String IMPORT_ORDERER_METHOD = "reorderImports"; + private static final String STRING_WRAPPER_CLASS = "com.google.googlejavaformat.java.StringWrapper"; + private static final String STRING_WRAPPER_METHOD = "wrap"; + /** Creates a step which formats everything - code, import order, and unused imports. */ public static FormatterStep create(Provisioner provisioner) { return create(defaultVersion(), provisioner); @@ -68,11 +73,16 @@ public static FormatterStep create(String version, Provisioner provisioner) { /** Creates a step which formats everything - code, import order, and unused imports. */ public static FormatterStep create(String version, String style, Provisioner provisioner) { + return create(version, style, provisioner, DEFAULT_REFLOW_LONG_STRINGS); + } + + /** Creates a step which formats everything - code, import order, and unused imports - and optionally reflows long strings. */ + public static FormatterStep create(String version, String style, Provisioner provisioner, boolean reflowLongStrings) { Objects.requireNonNull(version, "version"); Objects.requireNonNull(style, "style"); Objects.requireNonNull(provisioner, "provisioner"); return FormatterStep.createLazy(NAME, - () -> new State(NAME, version, style, provisioner), + () -> new State(NAME, version, style, provisioner, reflowLongStrings), State::createFormat); } @@ -106,6 +116,10 @@ public static String defaultStyle() { return DEFAULT_STYLE; } + public static boolean defaultReflowLongStrings() { + return DEFAULT_REFLOW_LONG_STRINGS; + } + static final class State implements Serializable { private static final long serialVersionUID = 1L; @@ -114,16 +128,22 @@ static final class State implements Serializable { final String stepName; final String version; final String style; + final boolean reflowLongStrings; State(String stepName, String version, Provisioner provisioner) throws IOException { this(stepName, version, DEFAULT_STYLE, provisioner); } State(String stepName, String version, String style, Provisioner provisioner) throws IOException { + this(stepName, version, style, provisioner, DEFAULT_REFLOW_LONG_STRINGS); + } + + State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws IOException { this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner); this.stepName = stepName; this.version = version; this.style = style; + this.reflowLongStrings = reflowLongStrings; } @SuppressWarnings({"unchecked", "rawtypes"}) @@ -153,11 +173,14 @@ FormatterFunc createFormat() throws Exception { Class importOrdererClass = classLoader.loadClass(IMPORT_ORDERER_CLASS); Method importOrdererMethod = importOrdererClass.getMethod(IMPORT_ORDERER_METHOD, String.class); + BiFunction reflowLongStrings = this.reflowLongStrings ? constructReflowLongStringsFunction(classLoader, formatterClazz) : (s, f) -> s; + return suggestJre11(input -> { String formatted = (String) formatterMethod.invoke(formatter, input); String removedUnused = removeUnused.apply(formatted); String sortedImports = (String) importOrdererMethod.invoke(null, removedUnused); - return fixWindowsBug(sortedImports, version); + String reflowedLongStrings = reflowLongStrings.apply(sortedImports, formatter); + return fixWindowsBug(reflowedLongStrings, version); }); } @@ -191,6 +214,20 @@ private static Function constructRemoveUnusedFunction(ClassLoade } return removeUnused; } + + private static BiFunction constructReflowLongStringsFunction(ClassLoader classLoader, Class formatterClazz) throws NoSuchMethodException { + Class stringWrapperClass; + try { + stringWrapperClass = classLoader.loadClass(STRING_WRAPPER_CLASS); + } catch (ClassNotFoundException e) { + // google-java-format 1.7 or lower, which happen to be LATEST_VERSION_JRE_8, so rely on suggestJre11 for the error + return (s, f) -> { + throw e; + }; + } + Method stringWrapperMethod = stringWrapperClass.getMethod(STRING_WRAPPER_METHOD, String.class, formatterClazz); + return (s, f) -> (String) stringWrapperMethod.invoke(null, s, f); + } } private static final boolean IS_WINDOWS = LineEnding.PLATFORM_NATIVE.str().equals("\r\n"); diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index b47a3f6dd1..5e2fea412d 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -112,7 +112,7 @@ spotless { // don't need to set target, it is inferred from java // apply a specific flavor of google-java-format - googleJavaFormat('1.8').aosp() + googleJavaFormat('1.8').aosp().reflowLongStrings() // make sure every file has the following copyright header. // optionally, Spotless can set copyright years by digging // through git history (see "license" section below) @@ -176,9 +176,9 @@ spotless { spotless { java { googleJavaFormat() - // optional: you can specify a specific version and/or switch to AOSP style + // optional: you can specify a specific version and/or switch to AOSP style and/or reflow long strings (requires at least 1.8) // - googleJavaFormat('1.7').aosp() + googleJavaFormat('1.8').aosp().reflowLongStrings() ``` ### eclipse jdt diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index 7924435b13..92e88a234b 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -90,6 +90,7 @@ public GoogleJavaFormatConfig googleJavaFormat(String version) { public class GoogleJavaFormatConfig { final String version; String style; + boolean reflowLongStrings; GoogleJavaFormatConfig(String version) { this.version = Objects.requireNonNull(version); @@ -97,19 +98,31 @@ public class GoogleJavaFormatConfig { addStep(createStep()); } - public void style(String style) { + public GoogleJavaFormatConfig style(String style) { this.style = Objects.requireNonNull(style); replaceStep(createStep()); + return this; } - public void aosp() { - style("AOSP"); + public GoogleJavaFormatConfig aosp() { + return style("AOSP"); + } + + public GoogleJavaFormatConfig reflowLongStrings() { + return reflowLongStrings(true); + } + + public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) { + this.reflowLongStrings = reflowLongStrings; + replaceStep(createStep()); + return this; } private FormatterStep createStep() { return GoogleJavaFormatStep.create(version, style, - provisioner()); + provisioner(), + reflowLongStrings); } } diff --git a/plugin-maven/README.md b/plugin-maven/README.md index fe854ce072..be32bb37e9 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -105,10 +105,11 @@ To use it in your pom, just [add the Spotless dependency](https://search.maven.o - + 1.8 + true - + 1.8 + + true ``` diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java index 3247b22b49..95c31ea6ca 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,10 +29,14 @@ public class GoogleJavaFormat implements FormatterStepFactory { @Parameter private String style; + @Parameter + private Boolean reflowLongStrings; + @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { String version = this.version != null ? this.version : GoogleJavaFormatStep.defaultVersion(); String style = this.style != null ? this.style : GoogleJavaFormatStep.defaultStyle(); - return GoogleJavaFormatStep.create(version, style, config.getProvisioner()); + boolean reflowLongStrings = this.reflowLongStrings != null ? this.reflowLongStrings : GoogleJavaFormatStep.defaultReflowLongStrings(); + return GoogleJavaFormatStep.create(version, style, config.getProvisioner(), reflowLongStrings); } } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted.test index de7710d80c..7f7483f1bc 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted.test @@ -4,7 +4,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted18.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted18.test index f66902c236..7a83a0a12c 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted18.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormatted18.test @@ -3,7 +3,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedAOSP.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedAOSP.test index 10ef7c35f9..dd1493b8c8 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedAOSP.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedAOSP.test @@ -4,7 +4,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test new file mode 100644 index 0000000000..24e91a2ac8 --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test @@ -0,0 +1,12 @@ +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very" + + " very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeUnformatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeUnformatted.test index a9a957c9c2..18f2aca0e2 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeUnformatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeUnformatted.test @@ -5,7 +5,7 @@ import mylib.UsedA; public class Java { public static void main(String[] args) { -System.out.println("hello"); +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormatted.test index de37ce34dd..9ee9bcbad6 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormatted.test @@ -8,7 +8,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedAOSP.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedAOSP.test index 4511380920..ab0bbbf033 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedAOSP.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedAOSP.test @@ -8,7 +8,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedReflowLongStrings.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedReflowLongStrings.test new file mode 100644 index 0000000000..883231d0f9 --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseFormattedReflowLongStrings.test @@ -0,0 +1,17 @@ +/* + * Some license stuff. + * Very official. + */ + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very" + + " very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test index edbcbc6bc5..4992ade147 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test @@ -5,7 +5,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedAOSP.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedAOSP.test index a7b9693bfe..5c2c225e68 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedAOSP.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedAOSP.test @@ -5,7 +5,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedReflowLongStrings.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedReflowLongStrings.test new file mode 100644 index 0000000000..17a4b12db7 --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageFormattedReflowLongStrings.test @@ -0,0 +1,14 @@ +package hello.world; + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very" + + " very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test index f0dbfb0ef9..61b1b8fce4 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test @@ -6,7 +6,7 @@ import mylib.UsedA; public class Java { public static void main(String[] args) { -System.out.println("hello"); +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseUnformatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseUnformatted.test index dd7f2ea299..f43756b938 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseUnformatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithLicenseUnformatted.test @@ -9,7 +9,7 @@ import mylib.UsedA; public class Java { public static void main(String[] args) { -System.out.println("hello"); +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormatted.test index edbcbc6bc5..4992ade147 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormatted.test @@ -5,7 +5,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedAOSP.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedAOSP.test index a7b9693bfe..5c2c225e68 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedAOSP.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedAOSP.test @@ -5,7 +5,8 @@ import mylib.UsedB; public class Java { public static void main(String[] args) { - System.out.println("hello"); + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedReflowLongStrings.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedReflowLongStrings.test new file mode 100644 index 0000000000..17a4b12db7 --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageFormattedReflowLongStrings.test @@ -0,0 +1,14 @@ +package hello.world; + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very" + + " very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageUnformatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageUnformatted.test index f0dbfb0ef9..61b1b8fce4 100644 --- a/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageUnformatted.test +++ b/testlib/src/main/resources/java/googlejavaformat/JavaCodeWithPackageUnformatted.test @@ -6,7 +6,7 @@ import mylib.UsedA; public class Java { public static void main(String[] args) { -System.out.println("hello"); +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); UsedB.someMethod(); UsedA.someMethod(); } diff --git a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java index b4d893c888..b64e8e13b9 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -82,11 +82,30 @@ public void behaviorWithAospStyle() throws Exception { .testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormattedAOSP.test"); } + @Test + public void behaviorWithReflowLongStrings() throws Exception { + try (StepHarness step = StepHarness.forStep(GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultVersion(), GoogleJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral(), true))) { + if (JreVersion.thisVm() < 11) { + step.testResourceException("java/googlejavaformat/JavaCodeUnformatted.test", throwable -> { + throwable.hasMessageStartingWith("You are running Spotless on JRE 8") + .hasMessageEndingWith(", which limits you to google-java-format 1.7\n" + + "If you upgrade your build JVM to 11+, then you can use google-java-format 1.11.0, which may have fixed this problem."); + }); + } else { + step.testResource("java/googlejavaformat/JavaCodeUnformatted.test", "java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test") + .testResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test", "java/googlejavaformat/JavaCodeWithLicenseFormattedReflowLongStrings.test") + .testResource("java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test", "java/googlejavaformat/JavaCodeWithLicensePackageFormattedReflowLongStrings.test") + .testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormattedReflowLongStrings.test"); + } + } + } + @Test public void equality() throws Exception { new SerializableEqualityTester() { String version = "1.2"; String style = ""; + boolean reflowLongStrings = false; @Override protected void setupTest(API api) { @@ -98,12 +117,15 @@ protected void setupTest(API api) { // change the style, and it's different style = "AOSP"; api.areDifferentThan(); + // change the reflowLongStrings flag, and it's different + reflowLongStrings = true; + api.areDifferentThan(); } @Override protected FormatterStep create() { String finalVersion = this.version; - return GoogleJavaFormatStep.create(finalVersion, style, TestProvisioner.mavenCentral()); + return GoogleJavaFormatStep.create(finalVersion, style, TestProvisioner.mavenCentral(), reflowLongStrings); } }.testEquals(); } From a15fcee6711e23258037dd8ebb29fe2668ef8e60 Mon Sep 17 00:00:00 2001 From: Thomas Broyer Date: Sat, 4 Sep 2021 21:00:16 +0200 Subject: [PATCH 3/4] Add changelog entries --- CHANGES.md | 2 ++ plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a42c3d57c1..5704402d0f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Added support for `google-java-format`'s `skip-reflowing-long-strings` option ([#929](https://github.com/diffplug/spotless/pull/929)) ## [2.15.3] - 2021-08-20 ### Changed diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 5bfb322cde..2da511581a 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* Added support for `google-java-format`'s `skip-reflowing-long-strings` option ([#929](https://github.com/diffplug/spotless/pull/929)) ## [5.14.3] - 2021-08-20 ### Changed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 65dacd15ce..7fa7dc7936 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Added support for `google-java-format`'s `skip-reflowing-long-strings` option ([#929](https://github.com/diffplug/spotless/pull/929)) ## [2.12.3] - 2021-08-20 ### Changed From 2b4dd071142a4a5dcac50a8dab8e2049261cbe1e Mon Sep 17 00:00:00 2001 From: Thomas Broyer Date: Sat, 4 Sep 2021 21:05:24 +0200 Subject: [PATCH 4/4] Add plugin-maven test --- .../spotless/maven/java/GoogleJavaFormatTest.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java index 4cbda00383..8ae19549a2 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ import org.junit.Test; +import com.diffplug.spotless.JreVersion; import com.diffplug.spotless.maven.MavenIntegrationHarness; public class GoogleJavaFormatTest extends MavenIntegrationHarness { @@ -41,6 +42,18 @@ public void specificVersionSpecificStyle() throws Exception { runTest("java/googlejavaformat/JavaCodeFormattedAOSP.test"); } + @Test + public void specificVersionReflowLongStrings() throws Exception { + JreVersion.assume11OrGreater(); + writePomWithJavaSteps( + "", + " 1.8", + " true", + ""); + + runTest("java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test"); + } + private void runTest(String targetResource) throws Exception { String path = "src/main/java/test.java"; setFile(path).toResource("java/googlejavaformat/JavaCodeUnformatted.test");