Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
24 changes: 20 additions & 4 deletions docs/decisions/ADR-002-tests-by-jdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Proposed

## Date
2026-11-14
2026-11-14; amended 2026-11-29 with the cases of JDK 17 and the visitor that lays out the code on JDK 17 to 20 (#293)

## Context

Expand All @@ -18,13 +18,17 @@ Today such tests skip themselves from inside, in three ways:

On JDK 21, where a local build runs, these 14 tests count as skipped among 1,422, and nothing says a change broke them. That is how the first push of #220 passed locally and failed CI's JDK 25 to 28 builds on `ModuleImportTest`. The case files of `FormatterCasesTest`, in `test-cases/`, have no way to say which JDK they need, so a case of newer syntax cannot be written at all.

The amendment of 2026-11-29 prepares a release line for Java 17 (#293). The formatter picks its visitor by the JDK it runs on: `Java21InputAstVisitor` from JDK 21, another one below. Every test ran on JDK 21 or later, so the visitor for older JDKs formatted nothing in any test, and of the 918 case files in `test-cases-java21/` all but 27 hold no syntax newer than Java 17.

## Decision

A test that needs a newer JDK says so on its class, and its case files live in a folder of that JDK:

- **Case files by JDK, side by side.** `test-cases/` becomes `test-cases-java21/`, for any JDK from 21; cases that need JDK 25 go to `test-cases-java25/` beside it, those that need JDK 28 to `test-cases-java28/`, and so on, one level deep.
- **Case files by JDK, side by side.** A case goes to the folder of the oldest JDK whose javac parses it: `test-cases-java17/` for the syntax of Java 17, which every JDK the formatter runs on parses; `test-cases-java21/` for the syntax of JDK 21; `test-cases-java25/` for JDK 25, `test-cases-java28/` for JDK 28, and so on, one level deep.
- **Classes by JDK.** A test class that needs a newer Java carries `@EnabledForJreRange` with that version, as `min = JRE.JAVA_25` (JUnit 6.1.3 has constants up to `JRE.JAVA_28`). On an older JDK it reports as disabled; on that JDK and later it runs with the JDK 21 tests.
- **One class per JDK for the case folders, JDK 21 too.** Each names the folders its JDK parses, one, two or three of them, newest first, as `FormatterCasesJava25Test` runs `test-cases-java25` and `test-cases-java21`; each is enabled from its JDK on, `min = JRE.JAVA_21`, `JAVA_25` or `JAVA_28`, with no upper bound, so on a newer JDK the older folders run again in the newer classes. The code they share takes the folders as arguments.
- **One class per JDK for the case folders, JDK 21 too.** Each names the folders its JDK parses, newest first, as `FormatterCasesJava25Test` runs `test-cases-java25`, `test-cases-java21` and `test-cases-java17`; each is enabled from its JDK on, `min = JRE.JAVA_21`, `JAVA_25` or `JAVA_28`, with no upper bound, so on a newer JDK the older folders run again in the newer classes. The code they share takes the folders as arguments.
- **The cases of JDK 17 through the visitor of JDK 17, on every JDK.** `FormatterCasesJava17Test` carries no condition, as 17 is JUnit 6's floor and `min = JRE.JAVA_17` is an error there. It runs `test-cases-java17` with `formatSourceAndFixImports` of a `Formatter` that lays out the code with the visitor of JDK 17, whatever JDK runs the test; the other surfaces choose the visitor the same way, and the classes above check them on the visitor of the running JDK.
- **Visitors by JDK.** `JavaInputAstVisitor` lays out the code up to JDK 20; `Java21InputAstVisitor` extends it for the syntax of JDK 21 and is loaded by name, from JDK 21 on, so that its class, built on the syntax trees of JDK 21, is only loaded where they exist. `Formatter.visitorFor` makes the choice, and a test names the visitor's JDK through a package-private constructor of `Formatter`.
- **Module imports from JDK 25.** They parse from JDK 23 as a preview, and are final in 25; CI runs no 23 or 24.
- **Preview features by the version that previews them.** Value classes are a preview of JDK 28: their formatting case goes to `test-cases-java28/`, and the `ModifierOrderer` test to a class with `@EnabledForJreRange(min = JRE.JAVA_28)`. CI's JDK 28 job runs early-access builds, which have the `value` modifier.
- **No assumption on the runtime inside a test.** `assumeTrue` on the runtime and `VERSIONED_TESTS` go.
Expand All @@ -51,9 +55,21 @@ A test that needs a newer JDK says so on its class, and its case files live in a
- Cons: a second kind of condition, and a case file cannot carry it; the early-access builds of JDK 28, where the feature is previewed, have the modifier.
- Rejected: preview features go by the version, as the rest.

### The cases of JDK 17 through the visitor of the running JDK only
- Pros: no seam in `Formatter`; every case runs through the visitor production uses on that JDK.
- Cons: CI runs on JDK 21 and later only, so the visitor of JDK 17 to 20 would still format nothing in a test; a change that breaks it would pass.
- Rejected: the visitor of each JDK is tested on every JDK, as its cases are.

### One visitor for JDK 17 and 21, as google-java-format did in 7e7242231
- Pros: one class, the shape of google-java-format today.
- Cons: javac 17 cannot compile it, and every call to an API of javac 21, as `CaseTree.getGuard()`, has to check the JDK by hand, or it fails on JDK 17 with `NoSuchMethodError`. google-java-format merged its visitor for JDK 21 only when it raised its floor to 21.
- Rejected: two classes while a line runs on JDK 17; one when it no longer does.

## Consequences

- `FormatterCasesTest` keeps the code that runs the cases; `FormatterCasesJava21Test`, `FormatterCasesJava25Test` and `FormatterCasesJava28Test` run them. A new JDK folder means a new class that names it and the older ones. The comments that name `test-cases/`, in `Level.java` and `RemoveUnusedImportsTest`, change with it.
- `FormatterCasesTest` keeps the code that runs the cases; `FormatterCasesJava17Test`, `FormatterCasesJava21Test`, `FormatterCasesJava25Test` and `FormatterCasesJava28Test` run them. A new JDK folder means a new class that names it and the older ones. The comments that name the folders, in `Level.java` and `RemoveUnusedImportsTest`, change with them, and so does the folder list of the WebAssembly module's differential test.
- On JDK 21 and later the cases of `test-cases-java17/` run twice: through the visitor of JDK 17 and, on every surface, through the visitor of the running JDK.
- `FormatterVisitorTest` pins which JDK gets which visitor. `Java14InputAstVisitor`, the visitor of JDK 14 to 20 in palantir-java-format, is merged into `JavaInputAstVisitor`, as google-java-format did in 3356bd342, and the visitor for JDKs older than 14 is gone.
- The `ModuleImport` pair of `FormatterIntegrationTest` becomes a case in `test-cases-java25/`; `ModuleImportTest` and the module-import test of `RemoveUnusedImportsTest` carry `@EnabledForJreRange(min = JRE.JAVA_25)`.
- The value-class test of `FormatterTest` becomes a case in `test-cases-java28/`; the one of `ModifierOrdererTest` moves to a class with `@EnabledForJreRange(min = JRE.JAVA_28)`.
- On JDK 23 and 24 the module-import tests no longer run.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,6 @@
}
]
},
{
"type": "com.palantir.javaformat.java.java14.Java14InputAstVisitor",
"methods": [
{
"name": "<init>",
"parameterTypes": [
"com.palantir.javaformat.OpsBuilder",
"int"
]
}
]
},
{
"type": "com.palantir.javaformat.java.java21.Java21InputAstVisitor",
"methods": [
Expand Down
1 change: 1 addition & 0 deletions open-java-format-wasm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def wasmDifferentialTest = tasks.register('wasmDifferentialTest', Exec) {
javaToolchains.launcherFor(oracleGraalVm).get().executablePath.asFile.path,
allJar.get().archiveFile.get().asFile.path,
layout.buildDirectory.dir('wasm-differential').get().asFile.path,
project(':open-java-format').file('src/test/resources/test-cases-java17').path,
project(':open-java-format').file('src/test/resources/test-cases-java21').path,
project(':open-java-format').file('src/test/resources/test-cases-java25').path,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ case BreakOnlyIfInnerLevelsThenFitOnOneLine(boolean keepIndentWhenInlined) -> {
// levels that are meant to look good even if partially inlined, e.g. method
// chains, which will catch things like builders, but not other kinds of levels like
// constant expressions.
// See the palantir-expression-lambdas cases in test-cases-java21/testdata for an example of
// See the palantir-expression-lambdas cases in test-cases-java17/testdata for an example of
// what this is trying to avoid.

// For this, need to actually check the last inner level of `lastLevel` (2 levels down).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,23 @@ public final class Formatter {

private final JavaFormatterOptions options;
private final boolean debugMode;
/** The JDK whose visitor lays out the code: the running one, unless a test names another. */
private final int visitorVersion;

@VisibleForTesting
Formatter(JavaFormatterOptions options, boolean debugMode) {
this(options, debugMode, getRuntimeVersion());
}

/**
* A formatter that lays out the code with the visitor of JDK {@code visitorVersion}, whatever JDK it runs on, so
* that a test can check the visitor of an older JDK on a newer one.
*/
@VisibleForTesting
Formatter(JavaFormatterOptions options, boolean debugMode, int visitorVersion) {
this.options = options;
this.debugMode = debugMode;
this.visitorVersion = visitorVersion;
}

/** A new Formatter instance with default options. */
Expand All @@ -124,10 +136,15 @@ JavaFormatterOptions options() {
* @param options the {@link JavaFormatterOptions}
* @param commentsHelper the {@link CommentsHelper}, used to rewrite comments
* @param debugMode whether to produce debugging output via {@link DebugRenderer}
* @param visitorVersion the JDK whose visitor lays out the code, see {@link #visitorFor}
* @return javaOutput the output produced
*/
static JavaOutput format(
final JavaInput javaInput, JavaFormatterOptions options, CommentsHelper commentsHelper, boolean debugMode)
final JavaInput javaInput,
JavaFormatterOptions options,
CommentsHelper commentsHelper,
boolean debugMode,
int visitorVersion)
throws FormatterException {

Context context = new Context();
Expand All @@ -139,15 +156,7 @@ static JavaOutput format(
javaInput.setCompilationUnit(unit);
OpsBuilder opsBuilder = new OpsBuilder(javaInput);

JavaInputAstVisitor visitor;
if (getRuntimeVersion() >= 21) {
visitor = createVisitor("com.palantir.javaformat.java.java21.Java21InputAstVisitor", opsBuilder, options);
} else if (getRuntimeVersion() >= 14) {
visitor = createVisitor("com.palantir.javaformat.java.java14.Java14InputAstVisitor", opsBuilder, options);
} else {
visitor = new JavaInputAstVisitor(opsBuilder, options.indentationMultiplier());
}

JavaInputAstVisitor visitor = visitorFor(visitorVersion, opsBuilder, options);
visitor.scan(unit, null);
opsBuilder.sync(javaInput.getText().length());
opsBuilder.drain();
Expand Down Expand Up @@ -242,6 +251,19 @@ static int getRuntimeVersion() {
return Runtime.version().feature();
}

/**
* The visitor that lays out the code on JDK {@code javaVersion}: {@link JavaInputAstVisitor} up to JDK 20, and from
* JDK 21 the visitor for its syntax. That one is loaded by name, so that its class, built on the syntax trees of
* JDK 21, is only loaded where the JDK has them.
*/
@VisibleForTesting
static JavaInputAstVisitor visitorFor(int javaVersion, OpsBuilder builder, JavaFormatterOptions options) {
if (javaVersion >= 21) {
return createVisitor("com.palantir.javaformat.java.java21.Java21InputAstVisitor", builder, options);
}
return new JavaInputAstVisitor(builder, options.indentationMultiplier());
}

private static JavaInputAstVisitor createVisitor(
final String className, final OpsBuilder builder, final JavaFormatterOptions options) {
try {
Expand Down Expand Up @@ -359,7 +381,7 @@ public List<Replacement> getFormatReplacements(String input, Collection<Range<In
JavaCommentsHelper commentsHelper = new JavaCommentsHelper(javaInput, options);
JavaOutput javaOutput;
try {
javaOutput = format(javaInput, options, commentsHelper, debugMode);
javaOutput = format(javaInput, options, commentsHelper, debugMode, visitorVersion);
} catch (FormattingError e) {
throw new FormatterException(e.diagnostics());
}
Expand Down
Loading
Loading