Skip to content

Commit 5e21a11

Browse files
committed
lintify the Jvm version infrastructure
1 parent 1776749 commit 5e21a11

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,8 @@ public static int version() {
6161
* @param <V> Version type of formatter
6262
*/
6363
public static class Support<V> {
64+
static final String LINT_CODE = "jvm-version";
65+
6466
private final String fmtName;
6567
private final Comparator<? super V> fmtVersionComparator;
6668
private final NavigableMap<Integer, V> jvm2fmtMaxVersion;
@@ -139,7 +141,7 @@ public void assertFormatterSupported(V formatterVersion) {
139141
Objects.requireNonNull(formatterVersion);
140142
String error = buildUnsupportedFormatterMessage(formatterVersion);
141143
if (!error.isEmpty()) {
142-
throw new IllegalArgumentException(error);
144+
throw Lint.atUndefinedLine(LINT_CODE, error).shortcut();
143145
}
144146
}
145147

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,9 +94,9 @@ void supportListsMinimumJvmIfOnlyHigherJvmSupported() {
9494
assertNull(testSupport.getRecommendedFormatterVersion(), "No formatter version is supported");
9595

9696
for (String fmtVersion : Arrays.asList("1.2", "1.2.3", "1.2-SNAPSHOT", "1.2.3-SNAPSHOT")) {
97-
String proposal = assertThrows(Exception.class, () -> {
97+
String proposal = assertThrows(Lint.ShortcutException.class, () -> {
9898
testSupport.assertFormatterSupported(fmtVersion);
99-
}).getMessage();
99+
}).getLints().get(0).getDetail();
100100
assertThat(proposal).contains(String.format("on JVM %d", Jvm.version()));
101101
assertThat(proposal).contains(String.format("%s %s requires JVM %d+", TEST_NAME, fmtVersion, higherJvmVersion));
102102
assertThat(proposal).contains(String.format("try %s alternatives", TEST_NAME));
@@ -112,9 +112,9 @@ void supportListsMinimumJvmIfOnlyHigherJvmSupported() {
112112
}
113113

114114
for (String fmtVersion : Arrays.asList("1.2.4", "2", "1.2.5-SNAPSHOT")) {
115-
String proposal = assertThrows(Exception.class, () -> {
115+
String proposal = assertThrows(Lint.ShortcutException.class, () -> {
116116
testSupport.assertFormatterSupported(fmtVersion);
117-
}).getMessage();
117+
}).getLints().get(0).getDetail();
118118
assertThat(proposal).contains(String.format("%s %s requires JVM %d+", TEST_NAME, fmtVersion, higherJvmVersion + 1));
119119

120120
proposal = assertThrows(Exception.class, () -> {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ void behavior() throws Exception {
6565
void versionBelowMinimumRequiredVersionIsNotAllowed() throws Exception {
6666
FormatterStep step = GoogleJavaFormatStep.create("1.2", "AOSP", TestProvisioner.mavenCentral());
6767
StepHarness.forStepNoRoundtrip(step)
68-
.testResourceExceptionMsg("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test")
69-
.contains("you are using 1.2");
68+
.expectLintsOfResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test")
69+
.toBe("LINE_UNDEFINED google-java-format(jvm-version) You are running Spotless on JVM 21. This requires google-java-format of at least 1.17.0 (you are using 1.2).",
70+
"");
7071
}
7172

7273
@Test
7374
@EnabledForJreRange(min = JAVA_16)
7475
void versionBelowOneDotTenIsNotAllowed() throws Exception {
7576
FormatterStep step = GoogleJavaFormatStep.create("1.9", "AOSP", TestProvisioner.mavenCentral());
7677
StepHarness.forStepNoRoundtrip(step)
77-
.testResourceExceptionMsg("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test")
78-
.contains("you are using 1.9");
78+
.expectLintsOfResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test")
79+
.toBe("LINE_UNDEFINED google-java-format(jvm-version) You are running Spotless on JVM 21. This requires google-java-format of at least 1.17.0 (you are using 1.9).",
80+
"");
7981
}
8082

8183
@Test

0 commit comments

Comments
 (0)