formatter-maven-plugin: Java `record` types being formatted incorrectly

Describe the bug After upgrading to 2.25.0, all Java record types are being reformatted.

For example, given the following record declaration:

public record FirstHeader(int totalBytes, int offset, int size, OffsetStatus status) {
    public boolean hasData() {
        return status == OffsetStatus.MORE_DATA_NEW_OFFSET || offset > 0;
    }
}

After running the formatted, the output yields:

public record FirstHeader(int totalBytes,int offset,int size,OffsetStatus status){

public boolean hasData(){return status==OffsetStatus.MORE_DATA_NEW_OFFSET||offset>0;}}

What is being lost here are the following:

  • Indentation to match the same indentation observed inside classes
  • Spaces after commas in the record’s definition
  • Spaces before { at the end of of declaration and methods
  • Spaces before and after operators like ||, ==, >.

Expected behavior

When the record is formatted using 2.24.1, we observe the correct formatting. But once we upgraded to 2.25.0, I’ve observed these differences. I’ve reviewed all formatter configuration settings that include “record” in their names, setting their values as follows:

<profiles version="12">
  <profile kind="CodeFormatterProfile" name="Debezium" version="12">
        <!-- Various other settings -->
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_record_components" value="16"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_record_declaration" value="16"/>
        <setting id="org.eclipse.jdt.core.formatter.brace_position_for_record_constructor" value="end_of_line"/>
        <setting id="org.eclipse.jdt.core.formatter.brace_position_for_record_declaration" value="end_of_line"/>
        <setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_record_header" value="true"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_record_components" value="insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_record_declaration" value="do not insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_record_declaration" value="do not insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_record_components" value="insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_record_constructor" value="insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_record_declaration" value="insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_record_declaration" value="do not insert"/>
        <setting id="org.eclipse.jdt.core.formatter.keep_record_constructor_on_one_line" value="one_line_never"/>
        <setting id="org.eclipse.jdt.core.formatter.keep_record_declaration_on_one_line" value="one_line_never"/>
        <setting id="org.eclipse.jdt.core.formatter.parentheses_positions_in_record_declaration" value="common_lines"/>
  </profile>
</profiles>

Despite those settings, the formatting with 2.25.0 is completely different than 2.24.1.

After performing a git bisect, the offending commit is https://github.com/revelc/formatter-maven-plugin/commit/df94a38e5c644d4171192d329a83dbbe2480377c but these changes do not seem related to Java record types.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (12 by maintainers)

Commits related to this issue

Most upvoted comments

So my issue with 2.26 stemmed from these being set to an older Java version:

        <setting id="org.eclipse.jdt.core.compiler.codegen.targetPlatform" value="21"/>
        <setting id="org.eclipse.jdt.core.compiler.compliance" value="21"/>
        <setting id="org.eclipse.jdt.core.compiler.source" value="21"/>

I suspect this may be the same reason for the initial problem, as I had no idea these were left to older versions 😦

Now that I am on 2.26, I’m going to check our full build and report back.

Tests are running, but all early indications this was my problem. They were originally nested in all the settings, so I did some rework on the XML and pulled these up so we don’t miss this going forward 👍