Skip to content

Call the syntax tree APIs of JDK 17 directly - #295

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/remove-jdk-14-16-reflection
Nov 29, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/remove-jdk-14-16-reflection

Conversation

@abashev

@abashev abashev commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Before this PR

JavaInputAstVisitor and RemoveUnusedImports found the tree methods that JDKs older than 17 lack through reflection, with fallbacks for when they were missing:

  • CompilationUnitTree.getModule;
  • ClassTree.getPermitsClause;
  • BindingPatternTree.getVariable, falling back to the getType and getBinding of JDK 14 and 15;
  • CaseTree.getLabels, falling back to CaseTree.getExpressions.

JavaInputAstVisitor also kept copies of Flags.RECORD and Flags.COMPACT_RECORD_CONSTRUCTOR, with a TODO for when Java 11 support goes. Since #294 the visitor for JDKs older than 14 is gone, and no JDK the formatter runs on is older than 17, so the fallbacks never run: of the visitor's uncovered lines, most were these.

After this PR

  • Direct calls. The four methods are called directly, and the fallbacks and the method lookups are gone, as google-java-format did in google/google-java-format@c1ea933ba when it stopped supporting JDK 14.
  • Flags. Flags.RECORD and Flags.COMPACT_RECORD_CONSTRUCTOR come from javac. Their values are the same on JDK 17, 21 and 27.
  • RemoveUnusedImports keeps its visit of a case's labels. The TreeScanner of JDK 17 visits CaseTree.getExpressions, which leave out a pattern label and so the type it names. The one of JDK 21 visits the labels, so for now they are visited twice, which changes nothing. Its TODO says when the override can go.
  • What stays. The reflection for APIs newer than JDK 21: ImportTree.isModule, the value modifier, and getDiagnostics across JDK 25.

The output does not change:

  • The tests 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 of JavaInputAstVisitor on JDK 21 goes from 97.4% to 97.6%, and branch coverage from 92.7% to 93.4%.

Possible downsides?

JavaInputAstVisitor and RemoveUnusedImports looked up through reflection
the tree methods that JDKs older than 17 lack: CompilationUnitTree.getModule,
ClassTree.getPermitsClause, BindingPatternTree.getVariable and
CaseTree.getLabels, and fell back to the getType and getBinding of the
binding patterns of JDK 14 and 15, and to CaseTree.getExpressions. No JDK
the formatter runs on is older than 17, so they are called directly now and
the fallbacks are gone, as google-java-format did in c1ea933ba. Flags.RECORD
and Flags.COMPACT_RECORD_CONSTRUCTOR replace the copies kept for Java 11;
their values are the same on JDK 17, 21 and 27.

RemoveUnusedImports keeps its own visit of a case's labels: the TreeScanner
of JDK 17 visits CaseTree.getExpressions, which leave out a pattern label,
while that of JDK 21 visits the labels, so for a while they are visited
twice. javac 17 compiles the direct calls; getLabels and CaseLabelTree are a
reflective preview API there, which gives a warning only. The reflection for
APIs newer than JDK 21, ImportTree.isModule and the value modifier, stays.

The tests pass on JDK 21, 25 and 27, and a corpus of 58,017 files formats to
the same bytes as with 21.1.2.

This prepares a release line for Java 17 (#293).
@abashev
abashev merged commit 482aee2 into 21.X.X Nov 29, 2026
17 checks passed
@abashev
abashev deleted the abashev/remove-jdk-14-16-reflection branch November 29, 2026 20:17
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