GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix: sort members treats record declarations as types by jamesepatrick · Pull Request #2942 · diffplug/spotless

Conversation

@jamesepatrick

@jamesepatrick jamesepatrick commented May 20, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

See #2941

ASTNode.RECORD_DECLARATION was missing from the switch statement in DefaultJavaElementComparator.category(), causing nested record declarations to fall through to the return 0 fallback instead of returning TYPE_INDEX. This placed records in the wrong sort category (near methods) rather than with other type declarations (T).

The same node type was also missing from the secondary sort switch in compare(), which handles name-based ordering within a category.

I did not add any tests for this as I could not find test class for this class.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jamesepatrick
Fix: sort members treats record declarations as types (diffplug#2941)
7919ae4
ASTNode.RECORD_DECLARATION was missing from the switch statement in
DefaultJavaElementComparator.category(), causing nested record
declarations to fall through to the `return 0` fallback instead of
returning TYPE_INDEX. This placed records in the wrong sort
category (near methods) rather than with other type declarations (T).

The same node type was also missing from the secondary sort switch in
compare(), which handles name-based ordering within a category.

I did not add any tests for this are there were not tests for the
Eclipse formatter.
@jamesepatrick jamesepatrick changed the title Fix: sort members treats record declarations as types (#2941) Fix: sort members treats record declarations as types May 20, 2026
@jamesepatrick
jamesepatrick marked this pull request as draft May 20, 2026 13:52
@jamesepatrick

jamesepatrick commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

Please hold. I believe my gotcha may be incorrect. Verifying results.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jamesepatrick

jamesepatrick commented May 21, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

My theory of this bug was incorrect. I had assumed that because this was hitting return 0; // should never happen code that this was the source of the issue.

The TL;DR is the EclipseJdtSortMembers leverages eclipse's jdt internals to handle sorting. The ComplicationUnit use by the sort needs to have JavaProject to function; we presently stub in an empty project. Since the Java compiler & compliance version is not defined it looks to be defaulting version 1.8 (see docs). This would also explain why the formatter also appears to have issues with sealed classes.

As a quick validation I hard-coded support for java 17.

diff --git i/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java w/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java
index a50c41a45..f40f096ae 100644
--- i/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java
+++ w/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java
@@ -28,6 +28,7 @@ import org.eclipse.jdt.core.IBuffer;
 import org.eclipse.jdt.core.IBufferChangedListener;
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.IOpenable;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.core.dom.AST;
@@ -220,7 +221,9 @@ public final class EclipseJdtSortMembers {
 
 		@Override
 		public Map<String, String> getOptions(boolean inheritJavaCoreOptions) {
-			return Map.of();
+			return Map.of(
+					JavaCore.COMPILER_SOURCE, JavaCore.VERSION_17,
+					JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_17);
 		}
 	}

Results in the correct sort order

public class SortMembersRecordBug {

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

  public void aMethod() {
  }

  public void bMethod() {
  }

  public void zMethod() {
  }
}

So there are a couple of different potential options.

1. Hard code in support for latest supported version

The simplest answer would be to use JavaCore.latestSupportedJavaVersion() to use the most recent version of Java that the bundled JDT supports. Since java, to the best of my knowledge, doesn't remove language features the formatter logic for 25 should be backwards compatible with 1.4 for syntactically valid java. I am a bit worried how JDT would handle using a newer version of java than was is presently installed.

2. Let the user specify it

We could let user define the it in the configuration file. For examplepom.xml

<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>
        <compilerSourceVersion>17</compilerSourceVersion> <!-- added -->
        <compilerComplianceVersion>17</compilerComplianceVersion> <!-- added -->
        <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>

3. Use the version of java that the formatter is being run with

I think it would be a reasonable assumption that the version number used to run spotless is a good version to run the JavaProject as. If we want to support 1.8 and earlier then we'd need to use the System.getProperty("java.version") rather than Runtime.version().


I think the best course of action is to realistically is an all of the above.

  • Use the user specified version if provided. If no user specified version is provided default version being used to run the formatter.
  • If version is greater than JavaCore.latestSupportedJavaVersion() use JavaCore.latestSupportedJavaVersion()
  • Verify version exists in JavaCore.getAllVersions(). Then use that.

I should have time to add this tomorrow. I'm not really sure how the properties get passed around so I may forego implementing that part.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jamesepatrick and others added 3 commits May 25, 2026 23:11
@jamesepatrick
Merge branch 'diffplug:main' into main
e31c671
@jamesepatrick
set compiler source/compliance options for sort members
bffaa81
Updated logic that attempt best guess at java version for the
COMPILER_SOURCE & COMPILER_COMPLIANCE to currently running version of
java. These fields had been left empty resulting in the AST Nodes unable
to correctly understand the syntax of language features past 1.8.

Now resolves the Java runtime version and maps it to the appropriate
JavaCore compiler source/compliance version constants, capping at the
latest version supported by the Eclipse JDT when the runtime is newer.
@jamesepatrick
Updated CHANGES.md files
01ef5de
@jamesepatrick
jamesepatrick marked this pull request as ready for review May 26, 2026 03:58
@nedtwigg
Merge branch 'main' into main
3936b6f
@nedtwigg
nedtwigg merged commit e0d466e into diffplug:main May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@jamesepatrick @nedtwigg
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.