From dbd0dd7e2a21360f1f777756a82714c1ef9bea14 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sat, 14 Nov 2026 00:01:52 +0300 Subject: [PATCH 1/3] Make Main easier to read Blank lines between the steps of format() and processArgs(), var for the entry of the result loop and for the cause of a failed format, a static import of ofNullable, a pattern-matching instanceof for the FormatterException, and Files.writeString in place of getBytes(UTF_8), which writes the same bytes. The continue after a failed write goes: nothing in the loop body came after it, as Error Prone's RedundantControlFlow pointed out. --- .../com/palantir/javaformat/java/Main.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) 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 ca0373eb7..615c90c97 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 @@ -15,6 +15,7 @@ package com.palantir.javaformat.java; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Optional.ofNullable; import com.google.common.io.ByteStreams; import com.palantir.javaformat.java.JavaFormatterOptions.Style; @@ -30,7 +31,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -98,10 +98,12 @@ public static void main(String[] args) { */ public int format(String... args) throws UsageException { CommandLineOptions parameters = processArgs(args); + if (parameters.version()) { errWriter.println(versionString()); return 0; } + if (parameters.help()) { throw new UsageException(); } @@ -153,9 +155,10 @@ private int formatFiles( } } - for (Map.Entry> result : results.entrySet()) { + for (var result : results.entrySet()) { Path path = result.getKey(); String formatted; + try { formatted = result.getValue().get(); } catch (InterruptedException e) { @@ -163,21 +166,24 @@ private int formatFiles( allOk = false; continue; } catch (ExecutionException e) { - if (e.getCause() instanceof FormatterException) { - for (FormatterDiagnostic diagnostic : ((FormatterException) e.getCause()).diagnostics()) { + var cause = e.getCause(); + + if (cause instanceof FormatterException formatterException) { + for (FormatterDiagnostic diagnostic : formatterException.diagnostics()) { errWriter.println(path + ":" + diagnostic); } } else { errWriter.println(path + ": error: " - + Optional.ofNullable(e.getCause()) - .map(Throwable::getMessage) - .orElse("null")); - Optional.ofNullable(e.getCause()).ifPresent(cause -> cause.printStackTrace(errWriter)); + + ofNullable(cause).map(Throwable::getMessage).orElse("null")); + + ofNullable(cause).ifPresent(c -> c.printStackTrace(errWriter)); } allOk = false; continue; } + boolean changed = !formatted.equals(inputs.get(path)); + if (changed && parameters.setExitIfChanged()) { anyChanged = true; } @@ -186,11 +192,11 @@ private int formatFiles( continue; // preserve original file } try { - Files.write(path, formatted.getBytes(UTF_8)); + Files.writeString(path, formatted); } catch (IOException e) { errWriter.println(path + ": could not write file: " + e.getMessage()); + allOk = false; - continue; } } else if (parameters.dryRun()) { if (changed) { @@ -255,7 +261,9 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti t.printStackTrace(); throw new UsageException(Objects.requireNonNullElse(t.getMessage(), "null")); } + int filesToFormat = parameters.files().size(); + if (parameters.stdin()) { filesToFormat++; } @@ -281,6 +289,7 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti if (parameters.dryRun() && parameters.inPlace()) { throw new UsageException("cannot use --dry-run and --replace at the same time"); } + return parameters; } } From 9620991707a169331f48f2725392baff8d9bbe6c Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sat, 14 Nov 2026 00:01:53 +0300 Subject: [PATCH 2/3] Refuse a Java file that is not UTF-8 instead of rewriting it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit new String(bytes, UTF_8) turned each byte that is not UTF-8 into U+FFFD, and --replace wrote the result back: an "é" saved as Latin-1 became EF BF BD, and the exit code was 0. Files.readString throws on such a byte, so the file is reported as one Main could not read and stays as it is, with exit code 2. Standard input still decodes with replacement. --- .../src/main/java/com/palantir/javaformat/java/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 615c90c97..db801f7b6 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 @@ -146,7 +146,7 @@ private int formatFiles( Path path = Paths.get(fileName); String input; try { - input = new String(Files.readAllBytes(path), UTF_8); + input = Files.readString(path); inputs.put(path, input); results.put(path, executorService.submit(new FormatFileCallable(parameters, input, options))); } catch (IOException e) { From 5f28292cd3c77b769942897ea3dfbc56b129d0f6 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sat, 14 Nov 2026 00:01:54 +0300 Subject: [PATCH 3/3] Make Main.format and processArgs package-private Only Main.main and the tests of the package call them. Code outside the package that runs the command line in process, through new Main(out, err, in).format(args), no longer compiles; nothing in this repository, the Maven plugin or the GitHub Action does that. --- .../src/main/java/com/palantir/javaformat/java/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 db801f7b6..8dec1ae35 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 @@ -96,7 +96,7 @@ public static void main(String[] args) { * * @param args the command-line arguments */ - public int format(String... args) throws UsageException { + int format(String... args) throws UsageException { CommandLineOptions parameters = processArgs(args); if (parameters.version()) { @@ -251,7 +251,7 @@ private static int exitCode(boolean allOk, boolean anyChanged) { } /** Parses and validates command-line flags. */ - public static CommandLineOptions processArgs(String... args) throws UsageException { + static CommandLineOptions processArgs(String... args) throws UsageException { CommandLineOptions parameters; try { parameters = CommandLineOptionsParser.parse(Arrays.asList(args));