Repository navigation
Call the syntax tree APIs of JDK 17 directly - #295
Merged
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before this PR
JavaInputAstVisitorandRemoveUnusedImportsfound 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 thegetTypeandgetBindingof JDK 14 and 15;CaseTree.getLabels, falling back toCaseTree.getExpressions.JavaInputAstVisitoralso kept copies ofFlags.RECORDandFlags.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
Flags.RECORDandFlags.COMPACT_RECORD_CONSTRUCTORcome from javac. Their values are the same on JDK 17, 21 and 27.RemoveUnusedImportskeeps its visit of a case's labels. TheTreeScannerof JDK 17 visitsCaseTree.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.ImportTree.isModule, thevaluemodifier, andgetDiagnosticsacross JDK 25.The output does not change:
java.baseandjdk.compilersources of JDK 21 and a sample of Gradle's cache of sources jars.Line coverage of
JavaInputAstVisitoron JDK 21 goes from 97.4% to 97.6%, and branch coverage from 92.7% to 93.4%.Possible downsides?
CaseTree.getLabelsandCaseLabelTreeare a reflective preview API there. A build that compiles these sources with javac 17 gets two[preview]warnings. That is a warning, not an error, and the methods exist at run time on JDK 17.