Skip to content

Make Main easier to read - #226

Open
abashev wants to merge 3 commits into
21.X.Xfrom
abashev/Make-Main-easier-to-read
Open

abashev wants to merge 3 commits into
21.X.Xfrom
abashev/Make-Main-easier-to-read

Conversation

@abashev

@abashev abashev commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Readability changes to Main, and two changes that come with them, each in a commit of its own so that either can be dropped.

  • Easier to read. Blank lines between the steps of format() and processArgs(), var for the entry of the result loop and the cause of a failed format, a static import of ofNullable, a pattern-matching instanceof for the FormatterException, and Files.writeString, which writes the same bytes as getBytes(UTF_8). The continue after a failed write goes: it was the last statement of the loop body (Error Prone's RedundantControlFlow).
  • A file that is not UTF-8 is refused, not rewritten. Files.readString replaces new String(Files.readAllBytes(path), UTF_8), which turned each byte that is not UTF-8 into U+FFFD.
  • Main.format and Main.processArgs become package-private. Only Main.main and the tests of the package call them.

What changes for users

  • --replace on a file saved as Latin-1, with é in a comment: 21.0.2 writes the file back with EF BF BD in place of the é and exits with 0. Now Main prints <file>: could not read file: Input length = 3, leaves the file as it is and exits with 2. Standard input still decodes with replacement.
  • Code outside the package that runs the command line through new Main(out, err, in).format(args) no longer compiles. Nothing in this repository, the Maven plugin or the GitHub Action does.

Tests

On JDK 21, :open-java-format:check passes on each of the three commits, with no new warning in Main; 1,422 tests.

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.
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.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant