Skip to content

java.EclipseJdtFormatterStep sorts records as method. #2941

@jamesepatrick

Description

@jamesepatrick

Hey Spotless folks,

I believe I've found a bug regarding the java.EclipseJdtFormatterStep's treatment of record declaration when using the sort members mechanism. It appears to be treating the record declaration as a method.

Given the following pom

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>io.jpatrick</groupId>
  <artifactId>sortmembers</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>3.5.0</version>
        <configuration>
          <baseDir>${spotless.baseDir}</baseDir>
          <java>
            <includes>
              <include>**/*.java</include>
            </includes>
            <eclipse>
              <sortMembersEnabled>true</sortMembersEnabled>
              <sortMembersOrder>T,SF,SI,SM,F,I,C,M</sortMembersOrder>
              <sortMembersDoNotSortFields>true</sortMembersDoNotSortFields>
              <sortMembersVisibilityOrderEnabled>true</sortMembersVisibilityOrderEnabled>
              <sortMembersVisibilityOrder>B,V,R,D</sortMembersVisibilityOrder>
            </eclipse>
          </java>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

and the following unsorted class

public class SortMembersRecordBug {

	public void aMethod() {
	}

	public record ConversionType(String convertType, String extension) {
	}

	public void bMethod() {
	}

	public void zMethod() {
	}
}

Produces the following. The record definition moves between bMethod & zMethod.

public class SortMembersRecordBug {

	public void aMethod() {
	}

	public void bMethod() {
	}

	public record ConversionType(String convertType, String extension) {
	}

	public void zMethod() {
	}
}

When formatting this in Eclispe it produces the record declaration at the top.

public class SortMembersRecordBug {
  public record ConversionType(String convertType, String extension) {
  }

  public void aMethod() {
  }

  public void bMethod() {
  }

  public void zMethod() {
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions