Skip to content

Commit a8a94c0

Browse files
committed
Fix semantics-aware sorting of static imports (fixes #522)
1 parent d107957 commit a8a94c0

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,18 +347,13 @@ private int compareFullyQualifiedClassName(String fqcn1, String fqcn2) {
347347
private String[] splitFqcnAndMember(String importString) {
348348
String s = importString.substring(STATIC_KEYWORD.length()).trim();
349349

350-
String fqcn;
351-
String member;
352-
350+
/*
351+
* Static imports always contain a member or wildcard and it's always the last
352+
* segment.
353+
*/
353354
int dot = s.lastIndexOf(".");
354-
if (!Character.isUpperCase(s.charAt(dot + 1))) {
355-
fqcn = s.substring(0, dot);
356-
member = s.substring(dot + 1);
357-
} else {
358-
fqcn = s;
359-
member = null;
360-
}
361-
355+
String fqcn = s.substring(0, dot);
356+
String member = s.substring(dot + 1);
362357
return new String[]{fqcn, member};
363358
}
364359

testlib/src/main/resources/java/importsorter/JavaCodeSortedLexicographic.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
2+
import static org.mockito.Mockito.mock;
3+
import static org.mockito.Mockito.when;
4+
15
import static com.example.Test.*;
26
import static com.example.Test.A_CONST;
37
import static com.example.Test.Nested.B_CONST;

testlib/src/main/resources/java/importsorter/JavaCodeSortedSemanticSort.test

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
2+
import static org.mockito.Mockito.mock;
3+
import static org.mockito.Mockito.when;
4+
15
import static com.example.Test.*;
26
import static com.example.Test.A_CONST;
3-
import static com.example.Test.Nested.B_CONST;
47
import static com.example.Test.Z_CONST;
8+
import static com.example.Test.Nested.B_CONST;
59

610
import com.example.Test;
711
import com.example.Test.*;

testlib/src/main/resources/java/importsorter/JavaCodeUnsortedSemanticSort.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ import com.sun.jna.platform.win32.Guid.CLSID;
1313
import com.example.a.A;
1414
import com.example.b;
1515
import com.example.c.C;
16+
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
19+
import static org.mockito.Mockito.when;

0 commit comments

Comments
 (0)