From 05ed5dc2bba00f64975176b0555186b0cd840c7a Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sun, 15 Nov 2026 00:06:15 +0300 Subject: [PATCH] Fix only the imports in the out-of-process fixImports BootstrappingFormatterService.fixImports and NativeImageFormatterService.fixImports ran Main with no flags, as formatSourceReflowStringsAndFixImports does: the imports were fixed, and the whole file was laid out and its long strings reflowed with them. FormatterServiceImpl.fixImports, in process, fixes only the imports. Both now pass --fix-imports-only. Nothing calls fixImports through these services today. An import optimizer in the IntelliJ plugin would, on every Optimize imports, and the one palantir-java-format had was removed as slow (#222). JvmFormatterServicesTest checks fixImports on the in-process and the bootstrapping services, and runs in every build; FormatterServicesTest, in the native jobs, checks it on the bootstrapping and native ones. Closes #221 --- .../BootstrappingFormatterService.java | 12 +++- .../NativeImageFormatterService.java | 15 +++-- .../bootstrap/FormatterServicesTest.java | 13 +++- .../bootstrap/JvmFormatterServicesTest.java | 61 +++++++++++++++++++ .../src/test/resources/fix-imports.input | 5 ++ .../src/test/resources/fix-imports.output | 5 ++ 6 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java create mode 100644 open-java-format-jdk-bootstrap/src/test/resources/fix-imports.input create mode 100644 open-java-format-jdk-bootstrap/src/test/resources/fix-imports.output diff --git a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java index 78b14e9ef..f9ad74638 100644 --- a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java +++ b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java @@ -64,7 +64,7 @@ public ImmutableList getFormatReplacements(String input, Collection @Override public String formatSourceReflowStringsAndFixImports(String input) { try { - return runFormatterCommand(input); + return runFormatterCommand(input, /* fixImportsOnly= */ false); } catch (IOException e) { throw new UncheckedIOException("Error running formatter command", e); } @@ -73,7 +73,7 @@ public String formatSourceReflowStringsAndFixImports(String input) { @Override public String fixImports(String input) throws FormatterException { try { - return runFormatterCommand(input); + return runFormatterCommand(input, /* fixImportsOnly= */ true); } catch (IOException e) { throw new UncheckedIOException("Error running formatter command", e); } @@ -86,6 +86,7 @@ private ImmutableList getFormatReplacementsInternal(String input, C jdkPath, jvmArgsForVersion(jdkMajorVersion), implementationClassPath, + /* fixImportsOnly= */ false, /* outputReplacements= */ true, ranges.stream().map(RangeUtils::toStringRange).toList()); @@ -97,11 +98,12 @@ private ImmutableList getFormatReplacementsInternal(String input, C return MAPPER.readValue(output.get(), new TypeReference<>() {}); } - private String runFormatterCommand(String input) throws IOException { + private String runFormatterCommand(String input, boolean fixImportsOnly) throws IOException { FormatterCliArgs command = new FormatterCliArgs( jdkPath, jvmArgsForVersion(jdkMajorVersion), implementationClassPath, + fixImportsOnly, /* outputReplacements= */ false, /* characterRanges= */ List.of()); return FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.ofNullable(jdkPath.getParent())) @@ -124,6 +126,7 @@ record FormatterCliArgs( Path jdkPath, List jvmArgs, List implementationClasspath, + boolean fixImportsOnly, boolean outputReplacements, List characterRanges) { @@ -137,6 +140,9 @@ List toArgs() { .collect(Collectors.joining(System.getProperty("path.separator")))); args.add(FORMATTER_MAIN_CLASS); + if (fixImportsOnly) { + args.add("--fix-imports-only"); + } if (!characterRanges.isEmpty()) { args.add("--character-ranges"); args.add(Joiner.on(',').join(characterRanges)); diff --git a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java index 25b08fdff..8a7fcbd88 100644 --- a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java +++ b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java @@ -49,6 +49,7 @@ public ImmutableList getFormatReplacements(String input, Collection try { FormatterNativeImageArgs command = new FormatterNativeImageArgs( nativeImagePath, + /* fixImportsOnly= */ false, /* outputReplacements= */ true, ranges.stream().map(RangeUtils::toStringRange).toList()); @@ -67,7 +68,7 @@ public ImmutableList getFormatReplacements(String input, Collection @Override public String formatSourceReflowStringsAndFixImports(String input) { try { - return runFormatterCommand(input); + return runFormatterCommand(input, /* fixImportsOnly= */ false); } catch (IOException e) { throw new UncheckedIOException("Error running the native image command", e); } @@ -76,26 +77,30 @@ public String formatSourceReflowStringsAndFixImports(String input) { @Override public String fixImports(String input) { try { - return runFormatterCommand(input); + return runFormatterCommand(input, /* fixImportsOnly= */ true); } catch (IOException e) { throw new UncheckedIOException("Error running the native image command", e); } } - private String runFormatterCommand(String input) throws IOException { + private String runFormatterCommand(String input, boolean fixImportsOnly) throws IOException { FormatterNativeImageArgs command = new FormatterNativeImageArgs( - nativeImagePath, /* outputReplacements= */ false, /* characterRanges= */ List.of()); + nativeImagePath, fixImportsOnly, /* outputReplacements= */ false, /* characterRanges= */ List.of()); return FormatterCommandRunner.runWithStdin( command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent())) .orElse(input); } - record FormatterNativeImageArgs(Path nativeImagePath, boolean outputReplacements, List characterRanges) { + record FormatterNativeImageArgs( + Path nativeImagePath, boolean fixImportsOnly, boolean outputReplacements, List characterRanges) { List toArgs() { var args = new ArrayList(); args.add(nativeImagePath.toAbsolutePath().toString()); + if (fixImportsOnly) { + args.add("--fix-imports-only"); + } if (!characterRanges.isEmpty()) { args.add("--character-ranges"); args.add(Joiner.on(',').join(characterRanges)); diff --git a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java index 5821fade2..86296aa04 100644 --- a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java +++ b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java @@ -63,6 +63,15 @@ void can_format_full_file(FormatterService formatterService) throws FormatterExc assertThat(formatted).isEqualTo(expectedOutput); } + @ParameterizedTest + @MethodSource("getFormatters") + void fixes_only_the_imports(FormatterService formatterService) throws FormatterException { + String input = getTestResourceContent("fix-imports.input"); + String expectedOutput = getTestResourceContent("fix-imports.output"); + + assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput); + } + @ParameterizedTest @MethodSource("getFormatters") void can_format_large_input_file(FormatterService formatterService) throws FormatterException { @@ -112,7 +121,7 @@ private String getTestResourceContent(String resourceName) { } } - private static List getClasspath() { + static List getClasspath() { String classpath = System.getProperty("java.class.path"); return Splitter.on(File.pathSeparatorChar) .trimResults() @@ -122,7 +131,7 @@ private static List getClasspath() { .toList(); } - private static Path javaBinPath() { + static Path javaBinPath() { String javaHome = Preconditions.checkNotNull(System.getProperty("java.home"), "java.home property not set"); return Path.of(javaHome).resolve("bin").resolve("java" + (isWindows() ? ".exe" : "")); } diff --git a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java new file mode 100644 index 000000000..12b0937d9 --- /dev/null +++ b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java @@ -0,0 +1,61 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.javaformat.bootstrap; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.palantir.javaformat.java.FormatterException; +import com.palantir.javaformat.java.FormatterService; +import com.palantir.javaformat.java.FormatterServiceImpl; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * The services that need no native binary: the one in this JVM and the one that starts a new JVM. Unlike + * {@link FormatterServicesTest}, this runs in every build. + */ +final class JvmFormatterServicesTest { + + @ParameterizedTest + @MethodSource("getFormatters") + void fixes_only_the_imports(FormatterService formatterService) throws FormatterException { + String input = getTestResourceContent("fix-imports.input"); + String expectedOutput = getTestResourceContent("fix-imports.output"); + + assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput); + } + + private static Stream getFormatters() { + return Stream.of( + new FormatterServiceImpl(), + new BootstrappingFormatterService( + FormatterServicesTest.javaBinPath(), + Runtime.version().feature(), + FormatterServicesTest.getClasspath())); + } + + private String getTestResourceContent(String resourceName) { + try { + URI resource = getClass().getClassLoader().getResource(resourceName).toURI(); + return Files.readString(Path.of(resource)); + } catch (Exception e) { + throw new RuntimeException("Error reading resource: " + resourceName, e); + } + } +} diff --git a/open-java-format-jdk-bootstrap/src/test/resources/fix-imports.input b/open-java-format-jdk-bootstrap/src/test/resources/fix-imports.input new file mode 100644 index 000000000..9ace817b5 --- /dev/null +++ b/open-java-format-jdk-bootstrap/src/test/resources/fix-imports.input @@ -0,0 +1,5 @@ +package p; +import java.util.Map; +import java.io.File; +import java.util.List; +class C { List l; Map m; } diff --git a/open-java-format-jdk-bootstrap/src/test/resources/fix-imports.output b/open-java-format-jdk-bootstrap/src/test/resources/fix-imports.output new file mode 100644 index 000000000..ed1fce7a5 --- /dev/null +++ b/open-java-format-jdk-bootstrap/src/test/resources/fix-imports.output @@ -0,0 +1,5 @@ +package p; +import java.util.List; +import java.util.Map; + +class C { List l; Map m; }