Skip to content

Commit

Permalink
google-java-format now uses the latest default available for the curr…
Browse files Browse the repository at this point in the history
…ent JRE. If the user is using an old JRE and has a failure, it lets them know that they might get a fix by updating to a newer JRE to get the latest GJF.
  • Loading branch information
nedtwigg committed Sep 8, 2020
1 parent 1a6ff6f commit 086c843
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
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);
}
};
}
}
}
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

0 comments on commit 086c843

Please sign in to comment.