Skip to content

Commit 46d7be3

Browse files
authored
Java 21 with Graal support (palantir#1211)
Java 21 Support with Graal & Java-based Implementations
1 parent e1ebf0e commit 46d7be3

32 files changed

Lines changed: 362 additions & 122 deletions

File tree

‎build.gradle‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ apply plugin: 'com.palantir.git-version'
2929
apply plugin: 'com.palantir.jdks'
3030
apply plugin: 'com.palantir.jdks.latest'
3131
apply plugin: 'com.palantir.baseline'
32+
apply plugin: 'com.palantir.baseline-java-versions'
3233
apply plugin: 'com.palantir.consistent-versions'
3334
apply plugin: 'com.palantir.baseline-java-versions'
3435

@@ -68,6 +69,7 @@ subprojects {
6869

6970
javaVersions {
7071
libraryTarget = 17
72+
runtime = 21
7173
}
7274

7375
jdks {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: improvement
2+
improvement:
3+
description: Java 21 with Graal support
4+
links:
5+
- https://github.com/palantir/palantir-java-format/pull/1211

‎gradle-palantir-java-format/build.gradle‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ dependencies {
2121
implementation project(':palantir-java-format-jdk-bootstrap')
2222
implementation 'com.palantir.gradle.utils:platform'
2323

24+
testImplementation project(':palantir-java-format')
2425
testImplementation 'com.netflix.nebula:nebula-test'
2526
testImplementation 'org.junit.jupiter:junit-jupiter'
26-
testImplementation 'org.junit.vintage:junit-vintage-engine'
2727
testImplementation 'org.assertj:assertj-core'
28-
testImplementation project(':palantir-java-format')
28+
29+
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
2930

3031
pluginClasspath 'com.diffplug.spotless:spotless-plugin-gradle'
3132
}
@@ -90,7 +91,6 @@ dependencies {
9091
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'nativeImage'))
9192
}
9293
}
93-
9494
}
9595

