google-java-format 2.0.0 replaces the formatter's layout engine, and makes some improvements to lambda formatting (#19). This release also adds an optional cleanup pass that makes small non-whitespace changes, which is enabled by default.
Because of these changes, 2.0.0 reformats more existing code than a typical release. We recommend reformatting your codebase in a single commit when upgrading.
Formatting changes
-
A lambda that is the only argument of a call is now kept on the same line as the call, instead of starting on a new line with an extra level of indentation (#19):
executor.submit(() -> { doSomething(); });
-
Text blocks are now indented as part of the main formatting pass, so their indentation is correct even with
--skip-reflowing-long-strings. -
Fixed indentation of comments after
assert, enum constants,forinitializers,tryresources, multi-catch clauses, and type arguments.
Cleanups
-
Added a
--cleanupflag, enabled by default, for small non-whitespace changes:- removing redundant semicolons
- removing empty annotation parentheses (
@Foo()→@Foo) - removing parentheses around a single untyped lambda parameter (
(x) -> ...→x -> ...) (#762)
Cleanups only apply to the ranges being formatted. Use
--cleanup=falseto only change whitespace (and the existing--skip-sorting-imports,--skip-removing-unused-imports, and--skip-reordering-modifiersflags for the other non-whitespace changes). -
When formatting a range with
--linesor--offsetand cleanups enabled, unformatted statements outside the range are re-indented to match the formatted output, so an edit that adds or removes a level of nesting doesn't leave the surrounding code misaligned. Blank lines left behind by cleanups are removed.
Configuration
--max-line-length=Nsets the column limit (default 100) (#425). (This flag was introduced in v1.37.0)
Language support
- Support value classes and value records (JEP 401: Value Objects (Preview))
Javadoc
- Strip trailing asterisks before
*/(#67). - Preserve relative indentation in
<pre>{@code ...}blocks without leading*. - Join hyphenated words broken across lines.
Long strings
- When formatting a range with
--linesor--offset, only long string literals in the requested range are reflowed (#1114). - Escaped backslashes (e.g.
\\tin a Windows path or a regular expression) are no longer split when reflowing long string literals (#1186, #1253, #1439). - Failures while reflowing long strings are reported as errors instead of crashing or hanging the CLI (#1439).
Other fixes
- Fixed formatting in the Eclipse plugin, which failed with
NoClassDefFoundErrorsince 1.36.0 because the plugin didn't bundlecommonmark(#1428). - The error for combining
--dry-runwith--replacenow names the right flag (#1094).
API changes
JavaFormatterOptions.Builder.cleanup(boolean)controls the new cleanup pass, and defaults totrue. This meansFormatter.formatSourcenow makes the non-whitespace changes listed above by default. Integrations that format code the user is editing, e.g. format-on-save in an IDE, should setcleanup(false); the IntelliJ plugin does this.Formatter.getFormatReplacementsis now strictly whitespace-only: it no longer reorders modifiers, and never applies cleanups. UseFormatter.formatSource(String, Collection<Range<Integer>>)to format character ranges with modifier reordering.JavaFormatterOptions.Stylebecame a record in 1.37.0. The enum-compatible methods, such asStyle.valueOf(String), have been restored for tools like Spotless that called them (#1478).- The previous layout engine's classes (
com.google.googlejavaformat.Doc,OpsBuilder, and related types) are no longer used to format Java, but are still included for other formatters that build on them.
Full Changelog: v1.37.0...v2.0.0