Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update google-java-format latest version on JDK 11+ #687

Merged
merged 2 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:";
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<String, String> removeUnused = constructRemoveUnusedFunction(classLoader);

return input -> fixWindowsBug(removeUnused.apply(input), version);
return suggestJre11(input -> fixWindowsBug(removeUnused.apply(input), version));
}

private static Function<String, String> constructRemoveUnusedFunction(ClassLoader classLoader)
Expand Down Expand Up @@ -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);
}
};
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).

Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* `<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)).

## [2.1.0] - 2020-08-29
### Added
Expand Down
14 changes: 14 additions & 0 deletions testlib/src/main/resources/java/googlejavaformat/TextBlock.clean
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 15 additions & 0 deletions testlib/src/main/resources/java/googlejavaformat/TextBlock.dirty
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.diffplug.spotless.java;

import java.lang.reflect.InvocationTargetException;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -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)
Expand Down