diff --git a/CHANGES.md b/CHANGES.md index 602c373c25..115cbf206b 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 +* `GoogleJavaFormatStep.defaultVersion()` now returns `1.9` on JDK 11+, while continuing to return `1.7` on earlier JDKs. This is especially helpful to `RemoveUnusedImportsStep`, since it always uses the default version of GJF (fixes [#681](https://github.com/diffplug/spotless/issues/681)). ### Fixed * We now run all tests against JDK 8, JDK 11, and also JDK 14 ([#684](https://github.com/diffplug/spotless/pull/684)). * We had test files in `testlib/src/main/resources` named `module-info.java` and `package-info.java`. They cause problems for the Eclipse IDE trying to interpret them literally. Added `.test` suffix to the filenames so that eclipse doesn't barf on them anymore ([#683](https://github.com/diffplug/spotless/pull/683)). 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 b30dbfd48b..5b127372ee 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java @@ -32,7 +32,6 @@ public class GoogleJavaFormatStep { // prevent direct instantiation private GoogleJavaFormatStep() {} - private static final String DEFAULT_VERSION = "1.7"; private static final String DEFAULT_STYLE = "GOOGLE"; static final String NAME = "google-java-format"; static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format:"; @@ -75,10 +74,25 @@ public static FormatterStep create(String version, String style, Provisioner pro State::createFormat); } + private static final int JRE_VERSION; + + static { + String jre = System.getProperty("java.version"); + if (jre.startsWith("1.8")) { + JRE_VERSION = 8; + } else { + JRE_VERSION = Integer.parseInt(jre.substring(0, jre.indexOf('.'))); + } + } + + /** On JRE 11+, returns `1.9`. On earlier JREs, returns `1.7`. */ public static String defaultVersion() { - return DEFAULT_VERSION; + return JRE_VERSION >= 11 ? LATEST_VERSION_JRE_11 : LATEST_VERSION_JRE_8; } + private static final String LATEST_VERSION_JRE_8 = "1.7"; + private static final String LATEST_VERSION_JRE_11 = "1.9"; + public static String defaultStyle() { return DEFAULT_STYLE; } @@ -130,20 +144,18 @@ FormatterFunc createFormat() throws Exception { Class importOrdererClass = classLoader.loadClass(IMPORT_ORDERER_CLASS); Method importOrdererMethod = importOrdererClass.getMethod(IMPORT_ORDERER_METHOD, String.class); - return input -> { + 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); - }; + }); } FormatterFunc createRemoveUnusedImportsOnly() throws Exception { ClassLoader classLoader = jarState.getClassLoader(); - Function removeUnused = constructRemoveUnusedFunction(classLoader); - - return input -> fixWindowsBug(removeUnused.apply(input), version); + return suggestJre11(input -> fixWindowsBug(removeUnused.apply(input), version)); } private static Function constructRemoveUnusedFunction(ClassLoader classLoader) @@ -204,4 +216,19 @@ static String fixWindowsBug(String input, String version) { } return input; } + + private static FormatterFunc suggestJre11(FormatterFunc in) { + if (JRE_VERSION >= 11) { + return in; + } else { + return unixIn -> { + try { + return in.apply(unixIn); + } catch (Exception e) { + throw new Exception("You are running Spotless on JRE " + JRE_VERSION + ", which limits you to google-java-format " + LATEST_VERSION_JRE_8 + "\n" + + "If you upgrade your build JVM to 11+, then you can use google-java-format " + LATEST_VERSION_JRE_11 + ", which may have fixed this problem.", e); + } + }; + } + } } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 1ac023365b..0d3636d832 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 +* `googleJavaFormat()` default version is now `1.9` on JDK 11+, while continuing to be `1.7` on earlier JDKs. This is especially helpful to `removeUnusedImports()`, since it always uses the default version of GJF (fixes [#681](https://github.com/diffplug/spotless/issues/681)). ### Fixed * We did not proactively check to ensure that the Gradle version was modern enough, now we do (fixes [#684](https://github.com/diffplug/spotless/pull/684)). diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index b6cb59174a..caef30816b 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 +* `` default version is now `1.9` on JDK 11+, while continuing to be `1.7` on earlier JDKs. This is especially helpful to ``, since it always uses the default version of GJF (fixes [#681](https://github.com/diffplug/spotless/issues/681)). ## [2.1.0] - 2020-08-29 ### Added diff --git a/testlib/src/main/resources/java/googlejavaformat/TextBlock.clean b/testlib/src/main/resources/java/googlejavaformat/TextBlock.clean new file mode 100644 index 0000000000..8ee77fbf7a --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/TextBlock.clean @@ -0,0 +1,14 @@ +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + var a = """ + Howdy + Partner! + """; + System.out.println(a); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/googlejavaformat/TextBlock.dirty b/testlib/src/main/resources/java/googlejavaformat/TextBlock.dirty new file mode 100644 index 0000000000..4c329387db --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/TextBlock.dirty @@ -0,0 +1,15 @@ + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + var a = """ + Howdy + Partner! + """; + System.out.println(a); + 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 2d0382e2e6..dc4738a074 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java @@ -15,6 +15,8 @@ */ package com.diffplug.spotless.java; +import java.lang.reflect.InvocationTargetException; + import org.junit.Assert; import org.junit.Test; @@ -27,8 +29,30 @@ import com.diffplug.spotless.TestProvisioner; public class GoogleJavaFormatStepTest extends ResourceHarness { + @Test + public void suggestJre11() throws Exception { + try (StepHarness step = StepHarness.forStep(GoogleJavaFormatStep.create(TestProvisioner.mavenCentral()))) { + if (JreVersion.thisVm() < 11) { + step.testException("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."); + }); + } else if (JreVersion.thisVm() < 13) { + step.testException("java/googlejavaformat/TextBlock.dirty", throwable -> { + throwable.isInstanceOf(InvocationTargetException.class) + .extracting(exception -> exception.getCause().getMessage()).asString().contains("7:18: error: unclosed string literal"); + }); + } else { + // JreVersion.thisVm() >= 13 + step.testResource("java/googlejavaformat/TextBlock.dirty", "java/googlejavaformat/TextBlock.clean"); + } + } + } + @Test public void behavior18() throws Exception { + // google-java-format requires JRE 11+ JreVersion.assume11OrGreater(); FormatterStep step = GoogleJavaFormatStep.create("1.8", TestProvisioner.mavenCentral()); StepHarness.forStep(step)