revelc/formatter-maven-plugin

Plugin does not know what to do when records have overridden getters

Closed this issue · 2 comments

In case a Java 14 record has an overridden implementation of a getter, the plugin doesn't know how to format it and ends up just cutting spaces and leaving everything on the same line

Below is an example of the modifications performed by the plugin:

image

  • formatter-maven-plugin version 2.20.0
  • Java version OpenJDK 17 2021-09-14
  • Apache Maven version 3.6.3
  • Using IntelliJ 2022.1.1

To Reproduce

Just create a record (Java 14+) that overwrites a getter. The snippet below can be used as example:

public record ReceiveEndorsementCmd(
        String id,
        boolean isAuthorized,
        List<String>errorMessages) {
    @Override public List<String>errorMessages() {
        return errorMessages == null ? new ArrayList<>() : errorMessages;
    }
}

Expected behavior

I don't know, something nicer than putting everything together on the same line cutting the spaces

Thank you! :)

This formatter plugin merely runs the upstream Eclipse formatter library using the user's provided configuration file. It does not control the specific formatting options that make it into the upstream Eclipse JDT formatter.

If this is not currently supported by the Eclipse formatter, then this is not a bug for this plugin, but a feature request you should file upstream in the Eclipse JDT project.

If this is supported by the Eclipse formatter, but does not work using this plugin, then only then could this be a bug for this project.

I advise investigating whether you can achieve your desired formatting within Eclipse, and then export that configuration for use with this plugin. If Eclipse is not capable of achieving your preferred formatting, then you can request that feature upstream at https://bugs.eclipse.org/ for the Eclipse JDT project. Once the feature is available in Eclipse, then this plugin will automatically gain that ability when we routinely upgrade that that newer Eclipse JDT core jar as our dependency.

I had the same problem, but couldn't afford yet to find a solution.
In my case I just added it to the exclude config.

      <configuration>
          <configFile>${project.basedir}/eclipse-formatter.xml</configFile>
          <excludes>
              <exclude>**/ChannelName.java</exclude>
          </excludes>
      </configuration>