Skip to content

Commit

Permalink
Fix semantics-aware sorting of static imports (fixes diffplug#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frettman committed May 23, 2023
1 parent d107957 commit d8746a6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
19 changes: 7 additions & 12 deletions lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,14 @@ private int compareFullyQualifiedClassName(String fqcn1, String fqcn2) {
private String[] splitFqcnAndMember(String importString) {
String s = importString.substring(STATIC_KEYWORD.length()).trim();

String fqcn;
String member;

/*
* Static imports always contain a member or wildcard and it's always the last
* segment.
*/
int dot = s.lastIndexOf(".");
if (!Character.isUpperCase(s.charAt(dot + 1))) {
fqcn = s.substring(0, dot);
member = s.substring(dot + 1);
} else {
fqcn = s;
member = null;
}

return new String[]{fqcn, member};
String fqcn = s.substring(0, dot);
String member = s.substring(dot + 1);
return new String[] { fqcn, member };
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import static com.example.Test.*;
import static com.example.Test.A_CONST;
import static com.example.Test.Nested.B_CONST;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import static com.example.Test.*;
import static com.example.Test.A_CONST;
import static com.example.Test.Nested.B_CONST;
import static com.example.Test.Z_CONST;
import static com.example.Test.Nested.B_CONST;

import com.example.Test;
import com.example.Test.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ import com.sun.jna.platform.win32.Guid.CLSID;
import com.example.a.A;
import com.example.b;
import com.example.c.C;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.when;

0 comments on commit d8746a6

Please sign in to comment.