9696
tasks.register("copyNativeImage", Copy.class) {

‎gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/spotless/PalantirJavaFormatStep.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static final class State implements Serializable {
6060
* Build a cacheable state for spotless from the given jars, that uses the given {@link FormatterService}.
6161
*
6262
* @param jars The jars that contain the palantir-java-format implementation. This is only used for caching and
63-
* up-to-dateness purposes.
63+
* up-to-dateness purposes.
6464
*/
6565
State(Iterable<File> jars, Supplier<FormatterService> memoizedFormatter) throws IOException {
6666
this.jarsSignature = FileSignature.signAsSet(jars);

gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.java renamed to gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/FormatDiffTest.groovy

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
2+
* (c) Copyright 2025 Palantir Technologies Inc. All rights reserved.
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.
@@ -16,27 +16,36 @@
1616

1717
package com.palantir.javaformat.gradle;
1818

19+
import com.google.common.base.Splitter
20+
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService
21+
import com.palantir.javaformat.java.FormatterService
22+
import org.junit.jupiter.params.ParameterizedTest
23+
import org.junit.jupiter.params.provider.MethodSource
24+
25+
import java.util.stream.Stream
26+
1927
import static java.nio.charset.StandardCharsets.UTF_8;
2028
import static org.assertj.core.api.Assertions.assertThat;
2129

2230
import com.google.common.base.Preconditions;
2331
import com.google.common.collect.ImmutableList;
2432
import com.google.common.io.ByteStreams;
25-
import com.palantir.javaformat.java.FormatterServiceImpl;
26-
import java.io.ByteArrayOutputStream;
27-
import java.io.IOException;
33+
import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
2834
import java.nio.charset.StandardCharsets;
2935
import java.nio.file.Files;
3036
import java.nio.file.Path;
3137
import java.nio.file.Paths;
32-
import java.util.List;
3338
import java.util.concurrent.TimeUnit;
3439
import java.util.stream.Collectors;
3540
import org.junit.jupiter.api.Assertions;
3641
import org.junit.jupiter.api.Test;
3742
import org.junit.jupiter.api.io.TempDir;
3843

3944
class FormatDiffTest {
45+
46+
private static final CLASSPATH_FILE = new File("build/impl.classpath")
47+
private static final NATIVE_IMAGE_FILE = new File("build/nativeImage.path")
48+
4049
@TempDir
4150
Path repo;
4251

@@ -58,8 +67,9 @@ void parsing_git_diff_output_works() throws IOException {
5867
strings);
5968
}
6069

61-
@Test
62-
void reformat_a_subpath_of_a_git_directory_for_only_changed_lines() throws IOException, InterruptedException {
70+
@ParameterizedTest
71+
@MethodSource("getFormatters")
72+
void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterService formatterService) throws IOException, InterruptedException {
6373
runCommandInRepo("git", "init");
6474
runCommandInRepo("git", "config", "user.name", "Test User");
6575
runCommandInRepo("git", "config", "user.email", "test-user@palantir.com");
@@ -77,7 +87,7 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines() throws IOExc
7787

7888
runCommandInRepo("git", "add", "-N", ".");
7989

80-
FormatDiff.formatDiff(subdir, new FormatterServiceImpl());
90+
FormatDiff.formatDiff(subdir, formatterService);
8191

8292
assertThat(reformatMe).hasContent("class ReformatMe {}");
8393
assertThat(dontTouchMe).hasContent(" class DontTouchMe {}");
@@ -95,4 +105,27 @@ private void runCommandInRepo(String... args) throws IOException, InterruptedExc
95105

96106
Preconditions.checkState(process.exitValue() == 0, "Expected return code of 0: " + stderr);
97107
}
108+
109+
private static Stream<FormatterService> getFormatters() {
110+
return Stream.of(
111+
new BootstrappingFormatterService(
112+
javaBinPath(), Runtime.version().feature(), getClasspath()),
113+
new NativeImageFormatterService(
114+
Path.of(NATIVE_IMAGE_FILE.text)));
115+
}
116+
117+
private static getClasspath() {
118+
return Splitter.on(':')
119+
.trimResults()
120+
.omitEmptyStrings()
121+
.splitToStream(CLASSPATH_FILE.text)
122+
.map(Path::of)
123+
.collect(Collectors.toList());
124+
}
125+
126+
private static Path javaBinPath() {
127+
String javaHome = Preconditions.checkNotNull(System.getProperty("java.home"), "java.home property not set");
128+
return Path.of(javaHome).resolve("bin").resolve("java");
129+
}
130+
98131
}

‎idea-plugin/build.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
apply plugin: 'java'
1718
apply plugin: 'com.palantir.external-publish-intellij'
1819

1920
def name = "palantir-java-format"

‎palantir-java-format-benchmarks/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dependencies {
1515
jmhImplementation 'org.openjdk.jmh:jmh-core'
1616
jmhImplementation 'org.openjdk.jmh:jmh-generator-annprocess'
1717
formatterNativeJars project(':palantir-java-format')
18-
formatterNativeImage(project(':palantir-java-format')) {
18+
formatterNativeImage(project(':palantir-java-format-native')) {
1919
attributes {
2020
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'nativeImage'))
2121
}

‎palantir-java-format-jdk-bootstrap/build.gradle‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ tasks.named('test', Test.class) {
3131
inputs.files(configurations.named('formatterNativeImage'))
3232
environment.put('NATIVE_IMAGE_CLASSPATH', configurations.formatterNativeImage.asPath)
3333
}
34-

‎palantir-java-format-native/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ graalvmNative {
5454
sharedLibrary = false
5555
classpath.setFrom(configurations.named('runtimeClasspath'))
5656
configurationFileDirectories.from(file('src/main/resources/META-INF/native-image'))
57-
buildArgs.addAll("-H:ConfigurationFileDirectories=${projectDir}/src/main/resources/META-INF/native-image",
57+
buildArgs.addAll(
5858
'-H:+UnlockExperimentalVMOptions',
5959
'-H:IncludeResourceBundles=com.sun.tools.javac.resources.compiler',
6060
'-H:IncludeResourceBundles=com.sun.tools.javac.resources.javac',

‎palantir-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@
8383
}
8484
]
8585
},
86+
{
87+
"type": "com.palantir.javaformat.java.java21.Java21InputAstVisitor",
88+
"methods": [
89+
{
90+
"name": "<init>",
91+
"parameterTypes": [
92+
"com.palantir.javaformat.OpsBuilder",
93+
"int"
94+
]
95+
}
96+
]
97+
},
8698
{
8799
"type": "com.sun.source.tree.BindingPatternTree"
88100
},

0 commit comments

Comments
 (0)