Skip to content

Commit f59b03d

Browse files
authored
Use modern infra to support multiple JDK versions (#1226)
Use modern infra to support multiple JDK versions
1 parent 30b646f commit f59b03d

File tree

7 files changed

+34
-86
lines changed

7 files changed

+34
-86
lines changed

build.gradle

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ apply plugin: 'com.palantir.jdks'
3030
apply plugin: 'com.palantir.jdks.latest'
3131
apply plugin: 'com.palantir.baseline'
3232
apply plugin: 'com.palantir.consistent-versions'
33+
apply plugin: 'com.palantir.baseline-java-versions'
3334

3435
version System.env.CIRCLE_TAG ?: gitVersion()
3536

@@ -49,15 +50,13 @@ subprojects {
4950
apply plugin: 'java-library'
5051
apply plugin: 'org.inferred.processors'
5152

52-
sourceCompatibility = 11
53-
targetCompatibility = 11
54-
5553
tasks.withType(Checkstyle).configureEach {
5654
enabled = false
5755
}
5856

5957
tasks.withType(JavaCompile).configureEach {
6058
options.errorprone.disable 'PreconditionsConstantMessage', 'PreferSafeLoggableExceptions', 'PreferSafeLoggingPreconditions'
59+
options.errorprone.disable 'StrictUnusedVariable'
6160
}
6261

6362
// Run `./gradlew test -Drecreate=true` to recreate all the expected
@@ -67,33 +66,9 @@ subprojects {
6766
}
6867
}
6968

70-
/**
71-
* There is a bunch of truly snowflaky stuff going on this project, because we have this Java14InputAstVisitor class
72-
* which needs to implement some interface methods defined in com.sun.source.util.TreePathScanner, which varies
73-
* depending on what JVM the formatter is running on!
74-
*/
75-
idea.project.ipr.withXml { xml ->
76-
/**
77-
* Even though the p-j-f codebase has sourceCompat=11 and targetCompat=11, we have to convince IntelliJ to let us
78-
* compile against methods from a newer JVM like visitYield, visitSwitchExpression etc. Hence why we turn off the
79-
* --release flag (using USE_RELEASE_OPTION below)
80-
*/
81-
def compilerConfiguration = xml.asNode().component.find({ it.'@name' == 'CompilerConfiguration' })
82-
compilerConfiguration.value().add(new XmlParser().parseText('<option name="USE_RELEASE_OPTION" value="false" />'))
83-
def module = compilerConfiguration.bytecodeTargetLevel.module.find({ it.'@name' == 'palantir-java-format' })
84-
// Module is null on jdk11
85-
if (module != null) {
86-
module.attributes().target = "14"
87-
}
88-
89-
xml.asNode().value().add(new XmlParser().parseText('''
90-
<component name="JavacSettings">
91-
<option name="PREFER_TARGET_JDK_COMPILER" value="false" />
92-
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
93-
<module name="palantir-java-format" options="--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" />
94-
</option>
95-
</component>
96-
'''))
69+
javaVersions {
70+
libraryTarget = 17
71+
runtime = 17
9772
}
9873

9974
jdks {

changelog/@unreleased/pr-1226.v2.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Use modern infra to support multiple JDK versions
4+
links:
5+
- https://github.com/palantir/palantir-java-format/pull/1226

gradle/javadoc.options

Lines changed: 0 additions & 1 deletion
This file was deleted.

idea-plugin/build.gradle

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ dependencies {
6969
testImplementation 'org.junit.platform:junit-platform-launcher'
7070
}
7171

72-
tasks.withType(JavaCompile).configureEach {
73-
options.errorprone.disable 'StrictUnusedVariable'
74-
// opt out of safe-logging checks which don't map correctly to idea plugins
75-
options.errorprone.disable 'PreferSafeLogger'
76-
options.errorprone.disable 'PreferSafeLoggableExceptions'
77-
options.errorprone.disable 'PreferSafeLoggingPreconditions'
78-
}
79-
8072
// Javadoc fails if there are no public classes to javadoc, so make it stop complaining.
8173
tasks.named("javadoc", Javadoc) {
8274
failOnError = false

palantir-java-format-spi/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ dependencies {
1111
testImplementation 'org.junit.jupiter:junit-jupiter'
1212
testImplementation 'com.fasterxml.jackson.core:jackson-databind'
1313
}
14+
15+
tasks.withType(JavaCompile).named('compileJava') {
16+
sourceCompatibility = JavaVersion.VERSION_11
17+
targetCompatibility = JavaVersion.VERSION_11
18+
}

palantir-java-format/build.gradle

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,24 @@ dependencies {
3030
annotationProcessor 'org.derive4j:derive4j'
3131
}
3232

33-
def exports = [
34-
'jdk.compiler/com.sun.tools.javac.file',
35-
'jdk.compiler/com.sun.tools.javac.main',
36-
'jdk.compiler/com.sun.tools.javac.parser',
37-
'jdk.compiler/com.sun.tools.javac.tree',
38-
'jdk.compiler/com.sun.tools.javac.util',
39-
'jdk.compiler/com.sun.tools.javac.code',
40-
'jdk.compiler/com.sun.tools.javac.api'
41-
]
42-
43-
def jvmArgList = exports.collect { value -> "--add-exports=${value}=ALL-UNNAMED".toString() }
44-
45-
tasks.withType(JavaCompile).configureEach {
46-
options.errorprone.disable 'StrictUnusedVariable'
47-
48-
// Allow access to internal javac apis
49-
options.compilerArgs += jvmArgList
50-
51-
if (JavaVersion.current() < JavaVersion.VERSION_14) {
52-
excludes = ['**/Java14InputAstVisitor.java']
53-
}
54-
}
55-
56-
tasks.withType(Test).configureEach {
57-
jvmArgs = jvmArgList
58-
}
59-
60-
tasks.withType(Javadoc).configureEach {
61-
// Allow access to internal javac apis
62-
options.optionFiles << file('../gradle/javadoc.options')
63-
64-
if (JavaVersion.current() < JavaVersion.VERSION_14) {
65-
exclude '**/Java14InputAstVisitor.java'
66-
}
33+
moduleJvmArgs {
34+
exports().addAll(
35+
'jdk.compiler/com.sun.tools.javac.file',
36+
'jdk.compiler/com.sun.tools.javac.main',
37+
'jdk.compiler/com.sun.tools.javac.parser',
38+
'jdk.compiler/com.sun.tools.javac.tree',
39+
'jdk.compiler/com.sun.tools.javac.util',
40+
'jdk.compiler/com.sun.tools.javac.code',
41+
'jdk.compiler/com.sun.tools.javac.api')
42+
}
43+
44+
tasks.withType(JavaCompile).named('compileJava') {
45+
// By setting the source and target compatibility to 11, while still using a higher level
46+
// JDK for compilation, we can compile against the AST classes we need to support >11 source
47+
// features but still run on 11 JDKs (with the addition of checking the java version at runtime
48+
// and dynamically loading the correct formatter implementation)
49+
sourceCompatibility = JavaVersion.VERSION_11
50+
targetCompatibility = JavaVersion.VERSION_11
6751
}
6852

6953
// false positives due to org.junit.runners.* in the test cases
@@ -76,16 +60,3 @@ tasks.named("test") {
7660
// https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution
7761
systemProperty 'junit.jupiter.execution.parallel.mode.default', 'concurrent'
7862
}
79-
80-
// necessary to compile Java14InputAstVisitor
81-
idea {
82-
module.languageLevel = new org.gradle.plugins.ide.idea.model.IdeaLanguageLevel(14)
83-
}
84-
85-
// This block may be replaced by BaselineExportsExtension exports
86-
// once https://github.com/gradle/gradle/issues/18824 is resolved.
87-
tasks.named("jar", Jar) {
88-
manifest {
89-
attributes('Add-Exports': exports.join(' '))
90-
}
91-
}

palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
/**
5353
* Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified for Java 14.
5454
*/
55+
@SuppressWarnings("Since15")
5556
public class Java14InputAstVisitor extends JavaInputAstVisitor {
5657
private static final Method COMPILATION_UNIT_TREE_GET_MODULE =
5758
maybeGetMethod(CompilationUnitTree.class, "getModule");

0 commit comments

Comments
 (0)