Skip to content

Commit 086c843

Browse files
committed
google-java-format now uses the latest default available for the current 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.
1 parent 1a6ff6f commit 086c843

4 files changed

Lines changed: 87 additions & 7 deletions

File tree

lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class GoogleJavaFormatStep {
3232
// prevent direct instantiation
3333
private GoogleJavaFormatStep() {}
3434

35-
private static final String DEFAULT_VERSION = "1.7";
3635
private static final String DEFAULT_STYLE = "GOOGLE";
3736
static final String NAME = "google-java-format";
3837
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
7574
State::createFormat);
7675
}
7776

77+
private static final int JRE_VERSION;
78+
79+
static {
80+
String jre = System.getProperty("java.version");
81+
if (jre.startsWith("1.8")) {
82+
JRE_VERSION = 8;
83+
} else {
84+
JRE_VERSION = Integer.parseInt(jre.substring(0, jre.indexOf('.')));
85+
}
86+
}
87+
88+
/** On JRE 11+, returns `1.9`. On earlier JREs, returns `1.7`. */
7889
public static String defaultVersion() {
79-
return DEFAULT_VERSION;
90+
return JRE_VERSION >= 11 ? LATEST_VERSION_JRE_11 : LATEST_VERSION_JRE_8;
8091
}
8192

93+
private static final String LATEST_VERSION_JRE_8 = "1.7";
94+
private static final String LATEST_VERSION_JRE_11 = "1.9";
95+
8296
public static String defaultStyle() {
8397
return DEFAULT_STYLE;
8498
}
@@ -130,20 +144,18 @@ FormatterFunc createFormat() throws Exception {
130144
Class<?> importOrdererClass = classLoader.loadClass(IMPORT_ORDERER_CLASS);
131145
Method importOrdererMethod = importOrdererClass.getMethod(IMPORT_ORDERER_METHOD, String.class);
132146

133-
return input -> {
147+
return suggestJre11(input -> {
134148
String formatted = (String) formatterMethod.invoke(formatter, input);
135149
String removedUnused = removeUnused.apply(formatted);
136150
String sortedImports = (String) importOrdererMethod.invoke(null, removedUnused);
137151
return fixWindowsBug(sortedImports, version);
138-
};
152+
});
139153
}
140154

141155
FormatterFunc createRemoveUnusedImportsOnly() throws Exception {
142156
ClassLoader classLoader = jarState.getClassLoader();
143-
144157
Function<String, String> removeUnused = constructRemoveUnusedFunction(classLoader);
145-
146-
return input -> fixWindowsBug(removeUnused.apply(input), version);
158+
return suggestJre11(input -> fixWindowsBug(removeUnused.apply(input), version));
147159
}
148160

149161
private static Function<String, String> constructRemoveUnusedFunction(ClassLoader classLoader)
@@ -204,4 +216,19 @@ static String fixWindowsBug(String input, String version) {
204216
}
205217
return input;
206218
}
219+
220+
private static FormatterFunc suggestJre11(FormatterFunc in) {
221+
if (JRE_VERSION >= 11) {
222+
return in;
223+
} else {
224+
return unixIn -> {
225+
try {
226+
return in.apply(unixIn);
227+
} catch (Exception e) {
228+
throw new Exception("You are running Spotless on JRE " + JRE_VERSION + ", which limits you to google-java-format " + LATEST_VERSION_JRE_8 + "\n"
229+
+ "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);
230+
}
231+
};
232+
}
233+
}
207234
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import mylib.UsedA;
2+
import mylib.UsedB;
3+
4+
public class Java {
5+
public static void main(String[] args) {
6+
var a = """
7+
Howdy
8+
Partner!
9+
""";
10+
System.out.println(a);
11+
UsedB.someMethod();
12+
UsedA.someMethod();
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import mylib.UsedA;
3+
import mylib.UsedB;
4+
5+
public class Java {
6+
public static void main(String[] args) {
7+
var a = """
8+
Howdy
9+
Partner!
10+
""";
11+
System.out.println(a);
12+
UsedB.someMethod();
13+
UsedA.someMethod();
14+
}
15+
}

testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.java;
1717

18+
import java.lang.reflect.InvocationTargetException;
19+
1820
import org.junit.Assert;
1921
import org.junit.Test;
2022

@@ -27,8 +29,30 @@
2729
import com.diffplug.spotless.TestProvisioner;
2830

2931
public class GoogleJavaFormatStepTest extends ResourceHarness {
32+
@Test
33+
public void suggestJre11() throws Exception {
34+
try (StepHarness step = StepHarness.forStep(GoogleJavaFormatStep.create(TestProvisioner.mavenCentral()))) {
35+
if (JreVersion.thisVm() < 11) {
36+
step.testException("java/googlejavaformat/TextBlock.dirty", throwable -> {
37+
throwable.hasMessageStartingWith("You are running Spotless on JRE 8")
38+
.hasMessageEndingWith(", which limits you to google-java-format 1.7\n"
39+
+ "If you upgrade your build JVM to 11+, then you can use google-java-format 1.9, which may have fixed this problem.");
40+
});
41+
} else if (JreVersion.thisVm() < 13) {
42+
step.testException("java/googlejavaformat/TextBlock.dirty", throwable -> {
43+
throwable.isInstanceOf(InvocationTargetException.class)
44+
.extracting(exception -> exception.getCause().getMessage()).asString().contains("7:18: error: unclosed string literal");
45+
});
46+
} else {
47+
// JreVersion.thisVm() >= 13
48+
step.testResource("java/googlejavaformat/TextBlock.dirty", "java/googlejavaformat/TextBlock.clean");
49+
}
50+
}
51+
}
52+
3053
@Test
3154
public void behavior18() throws Exception {
55+
// google-java-format requires JRE 11+
3256
JreVersion.assume11OrGreater();
3357
FormatterStep step = GoogleJavaFormatStep.create("1.8", TestProvisioner.mavenCentral());
3458
StepHarness.forStep(step)

0 commit comments

Comments
 (0)