Repository navigation
Test the visitor for JDK 17 on every JDK, and make it the base visitor - #294
Merged
Merged
Conversation
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).
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
The formatter picks its visitor by the JDK it runs on:
Java21InputAstVisitorfrom JDK 21,Java14InputAstVisitorfrom 14 to 20,JavaInputAstVisitorbelow. CI and every local build run on JDK 21 or later, so no test ever laid out code withJava14InputAstVisitoror the base as the chosen visitor, andFormatterCasesJava21Testran 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.
visitCompoundAssignmentran for no test at all, as no case had a+=.After this PR
test-cases-java17/, andtest-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.Formatterholds the JDK whose visitor lays out the code: the running one, unless a test names another through a package-private constructor.visitorFormakes the choice.FormatterCasesJava17Testrunstest-cases-java17/through the visitor of JDK 17 withformatSourceAndFixImports, whatever JDK runs the tests. BreakingJavaInputAstVisitor.variableName, whichJava21InputAstVisitoroverrides, fails 575 of its cases and none ofFormatterCasesJava21Test.FormatterVisitorTestpins which JDK gets which visitor.transient,volatile,synchronized,strictfporsealed, andfloat;@Foo();forloop with two updates and ado-whilewhose body is not a block;Java14InputAstVisitoris merged intoJavaInputAstVisitor, as google-java-format did in google/google-java-format@3356bd342 when JDK 17 became its floor.Java21InputAstVisitorextends the base.visitorForloads it by name from JDK 21 on and buildsJavaInputAstVisitorbelow.The output does not change:
java.baseandjdk.compilersources of JDK 21 and a sample of Gradle's cache of sources jars.Line coverage on JDK 21:
JavaInputAstVisitorandJava14InputAstVisitorwere 94.4% together; the merged class is 97.4%.Java21InputAstVisitorstays at 100%.import moduleandvalue, which only JDK 25 and 28 parse.Possible downsides?
test-cases-java17/run twice, once through each visitor: about 900 more dynamic tests, a few seconds.Java14InputAstVisitornow goes intoJavaInputAstVisitor. Ports from google-java-format, which has a singleJavaInputAstVisitortoday, land closer to their place than before.nativejobs check that the remaining reflection entry still loadsJava21InputAstVisitor.