From fef9a8cdbf7facadd7286333369b3d992ba4b9db Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sun, 29 Nov 2026 23:41:32 +0300 Subject: [PATCH] Check the cases of Java 17 on every surface through the visitor for JDK 17 FormatterCasesJava17Test laid out test-cases-java17 through the visitor of JDK 17 with formatSourceAndFixImports only: the service and the command line create formatters of their own, which took the visitor of the running JDK. The other surfaces were checked by FormatterCasesJava21Test alone, on JDK 21 and later, so on JDK 17 nothing would check them. FormatterServiceImpl and Main now take the visitor's JDK through package-private constructors, the running one by default, and Main hands it to FormatFileCallable. FormatterCasesTest checks every surface with the visitor it is given: FormatterCasesJava17Test with the one of JDK 17, the classes for newer JDKs with the one of the JDK they run on, as before. FormatterVisitorTest shows that the service and the command line use the visitor they are given: on JDK 21 and later both format a guarded case label with the visitor of JDK 21 and fail on it with the one of JDK 17. FormatterCasesJava21Test keeps test-cases-java17: on JDK 21 and later the visitor of JDK 21 is the one that lays out that code. This prepares a release line for Java 17 (#293). --- docs/decisions/ADR-002-tests-by-jdk.md | 6 +- .../javaformat/java/FormatFileCallable.java | 9 ++- .../palantir/javaformat/java/Formatter.java | 4 +- .../javaformat/java/FormatterServiceImpl.java | 9 ++- .../com/palantir/javaformat/java/Main.java | 16 +++++- .../java/FormatterCasesJava17Test.java | 5 +- .../javaformat/java/FormatterCasesTest.java | 57 ++++++++++++------- .../javaformat/java/FormatterVisitorTest.java | 44 ++++++++++++++ 8 files changed, 117 insertions(+), 33 deletions(-) diff --git a/docs/decisions/ADR-002-tests-by-jdk.md b/docs/decisions/ADR-002-tests-by-jdk.md index 2af3e8d97..4161d534a 100644 --- a/docs/decisions/ADR-002-tests-by-jdk.md +++ b/docs/decisions/ADR-002-tests-by-jdk.md @@ -27,8 +27,8 @@ A test that needs a newer JDK says so on its class, and its case files live in a - **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, 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`. +- **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` on every surface the other classes check, laid out by the visitor of JDK 17 whatever JDK runs the test; on a JDK from 21 the classes above run the same folder through the visitor of that JDK, the one production uses there. +- **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. A test names the visitor's JDK through package-private constructors of `Formatter`, `FormatterServiceImpl` and `Main`, which hands it to `FormatFileCallable`. - **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. @@ -68,7 +68,7 @@ A test that needs a newer JDK says so on its class, and its case files live in a ## Consequences - `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. +- On JDK 21 and later the cases of `test-cases-java17/` run twice on every surface: through the visitor of JDK 17 and through the visitor of the running JDK. On JDK 17 to 20 they run once, through the visitor of JDK 17, which is the one of the running JDK there. - `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)`. diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java index ecf5f18b4..4f467b06d 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java @@ -34,11 +34,18 @@ class FormatFileCallable implements Callable { private final String input; private final CommandLineOptions parameters; private final JavaFormatterOptions options; + private final int visitorVersion; public FormatFileCallable(CommandLineOptions parameters, String input, JavaFormatterOptions options) { + this(parameters, input, options, Formatter.getRuntimeVersion()); + } + + /** Lays out the code with the visitor of JDK {@code visitorVersion}, see {@link Formatter#visitorFor}. */ + FormatFileCallable(CommandLineOptions parameters, String input, JavaFormatterOptions options, int visitorVersion) { this.input = input; this.parameters = parameters; this.options = options; + this.visitorVersion = visitorVersion; } @Override @@ -47,7 +54,7 @@ public String call() throws FormatterException { return fixImports(input); } - Formatter formatter = Formatter.createFormatter(options); + Formatter formatter = new Formatter(options, false, visitorVersion); if (parameters.outputReplacements()) { return writeFormatReplacements(replacements(formatter)); } diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java b/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java index fce181fe8..6e314ffa4 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java @@ -105,9 +105,9 @@ public final class Formatter { /** * 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. + * that a test can check the visitor of an older JDK on a newer one. The command line and the service hand it the + * version a test gives them. */ - @VisibleForTesting Formatter(JavaFormatterOptions options, boolean debugMode, int visitorVersion) { this.options = options; this.debugMode = debugMode; diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java index 41644d0f1..2040f2d95 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java @@ -16,6 +16,7 @@ package com.palantir.javaformat.java; import com.google.auto.service.AutoService; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.Range; import com.google.common.collect.TreeRangeSet; @@ -28,9 +29,15 @@ public final class FormatterServiceImpl implements FormatterService { private final Formatter formatter; public FormatterServiceImpl() { + this(Formatter.getRuntimeVersion()); + } + + /** A service that lays out the code with the visitor of JDK {@code visitorVersion}, see {@link Formatter}. */ + @VisibleForTesting + FormatterServiceImpl(int visitorVersion) { JavaFormatterOptions options = JavaFormatterOptions.builder().style(Style.OJF).build(); - formatter = Formatter.createFormatter(options); + formatter = new Formatter(options, false, visitorVersion); } @Override diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/Main.java b/open-java-format/src/main/java/com/palantir/javaformat/java/Main.java index 8dec1ae35..ea21f1e27 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/Main.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/Main.java @@ -17,6 +17,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Optional.ofNullable; +import com.google.common.annotations.VisibleForTesting; import com.google.common.io.ByteStreams; import com.palantir.javaformat.java.JavaFormatterOptions.Style; import java.io.IOException; @@ -57,11 +58,20 @@ static String versionString() { private final PrintWriter outWriter; private final PrintWriter errWriter; private final InputStream inStream; + /** The JDK whose visitor lays out the code: the running one, unless a test names another. */ + private final int visitorVersion; public Main(PrintWriter outWriter, PrintWriter errWriter, InputStream inStream) { + this(outWriter, errWriter, inStream, Formatter.getRuntimeVersion()); + } + + /** A command line that lays out the code with the visitor of JDK {@code visitorVersion}, see {@link Formatter}. */ + @VisibleForTesting + Main(PrintWriter outWriter, PrintWriter errWriter, InputStream inStream, int visitorVersion) { this.outWriter = outWriter; this.errWriter = errWriter; this.inStream = inStream; + this.visitorVersion = visitorVersion; } /** @@ -148,7 +158,9 @@ private int formatFiles( try { input = Files.readString(path); inputs.put(path, input); - results.put(path, executorService.submit(new FormatFileCallable(parameters, input, options))); + results.put( + path, + executorService.submit(new FormatFileCallable(parameters, input, options, visitorVersion))); } catch (IOException e) { errWriter.println(fileName + ": could not read file: " + e.getMessage()); allOk = false; @@ -220,7 +232,7 @@ private int formatStdin(CommandLineOptions parameters, JavaFormatterOptions opti boolean ok = true; boolean anyChanged = false; try { - String output = new FormatFileCallable(parameters, input, options).call(); + String output = new FormatFileCallable(parameters, input, options, visitorVersion).call(); boolean changed = !input.equals(output); if (changed && parameters.setExitIfChanged()) { anyChanged = true; diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesJava17Test.java b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesJava17Test.java index cc1892df4..ca3b9772f 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesJava17Test.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesJava17Test.java @@ -21,8 +21,9 @@ import org.junit.jupiter.api.parallel.ExecutionMode; /** - * The cases for the syntax of JDK 17, laid out by the visitor of JDK 17 on every JDK. {@link FormatterCasesJava21Test} - * and the classes for newer JDKs run the same folder through the visitor of the JDK they run on. + * The cases for the syntax of JDK 17, laid out by the visitor of JDK 17 on every surface and every JDK. + * {@link FormatterCasesJava21Test} and the classes for newer JDKs run the same folder through the visitor of the JDK + * they run on, the one that lays out the code there. */ @Execution(ExecutionMode.CONCURRENT) final class FormatterCasesJava17Test { diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java index 95c3863aa..dadac836c 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java @@ -41,48 +41,52 @@ * the command line prints, which the bootstrapping and native services run, and the command line printing text gives * what applying them gives. Adding a case is adding a file. The cases are in a folder of the JDK they need, and each * JDK has a class that runs the folders it can parse, such as {@link FormatterCasesJava21Test}; - * {@link FormatterCasesJava17Test} runs the cases of JDK 17 through the visitor of JDK 17 on every JDK. + * {@link FormatterCasesJava17Test} runs the cases of JDK 17 through the visitor of JDK 17, on every surface and every + * JDK. */ final class FormatterCasesTest { - private static final Formatter FORMATTER = Formatter.createFormatter( - JavaFormatterOptions.builder().style(Style.OJF).build()); - private static final FormatterService SERVICE = new FormatterServiceImpl(); private static final ObjectMapper MAPPER = JsonMapper.builder().addModule(new GuavaModule()).build(); - /** The tests of the case files under {@code folders} on the test classpath. */ + /** The tests of the case files under {@code folders} on the test classpath, with the visitor of the running JDK. */ static List casesIn(String... folders) throws Exception { + return casesThroughVisitorOf(Formatter.getRuntimeVersion(), folders); + } + + /** + * The tests of the case files under {@code folders}, laid out by the visitor of JDK {@code javaVersion} on every + * surface, whatever JDK runs them. + */ + static List casesThroughVisitorOf(int javaVersion, String... folders) throws Exception { + var formatter = + new Formatter(JavaFormatterOptions.builder().style(Style.OJF).build(), false, javaVersion); + FormatterService service = new FormatterServiceImpl(javaVersion); + return testsOf(folders, (folder, path, testCase) -> { - formatsTwice(FORMATTER, folder, path, testCase); + formatsTwice(formatter, folder, path, testCase); var lines = changedLines(testCase); var characters = lines.lowerEndpoint() + ":" + (lines.upperEndpoint() - 1); List replacements = MAPPER.readValue( - commandLine(testCase.before(), "--character-ranges", characters, "--output-replacements", "-"), + commandLine( + javaVersion, + testCase.before(), + "--character-ranges", + characters, + "--output-replacements", + "-"), new TypeReference<>() {}); - assertThat(SERVICE.getFormatReplacements(testCase.before(), List.of(lines))) + assertThat(service.getFormatReplacements(testCase.before(), List.of(lines))) .as(() -> folder + "/" + path + ", the in-process service on characters " + lines) .isEqualTo(replacements); - assertThat(commandLine(testCase.before(), "--character-ranges", characters, "-")) + assertThat(commandLine(javaVersion, testCase.before(), "--character-ranges", characters, "-")) .as(() -> folder + "/" + path + ", the command line printing characters " + lines) .isEqualTo(Utils.applyReplacements(testCase.before(), replacements)); }); } - /** - * The tests of the case files under {@code folders}, laid out by the visitor of JDK {@code javaVersion} whatever - * JDK runs them: the before block gives the after block, and formatting that again changes nothing. Only {@link - * Formatter#formatSourceAndFixImports}: the other surfaces choose the visitor the same way, and {@link #casesIn} - * checks them on the running JDK. - */ - static List casesThroughVisitorOf(int javaVersion, String... folders) throws Exception { - var formatter = - new Formatter(JavaFormatterOptions.builder().style(Style.OJF).build(), false, javaVersion); - return testsOf(folders, (folder, path, testCase) -> formatsTwice(formatter, folder, path, testCase)); - } - /** What one test checks of one case file. */ private interface CaseCheck { void check(String folder, String path, CaseFile testCase) throws Exception; @@ -168,12 +172,21 @@ private static Range changedLines(CaseFile testCase) { /** What {@code open-java-format} prints for the source on standard input. */ static String commandLine(String source, String... args) throws UsageException { + return commandLine(Formatter.getRuntimeVersion(), source, args); + } + + /** + * What {@code open-java-format} prints for the source on standard input, laid out by the visitor of JDK + * {@code visitorVersion}. + */ + private static String commandLine(int visitorVersion, String source, String... args) throws UsageException { var out = new StringWriter(); var err = new StringWriter(); var main = new Main( new PrintWriter(out, true), new PrintWriter(err, true), - new ByteArrayInputStream(source.getBytes(UTF_8))); + new ByteArrayInputStream(source.getBytes(UTF_8)), + visitorVersion); assertThat(main.format(args)).as(err::toString).isZero(); diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterVisitorTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterVisitorTest.java index 8c5511b54..74557f94a 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterVisitorTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterVisitorTest.java @@ -14,12 +14,19 @@ package com.palantir.javaformat.java; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.palantir.javaformat.OpsBuilder; import com.palantir.javaformat.java.JavaFormatterOptions.Style; import com.palantir.javaformat.java.java21.Java21InputAstVisitor; +import java.io.ByteArrayInputStream; +import java.io.PrintWriter; +import java.io.StringWriter; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; /** Which visitor lays out the code on which JDK: one for JDK 17 to 20, and {@link Java21InputAstVisitor} from 21. */ final class FormatterVisitorTest { @@ -40,6 +47,43 @@ void jdk17To20GetOneVisitorThatIsNotTheOneForJdk21() throws FormatterException { assertThat(visitorFor(20)).isExactlyInstanceOf(visitor.getClass()); } + /** + * A guard after a case label is syntax of JDK 21, which the visitor of JDK 17 does not lay out: the service and the + * command line fail on it with that visitor and format it with the visitor of JDK 21, so they lay out the code with + * the visitor they are given. + */ + @Test + @EnabledForJreRange(min = JRE.JAVA_21) + void theServiceAndTheCommandLineUseTheVisitorTheyAreGiven() throws Exception { + String guard = """ + class T { + int f(Object o) { + return switch (o) { + case String s when s.isEmpty() -> 0; + default -> 1; + }; + } + } + """; + + assertThat(new FormatterServiceImpl(21).formatSourceReflowStringsAndFixImports(guard)) + .contains("case String s when s.isEmpty() -> 0;"); + assertThatThrownBy(() -> new FormatterServiceImpl(17).formatSourceReflowStringsAndFixImports(guard)) + .isInstanceOf(FormatterException.class); + + assertThat(commandLineExitCode(21, guard)).isZero(); + assertThat(commandLineExitCode(17, guard)).isNotZero(); + } + + private static int commandLineExitCode(int visitorVersion, String source) throws UsageException { + var main = new Main( + new PrintWriter(new StringWriter(), true), + new PrintWriter(new StringWriter(), true), + new ByteArrayInputStream(source.getBytes(UTF_8)), + visitorVersion); + return main.format("-"); + } + private static JavaInputAstVisitor visitorFor(int javaVersion) throws FormatterException { return Formatter.visitorFor(javaVersion, new OpsBuilder(new JavaInput("class T {}")), OPTIONS); }