Skip to content

Commit 7717dba

Browse files
authored
Fix Java 21 compatibilities (#1920)
2 parents 4297aa0 + c95574f commit 7717dba

File tree

22 files changed

+91
-37
lines changed

22 files changed

+91
-37
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ jobs:
4242
fail-fast: false
4343
matrix:
4444
kind: [maven, gradle]
45-
jre: [11, 17]
45+
# Test on the latest Java version once Gradle & Maven support it.
46+
jre: [11, 17, 21]
4647
os: [ubuntu-latest]
4748
include:
4849
# test windows at the diagonals of the above matrix

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Changes
14+
* Use palantir-java-format 2.38.0 on Java 21. ([#1920](https://github.com/diffplug/spotless/pull/1920))
15+
* Bump default `googleJavaFormat` version to latest `1.17.0` -> `1.18.1`. ([#1920](https://github.com/diffplug/spotless/pull/1920))
1316

1417
## [2.43.0] - 2023-11-27
1518
### Added

gradle/wrapper/gradle-wrapper.jar

-19.8 KB
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

lib/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ dependencies {
8484
gherkinCompileOnly 'io.cucumber:gherkin-utils:8.0.2'
8585
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.0'
8686
// googleJavaFormat
87-
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.17.0'
87+
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.18.1'
8888
// gson
8989
gsonCompileOnly 'com.google.code.gson:gson:2.10.1'
9090
// jackson

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private GoogleJavaFormatStep() {}
3535
private static final boolean DEFAULT_REORDER_IMPORTS = false;
3636
private static final boolean DEFAULT_FORMAT_JAVADOC = true;
3737
static final String NAME = "google-java-format";
38-
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
38+
public static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
3939

4040
/** Creates a step which formats everything - code, import order, and unused imports. */
4141
public static FormatterStep create(Provisioner provisioner) {
@@ -83,7 +83,7 @@ public static FormatterStep create(String groupArtifact, String version, String
8383
.addMin(11, "1.8") // we only support google-java-format >= 1.8 due to api changes
8484
.addMin(16, "1.10.0") // java 16 requires at least 1.10.0 due to jdk api changes in JavaTokenizer
8585
.addMin(21, "1.17.0") // java 21 requires at least 1.17.0 due to https://github.com/google/google-java-format/issues/898
86-
.add(11, "1.17.0"); // default version
86+
.add(11, "1.18.1"); // default version
8787

8888
public static String defaultGroupArtifact() {
8989
return MAVEN_COORDINATE;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ private PalantirJavaFormatStep() {}
2929

3030
private static final String DEFAULT_STYLE = "PALANTIR";
3131
private static final String NAME = "palantir-java-format";
32-
private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
33-
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0");
32+
public static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
33+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0").add(21, "2.38.0");
3434

3535
/** Creates a step which formats everything - code, import order, and unused imports. */
3636
public static FormatterStep create(Provisioner provisioner) {

plugin-gradle/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Changes
7+
* Use palantir-java-format 2.38.0 on Java 21. ([#1920](https://github.com/diffplug/spotless/pull/1920))
8+
* Bump default `googleJavaFormat` version to latest `1.17.0` -> `1.18.1`. ([#1920](https://github.com/diffplug/spotless/pull/1920))
69
### Fixed
710
* Make `KtfmtConfig.ConfigurableStyle#configure` public. ([#1926](https://github.com/diffplug/spotless/pull/1926))
811
* **BREAKING CHANGE** `6.23.0` made breaking changes to the ABI of the `KotlinExtension` and `GroovyExtension`. Those are reflected retroactively now.

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleProvisioner.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.gradle.api.artifacts.Configuration;
2727
import org.gradle.api.artifacts.ConfigurationContainer;
2828
import org.gradle.api.artifacts.dsl.DependencyHandler;
29+
import org.gradle.api.attributes.Attribute;
2930
import org.gradle.api.attributes.Bundling;
3031
import org.gradle.api.attributes.Category;
3132
import org.gradle.api.initialization.dsl.ScriptHandler;
@@ -35,6 +36,8 @@
3536
import com.diffplug.common.base.Unhandled;
3637
import com.diffplug.common.collect.ImmutableList;
3738
import com.diffplug.spotless.Provisioner;
39+
import com.diffplug.spotless.java.GoogleJavaFormatStep;
40+
import com.diffplug.spotless.java.PalantirJavaFormatStep;
3841

3942
/** Should be package-private. */
4043
class GradleProvisioner {
@@ -116,14 +119,26 @@ private static Provisioner forConfigurationContainer(Project project, Configurat
116119
+ new Request(withTransitives, mavenCoords).hashCode());
117120
mavenCoords.stream()
118121
.map(dependencies::create)
119-
.forEach(config.getDependencies()::add);
122+
.forEach(dependency -> {
123+
config.getDependencies().add(dependency);
124+
String coordinate = dependency.getGroup() + ":" + dependency.getName();
125+
if (coordinate.startsWith(GoogleJavaFormatStep.MAVEN_COORDINATE) ||
126+
coordinate.startsWith(PalantirJavaFormatStep.MAVEN_COORDINATE)) {
127+
// Use Guava 32.1.3, see https://github.com/google/guava/issues/6657.
128+
// TODO: May remove this after https://github.com/google/google-java-format/pull/996 and https://github.com/palantir/palantir-java-format/issues/957 are released.
129+
config.getDependencies().add(dependencies.create("com.google.guava:guava:32.1.3-jre"));
130+
}
131+
});
120132
config.setDescription(mavenCoords.toString());
121133
config.setTransitive(withTransitives);
122134
config.setCanBeConsumed(false);
123135
config.setVisible(false);
124136
config.attributes(attr -> {
125137
attr.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY));
126138
attr.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.EXTERNAL));
139+
// TODO: This is a copy-paste from org.gradle.api.attributes.java.TargetJvmEnvironment which is added in Gradle 7.0, remove this once we drop support for Gradle 6.x.
140+
// Add this attribute for resolving Guava dependency, see https://github.com/google/guava/issues/6801.
141+
attr.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm");
127142
});
128143
return config.resolve();
129144
} catch (Exception e) {

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.gradle.testkit.runner.BuildResult;
2121
import org.gradle.testkit.runner.GradleRunner;
2222
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.condition.EnabledForJreRange;
24+
import org.junit.jupiter.api.condition.JRE;
2325

2426
public class ConfigurationCacheTest extends GradleIntegrationHarness {
2527
@Override
@@ -63,6 +65,7 @@ public void helpConfiguresIfTasksAreCreated() throws IOException {
6365
}
6466

6567
@Test
68+
@EnabledForJreRange(max = JRE.JAVA_20)
6669
public void jvmLocalCache() throws IOException {
6770
setFile("build.gradle").toLines(
6871
"plugins {",
@@ -91,6 +94,10 @@ public void jvmLocalCache() throws IOException {
9194
gradleRunner().withArguments("spotlessApply").build();
9295
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test");
9396

97+
// the withDebug forces it to start a new deamon, but only in Gradle 8.3 and older
98+
// starting with Gradle 8.5 this doesn't work anymore
99+
// and we need Gradle 8.5 for Java 21
100+
// so we can't test this on Java 21 for now
94101
BuildResult failure = gradleRunner().withDebug(true).withArguments("spotlessApply", "--stacktrace").buildAndFail();
95102
failure.getOutput().contains("Spotless daemon-local cache is stale. Regenerate the cache with\n" +
96103
" rm -rf .gradle/configuration-cache\n" +

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GoogleJavaFormatIntegrationTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void integration() throws IOException {
3131
"spotless {",
3232
" java {",
3333
" target file('test.java')",
34-
" googleJavaFormat('1.10.0')",
34+
" googleJavaFormat('1.17.0')",
3535
" }",
3636
"}");
3737

@@ -41,7 +41,7 @@ void integration() throws IOException {
4141

4242
checkRunsThenUpToDate();
4343
replace("build.gradle",
44-
"googleJavaFormat('1.10.0')",
44+
"googleJavaFormat('1.17.0')",
4545
"googleJavaFormat()");
4646
checkRunsThenUpToDate();
4747
}
@@ -57,7 +57,7 @@ void integrationWithReorderImports() throws IOException {
5757
"spotless {",
5858
" java {",
5959
" target file('test.java')",
60-
" googleJavaFormat('1.12.0').aosp().reorderImports(true)",
60+
" googleJavaFormat('1.17.0').aosp().reorderImports(true)",
6161
" }",
6262
"}");
6363

@@ -67,7 +67,7 @@ void integrationWithReorderImports() throws IOException {
6767

6868
checkRunsThenUpToDate();
6969
replace("build.gradle",
70-
"googleJavaFormat('1.12.0')",
70+
"googleJavaFormat('1.17.0')",
7171
"googleJavaFormat()");
7272
checkRunsThenUpToDate();
7373
}
@@ -83,7 +83,7 @@ void integrationWithSkipJavadocFormatting() throws IOException {
8383
"spotless {",
8484
" java {",
8585
" target file('test.java')",
86-
" googleJavaFormat('1.12.0').skipJavadocFormatting()",
86+
" googleJavaFormat('1.17.0').skipJavadocFormatting()",
8787
" }",
8888
"}");
8989

@@ -93,7 +93,7 @@ void integrationWithSkipJavadocFormatting() throws IOException {
9393

9494
checkRunsThenUpToDate();
9595
replace("build.gradle",
96-
"googleJavaFormat('1.12.0')",
96+
"googleJavaFormat('1.17.0')",
9797
"googleJavaFormat()");
9898
checkRunsThenUpToDate();
9999
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ public enum GradleVersionSupport {
5454
GradleVersionSupport(String version) {
5555
String minVersionForRunningJRE;
5656
switch (Jvm.version()) {
57+
case 22:
58+
// TODO: https://docs.gradle.org/current/userguide/compatibility.html
5759
case 21:
60+
minVersionForRunningJRE = "8.5";
61+
break;
5862
case 20:
59-
// TODO: https://docs.gradle.org/current/userguide/compatibility.html
63+
minVersionForRunningJRE = "8.3";
64+
break;
6065
case 19:
6166
minVersionForRunningJRE = "7.6";
6267
break;

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/MultiProjectTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void createSubproject(String name) throws IOException {
4747
"spotless {",
4848
" java {",
4949
" target file('test.java')",
50-
" googleJavaFormat('1.16.0')",
50+
" googleJavaFormat('1.17.0')",
5151
" }",
5252
"}");
5353
setFile(name + "/test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
@@ -71,7 +71,7 @@ public void hasRootSpotless() throws IOException {
7171
"spotless {",
7272
" java {",
7373
" target file('test.java')",
74-
" googleJavaFormat('1.16.0')",
74+
" googleJavaFormat('1.17.0')",
7575
" }",
7676
"}");
7777
setFile("test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
@@ -88,7 +88,7 @@ public void predeclaredFails() throws IOException {
8888
"spotless { predeclareDeps() }");
8989
createNSubprojects();
9090
Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput())
91-
.contains("Add a step with [com.google.googlejavaformat:google-java-format:1.16.0] into the `spotlessPredeclare` block in the root project.");
91+
.contains("Add a step with [com.google.googlejavaformat:google-java-format:1.17.0] into the `spotlessPredeclare` block in the root project.");
9292
}
9393

9494
@Test
@@ -100,7 +100,7 @@ public void predeclaredSucceeds() throws IOException {
100100
"repositories { mavenCentral() }",
101101
"spotless { predeclareDeps() }",
102102
"spotlessPredeclare {",
103-
" java { googleJavaFormat('1.16.0') }",
103+
" java { googleJavaFormat('1.17.0') }",
104104
"}");
105105
createNSubprojects();
106106
gradleRunner().withArguments("spotlessApply").build();
@@ -115,7 +115,7 @@ public void predeclaredFromBuildscriptSucceeds() throws IOException {
115115
"repositories { mavenCentral() }",
116116
"spotless { predeclareDepsFromBuildscript() }",
117117
"spotlessPredeclare {",
118-
" java { googleJavaFormat('1.16.0') }",
118+
" java { googleJavaFormat('1.17.0') }",
119119
"}");
120120
createNSubprojects();
121121
gradleRunner().withArguments("spotlessApply").build();
@@ -129,7 +129,7 @@ public void predeclaredOrdering() throws IOException {
129129
"}",
130130
"repositories { mavenCentral() }",
131131
"spotlessPredeclare {",
132-
" java { googleJavaFormat('1.16.0') }",
132+
" java { googleJavaFormat('1.17.0') }",
133133
"}",
134134
"spotless { predeclareDepsFromBuildscript() }");
135135
createNSubprojects();
@@ -145,7 +145,7 @@ public void predeclaredUndeclared() throws IOException {
145145
"}",
146146
"repositories { mavenCentral() }",
147147
"spotlessPredeclare {",
148-
" java { googleJavaFormat('1.16.0') }",
148+
" java { googleJavaFormat('1.17.0') }",
149149
"}");
150150
createNSubprojects();
151151
Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput())

plugin-maven/CHANGES.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Changes
7+
* Use palantir-java-format 2.38.0 on Java 21. ([#1920](https://github.com/diffplug/spotless/pull/1920))
8+
* Bump default `googleJavaFormat` version to latest `1.17.0` -> `1.18.1`. ([#1920](https://github.com/diffplug/spotless/pull/1920))
69
### Fixed
710
* Revert [#1846](https://github.com/diffplug/spotless/issues/1846) from 2.41.0 which causes the plugin to format generated sources in the `target` directory. ([#1928](https://github.com/diffplug/spotless/pull/1928))
811

912
## [2.41.0] - 2023-11-27
1013
### Added
11-
* CompileSourceRoots and TestCompileSourceRoots are now respected as default includes. These properties are commonly set when adding extra source directories. ([#1846](https://github.com/diffplug/spotless/issues/1846))
14+
* ~~CompileSourceRoots and TestCompileSourceRoots are now respected as default includes. These properties are commonly set when adding extra source directories.~~ ([#1846](https://github.com/diffplug/spotless/issues/1846))
15+
* Reverted in the next release (`2.41.1`) due to backward compatibility problems, see [#1914](https://github.com/diffplug/spotless/issues/1914).
1216
* Support custom rule sets for Ktlint. ([#1896](https://github.com/diffplug/spotless/pull/1896))
1317
### Fixed
1418
* Fix crash when build dir is a softlink to another directory. ([#1859](https://github.com/diffplug/spotless/pull/1859))

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenProvisionerTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void testMultipleDependenciesExcludingTransitives() throws Exception {
3333
void testSingleDependencyIncludingTransitives() throws Exception {
3434
writePomWithJavaSteps(
3535
"<googleJavaFormat>",
36-
" <version>1.10.0</version>",
3736
"</googleJavaFormat>");
3837
assertResolveDependenciesWorks();
3938
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/SpecificFilesTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ private void integration(String patterns, boolean firstFormatted, boolean second
5858
" <include>src/**/java/**/*.java</include>",
5959
"</includes>",
6060
"<googleJavaFormat>",
61-
" <version>1.10.0</version>",
6261
"</googleJavaFormat>");
6362

6463
setFile(testFile(1)).toResource(fixture(false));

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GoogleJavaFormatTest extends MavenIntegrationHarness {
2424
void specificVersionDefaultStyle() throws Exception {
2525
writePomWithJavaSteps(
2626
"<googleJavaFormat>",
27-
" <version>1.10.0</version>",
27+
" <version>1.17.0</version>",
2828
"</googleJavaFormat>");
2929

3030
runTest("java/googlejavaformat/JavaCodeFormatted.test");
@@ -34,7 +34,7 @@ void specificVersionDefaultStyle() throws Exception {
3434
void specificVersionSpecificStyle() throws Exception {
3535
writePomWithJavaSteps(
3636
"<googleJavaFormat>",
37-
" <version>1.10.0</version>",
37+
" <version>1.17.0</version>",
3838
" <style>AOSP</style>",
3939
"</googleJavaFormat>");
4040

@@ -45,7 +45,7 @@ void specificVersionSpecificStyle() throws Exception {
4545
void specificVersionReflowLongStrings() throws Exception {
4646
writePomWithJavaSteps(
4747
"<googleJavaFormat>",
48-
" <version>1.10.0</version>",
48+
" <version>1.17.0</version>",
4949
" <reflowLongStrings>true</reflowLongStrings>",
5050
"</googleJavaFormat>");
5151

@@ -56,7 +56,7 @@ void specificVersionReflowLongStrings() throws Exception {
5656
void specificVersionReorderImports() throws Exception {
5757
writePomWithJavaSteps(
5858
"<googleJavaFormat>",
59-
" <version>1.12.0</version>",
59+
" <version>1.17.0</version>",
6060
" <style>AOSP</style>",
6161
" <reorderImports>true</reorderImports>",
6262
"</googleJavaFormat>");
@@ -68,7 +68,7 @@ void specificVersionReorderImports() throws Exception {
6868
void specificVersionSkipJavadocFormatting() throws Exception {
6969
writePomWithJavaSteps(
7070
"<googleJavaFormat>",
71-
" <version>1.12.0</version>",
71+
" <version>1.17.0</version>",
7272
" <formatJavadoc>false</formatJavadoc>",
7373
"</googleJavaFormat>");
7474

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void specificVersionDefaultStyle() throws Exception {
3434
void specificJava11Version2() throws Exception {
3535
writePomWithJavaSteps(
3636
"<palantirJavaFormat>",
37-
" <version>2.10.0</version>",
37+
" <version>2.38.0</version>",
3838
"</palantirJavaFormat>");
3939

4040
runTest("java/palantirjavaformat/JavaCodeFormatted.test");

0 commit comments

Comments
 (0)