Skip to content

Test the visitor for JDK 17 on every JDK, and make it the base visitor - #294

Merged
abashev merged 5 commits into
21.X.Xfrom
abashev/visitor-for-java-17
Nov 29, 2026
Merged

abashev merged 5 commits into
21.X.Xfrom
abashev/visitor-for-java-17

Conversation

@abashev

@abashev abashev commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Before this PR

The formatter picks its visitor by the JDK it runs on: Java21InputAstVisitor from JDK 21, Java14InputAstVisitor from 14 to 20, JavaInputAstVisitor below. CI and every local build run on JDK 21 or later, so no test ever laid out code with Java14InputAstVisitor or the base as the chosen visitor, and FormatterCasesJava21Test ran its 918 case files on JDK 21 and later only, although 891 of them hold no syntax newer than Java 17. A release line for Java 17 (#293) would format through exactly that untested visitor.

Coverage of the core's tests on JDK 21 also found code that no case reached. visitCompoundAssignment ran for no test at all, as no case had a +=.

After this PR

  • Cases by JDK. The case files that javac 17 parses move to test-cases-java17/, and test-cases-java21/ keeps the 9 cases (27 files) of newer syntax. The classes for JDK 21, 25 and 28 run the new folder after their own, so every case still runs wherever it ran before. The WebAssembly module's differential test gets the folder too.
  • The visitor for JDK 17, tested on every JDK. Formatter holds the JDK whose visitor lays out the code: the running one, unless a test names another through a package-private constructor. visitorFor makes the choice. FormatterCasesJava17Test runs test-cases-java17/ through the visitor of JDK 17 with formatSourceAndFixImports, whatever JDK runs the tests. Breaking JavaInputAstVisitor.variableName, which Java21InputAstVisitor overrides, fails 575 of its cases and none of FormatterCasesJava21Test. FormatterVisitorTest pins which JDK gets which visitor.
  • Seven new cases cover what no case reached:
    • every compound assignment operator;
    • declarations that start with transient, volatile, synchronized, strictfp or sealed, and float;
    • an annotation element with an array default and @Foo();
    • an enum and a record with two interfaces, and a record with a static field;
    • a for loop with two updates and a do-while whose body is not a block;
    • type annotations on array dimensions and on varargs.
  • One visitor up to JDK 20. Java14InputAstVisitor is merged into JavaInputAstVisitor, as google-java-format did in google/google-java-format@3356bd342 when JDK 17 became its floor.
    • Its methods replace, in place, those of the base they overrode. Its reflection for JDK 14 to 16 moves as it is.
    • Java21InputAstVisitor extends the base. visitorFor loads it by name from JDK 21 on and builds JavaInputAstVisitor below.
    • The visitor for JDKs older than 14 is gone, and so is the native image's reflection entry for the removed class.
  • ADR-002 describes the folders, the class for JDK 17 and the visitors by JDK. It also records the two alternatives turned down:
    • running the cases of JDK 17 through the running JDK's visitor only;
    • one visitor for every JDK, which javac 17 cannot compile and which has to guard every API of javac 21 by hand.

The output does not change:

  • The tests did not change in the merge, and they pass on JDK 21, 25 and 27.
  • A corpus of 58,017 files formats to the same bytes as with 21.1.2. The corpus is the java.base and jdk.compiler sources of JDK 21 and a sample of Gradle's cache of sources jars.

Line coverage on JDK 21:

  • JavaInputAstVisitor and Java14InputAstVisitor were 94.4% together; the merged class is 97.4%.
  • Java21InputAstVisitor stays at 100%.
  • What is left uncovered: the reflection for JDK 14 to 16, defensive throws, three enums that nothing uses, and import module and value, which only JDK 25 and 28 parse.

Possible downsides?

  • On JDK 21 and later the cases of test-cases-java17/ run twice, once through each visitor: about 900 more dynamic tests, a few seconds.
  • A change ported from palantir-java-format's Java14InputAstVisitor now goes into JavaInputAstVisitor. Ports from google-java-format, which has a single JavaInputAstVisitor today, land closer to their place than before.
  • The native image was not built locally. CI's native jobs check that the remaining reflection entry still loads Java21InputAstVisitor.

The cases in test-cases-java21 were run on JDK 21 and later only, and most of
them hold no syntax newer than Java 17. Every case that javac 17 parses now
lives in test-cases-java17, under the same subfolders; test-cases-java21 keeps
the 9 cases (27 files) whose syntax javac 17 does not parse: CompactSource,
ojf-issue-24-jbang-compact-source, I1309, SwitchDouble, SwitchGuardClause,
SwitchRecord, SwitchUnderscore, Unnamed and UnnamedPattern.

The classes for JDK 21, 25 and 28 run the new folder after their own, so every
case still runs on every JDK it ran on before: 918 on JDK 21, 919 on JDK 25.
The wasm differential test gets the new folder too.

This prepares a release line for Java 17 (#293).
The formatter picks its visitor by the JDK it runs on: Java21InputAstVisitor
from JDK 21, Java14InputAstVisitor below. Every test ran on JDK 21 or later,
so the visitor for older JDKs never formatted anything in a test, and
nothing would notice a change that breaks it.

Formatter now takes the visitor's JDK as a field, the running one unless a
test names another through a package-private constructor, and chooses the
visitor in visitorFor. FormatterCasesJava17Test runs test-cases-java17 through
the visitor of JDK 17 on whatever JDK runs the tests, with
formatSourceAndFixImports; the other surfaces choose the visitor the same way
and FormatterCasesJava21Test checks them. All 891 cases pass on JDK 21, 25 and
27; a change to JavaInputAstVisitor.variableName, which Java21InputAstVisitor
overrides, fails 575 of them and none of FormatterCasesJava21Test.
FormatterVisitorTest pins which JDK gets which visitor.

This prepares a release line for Java 17 (#293).
Coverage of the core's tests on JDK 21 found code of JavaInputAstVisitor
and Java14InputAstVisitor that no test ran: visitCompoundAssignment ran for
no case at all, as no case had a +=, and neither did a declaration that
starts with transient, volatile, synchronized, strictfp or sealed, the float
type, an annotation element with an array default, an annotation with empty
parentheses, an enum or a record with two interfaces, a for loop with two
updates, a do-while whose body is not a block, nor a record with a static
field.

Seven cases in test-cases-java17 cover them, with the output of the current
formatter; they run through the visitor of JDK 17 and, in
FormatterCasesJava21Test, on every surface. Line coverage of
JavaInputAstVisitor goes from 94.6% to 95.9% and of Java14InputAstVisitor
from 91.9% to 93.5%; Java21InputAstVisitor stays at 100%. What is left in
Java14InputAstVisitor is the reflection for JDK 14 to 16 and defensive
throws.

This prepares a release line for Java 17 (#293).
JavaInputAstVisitor laid out the code of JDKs older than 14, and
Java14InputAstVisitor extended it for every JDK from 14 to 20. No JDK the
formatter runs on is older than 17, so the two are one class now, under the
name of the base, as google-java-format did in 3356bd342 when JDK 17 became
its floor. Java14InputAstVisitor's methods replace the ones of the base they
overrode, in place; those the base did not have sit next to the code they
belong with. Its reflection for JDK 14 to 16 moves as it is.

Java21InputAstVisitor extends JavaInputAstVisitor. Formatter.visitorFor
loads it by name from JDK 21 on and builds JavaInputAstVisitor below; the
visitor for JDKs older than 14 is gone, and so is the native image's
reflection entry for the removed class.

The tests did not change and pass on JDK 21, 25 and 27. Line coverage of the
merged class is 97.4%, against 95.8% for the two classes before: the code it
no longer has is the base's versions of the methods Java14InputAstVisitor
overrode, which ran for no case. A corpus of 58,017 files, the java.base and
jdk.compiler sources of JDK 21 and Gradle's cache of sources jars, formats to
the same bytes as with 21.1.2.

This prepares a release line for Java 17 (#293).
A case goes to the folder of the oldest JDK whose javac parses it, now from
test-cases-java17 up. FormatterCasesJava17Test runs that folder through the
visitor of JDK 17 on every JDK, without a condition, as 17 is JUnit 6's floor;
the classes for newer JDKs run it after their own folders, through the
visitor of the JDK they run on. JavaInputAstVisitor lays out the code up to
JDK 20, Java21InputAstVisitor from 21, loaded by name.

The ADR records the two alternatives turned down: running the cases of JDK 17
through the running JDK's visitor only, which leaves the visitor of JDK 17 to
20 untested as CI runs on 21 and later; and one visitor for every JDK, which
javac 17 cannot compile and which has to guard every API of javac 21 by hand.

This prepares a release line for Java 17 (#293).
@abashev
abashev merged commit 3940645 into 21.X.X Nov 29, 2026
17 checks passed
@abashev
abashev deleted the abashev/visitor-for-java-17 branch November 29, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant