Skip to content

Commit 8700f88

Browse files
authored
Java import order, ignore duplicate group entries (#2293)
2 parents 85a0beb + 8fb2cb8 commit 8700f88

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1717
* Add explicit support for JSONC / CSS via biome, via the file extensions `.css` and `.jsonc`.
1818
([#2259](https://github.com/diffplug/spotless/pull/2259))
1919
* Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279))
20+
### Fixed
21+
* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293))
2022

2123
## [3.0.0.BETA2] - 2024-08-25
2224
### Changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ final class ImportSorterImpl {
3333
private static final String SUBGROUP_SEPARATOR = "|";
3434

3535
private final List<ImportsGroup> importsGroups;
36+
private final Set<String> knownGroupings = new HashSet<>();
3637
private final Map<String, List<String>> matchingImports = new HashMap<>();
3738
private final List<String> notMatching = new ArrayList<>();
3839
private final Set<String> allImportOrderItems = new HashSet<>();
@@ -44,10 +45,12 @@ private static class ImportsGroup {
4445

4546
private final List<String> subGroups;
4647

47-
public ImportsGroup(String importOrder) {
48+
public ImportsGroup(String importOrder, Set<String> knownGroupings) {
4849
this.subGroups = Stream.of(importOrder.split("\\" + SUBGROUP_SEPARATOR, -1))
4950
.map(this::normalizeStatic)
51+
.filter(group -> !knownGroupings.contains(group))
5052
.collect(Collectors.toList());
53+
knownGroupings.addAll(this.subGroups);
5154
}
5255

5356
private String normalizeStatic(String subgroup) {
@@ -80,7 +83,7 @@ private List<String> sort(List<String> imports, String lineFormat) {
8083

8184
private ImportSorterImpl(List<String> importOrder, boolean wildcardsLast, boolean semanticSort,
8285
Set<String> treatAsPackage, Set<String> treatAsClass) {
83-
importsGroups = importOrder.stream().filter(Objects::nonNull).map(ImportsGroup::new).collect(Collectors.toList());
86+
importsGroups = importOrder.stream().filter(Objects::nonNull).map(order -> new ImportsGroup(order, knownGroupings)).collect(Collectors.toList());
8487
putStaticItemIfNotExists(importsGroups);
8588
putCatchAllGroupIfNotExists(importsGroups);
8689

@@ -107,13 +110,13 @@ private void putStaticItemIfNotExists(List<ImportsGroup> importsGroups) {
107110
indexOfFirstStatic = i;
108111
}
109112
}
110-
importsGroups.add(indexOfFirstStatic, new ImportsGroup(STATIC_KEYWORD));
113+
importsGroups.add(indexOfFirstStatic, new ImportsGroup(STATIC_KEYWORD, this.knownGroupings));
111114
}
112115

113116
private void putCatchAllGroupIfNotExists(List<ImportsGroup> importsGroups) {
114117
boolean catchAllSubGroupExist = importsGroups.stream().anyMatch(group -> group.getSubGroups().contains(CATCH_ALL_SUBGROUP));
115118
if (!catchAllSubGroupExist) {
116-
importsGroups.add(new ImportsGroup(CATCH_ALL_SUBGROUP));
119+
importsGroups.add(new ImportsGroup(CATCH_ALL_SUBGROUP, this.knownGroupings));
117120
}
118121
}
119122

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1010
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
1111
([#2259](https://github.com/diffplug/spotless/pull/2259))
1212
* Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279))
13+
### Fixed
14+
* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293))
1315

1416
## [7.0.0.BETA2] - 2024-08-25
1517
### Changed

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
1212
([#2259](https://github.com/diffplug/spotless/pull/2259))
1313
* Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279))
14+
### Fixed
15+
* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293))
1416

1517
## [2.44.0.BETA2] - 2024-08-25
1618
### Changed

testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,12 @@ void sortImportsFromArrayWithSubgroups() {
4343
StepHarness.forStep(step).testResource("java/importsorter/JavaCodeUnsortedImportsSubgroups.test", "java/importsorter/JavaCodeSortedImportsSubgroups.test");
4444
}
4545

46+
@Test
47+
void sortImportsFromArrayWithDuplicateEntries() {
48+
FormatterStep step = ImportOrderStep.forJava().createFrom("java|javax", "java", "org|\\#com", "\\#");
49+
StepHarness.forStep(step).testResource("java/importsorter/JavaCodeUnsortedImportsSubgroups.test", "java/importsorter/JavaCodeSortedImportsSubgroups.test");
50+
}
51+
4652
@Test
4753
void sortImportsFromArrayWithSubgroupsLeadingCatchAll() {
4854
FormatterStep step = ImportOrderStep.forJava().createFrom("\\#|");

0 commit comments

Comments
 (0)