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 ed7b89be5f..454a3cc0bd 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java @@ -30,7 +30,7 @@ public class GoogleJavaFormatStep { // prevent direct instantiation private GoogleJavaFormatStep() {} - private static final String DEFAULT_VERSION = "1.1"; + private static final String DEFAULT_VERSION = "1.2"; 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"; @@ -67,10 +67,12 @@ static final class State implements Serializable { /** The jar that contains the eclipse formatter. */ final JarState jarState; final String stepName; + final String version; State(String stepName, String version, Provisioner provisioner) throws IOException { - this.stepName = stepName; this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner); + this.stepName = stepName; + this.version = version; } @SuppressWarnings({"unchecked", "rawtypes"}) @@ -94,7 +96,7 @@ FormatterFunc createFormat() throws Exception { String formatted = (String) formatterMethod.invoke(formatter, input); String removedUnused = (String) removeUnusedMethod.invoke(null, formatted, removeJavadocConstant); String sortedImports = (String) importOrdererMethod.invoke(null, removedUnused); - return fixWindowsBug(sortedImports); + return fixWindowsBug(sortedImports, version); }; } @@ -107,18 +109,21 @@ FormatterFunc createRemoveUnusedImportsOnly() throws Exception { Object removeJavadocConstant = Enum.valueOf((Class) removeJavadocOnlyClass, REMOVE_UNUSED_IMPORT_JavadocOnlyImports_Keep); Method removeUnusedMethod = removeUnusedClass.getMethod(REMOVE_UNUSED_METHOD, String.class, removeJavadocOnlyClass); - return input -> fixWindowsBug((String) removeUnusedMethod.invoke(null, input, removeJavadocConstant)); + return input -> { + String removeUnused = (String) removeUnusedMethod.invoke(null, input, removeJavadocConstant); + return fixWindowsBug(removeUnused, version); + }; } } private static final boolean IS_WINDOWS = LineEnding.PLATFORM_NATIVE.str().equals("\r\n"); /** - * google-java-format's removeUnusedImports does *wacky* stuff on Windows. + * google-java-format-1.1's removeUnusedImports does *wacky* stuff on Windows. * The beauty of normalizing all line endings to unix! */ - static String fixWindowsBug(String input) { - if (IS_WINDOWS) { + static String fixWindowsBug(String input, String version) { + if (IS_WINDOWS && version.equals("1.1")) { int firstImport = input.indexOf("\nimport "); if (firstImport == 0) { return input; 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 868b0b649c..7e4f25ce95 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java @@ -28,7 +28,7 @@ public class GoogleJavaFormatStepTest extends ResourceHarness { @Test public void behavior() throws Exception { - FormatterStep step = GoogleJavaFormatStep.create("1.1", TestProvisioner.mavenCentral()); + FormatterStep step = GoogleJavaFormatStep.create("1.2", TestProvisioner.mavenCentral()); StepHarness.forStep(step) .testResource("java/googlejavaformat/JavaCodeUnformatted.test", "java/googlejavaformat/JavaCodeFormatted.test") .testResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test", "java/googlejavaformat/JavaCodeWithLicenseFormatted.test") @@ -39,7 +39,7 @@ public void behavior() throws Exception { @Test public void equality() throws Exception { new StepEqualityTester() { - String version = "1.1"; + String version = "1.2"; @Override protected void setupTest(API api) { @@ -47,7 +47,7 @@ protected void setupTest(API api) { api.assertThisEqualToThis(); api.areDifferentThan(); // change the version, and it's different - version = "1.0"; + version = "1.1"; api.assertThisEqualToThis(); api.areDifferentThan(); } @@ -61,7 +61,7 @@ protected FormatterStep create() { } @Test - public void fixWindowsBug() { + public void fixWindowsBugForGfj1Point1() { fixWindowsBugTestcase(""); fixWindowsBugTestcase( "", @@ -96,6 +96,6 @@ public void fixWindowsBug() { private void fixWindowsBugTestcase(String... lines) { String input = StringPrinter.buildStringFromLines(lines); - Assert.assertEquals(input, GoogleJavaFormatStep.fixWindowsBug(input)); + Assert.assertEquals(input, GoogleJavaFormatStep.fixWindowsBug(input, "1.1")); } }