diff --git a/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java b/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java index 126a55e5220..a074f4efd1b 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java @@ -120,6 +120,14 @@ public List> addImport(List> origi return singletonList(paddedToAdd); } + // Do not add the import if it is already present. + JavaType addedType = paddedToAdd.getElement().getQualid().getType(); + for (JRightPadded originalImport : originalImports) { + if (addedType != null && TypeUtils.isOfType(addedType, originalImport.getElement().getQualid().getType())) { + return originalImports; + } + } + // don't star fold just yet, because we are only going to star fold adjacent imports along with // the import to add at most. we don't even want to star fold other non-adjacent imports in the same // block that should be star folded according to the layout style (minimally invasive change). @@ -127,23 +135,6 @@ public List> addImport(List> origi new ImportLayoutStyle(Integer.MAX_VALUE, Integer.MAX_VALUE, layout, packagesToFold) .orderImports(ListUtils.concat(originalImports, paddedToAdd), new HashSet<>()); - if (ideallyOrdered.size() == originalImports.size()) { - Set originalPaths = new HashSet<>(); - for (JRightPadded originalImport : originalImports) { - originalPaths.add(originalImport.getElement().getTypeName()); - } - int sharedImports = 0; - for (JRightPadded importJRightPadded : ideallyOrdered) { - if (originalPaths.contains(importJRightPadded.getElement().getTypeName())) { - sharedImports++; - } - } - if (sharedImports == originalImports.size()) { - // must be a duplicate of an existing import - return originalImports; - } - } - JRightPadded before = null; JRightPadded after = null; diff --git a/rewrite-java/src/test/java/org/openrewrite/java/style/ImportLayoutStyleTest.java b/rewrite-java/src/test/java/org/openrewrite/java/style/ImportLayoutStyleTest.java index ecfa335c43e..a012ada4684 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/style/ImportLayoutStyleTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/style/ImportLayoutStyleTest.java @@ -20,9 +20,17 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import org.junit.jupiter.api.Test; +import org.openrewrite.Issue; import org.openrewrite.config.DeclarativeNamedStyles; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JLeftPadded; +import org.openrewrite.java.tree.JRightPadded; +import org.openrewrite.java.tree.Space; +import org.openrewrite.java.tree.TypeTree; +import org.openrewrite.marker.Markers; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.Set; @@ -80,4 +88,41 @@ void deserializeInDeclarativeNamedStyles() throws IOException { mapper.readValue(mapper.writeValueAsBytes(style), DeclarativeNamedStyles.class); } + + @Test + @Issue("https://github.com/openrewrite/rewrite/issues/4196") + void addImportInPresenceOfDuplicateOtherImport() { + ImportLayoutStyle style = new ImportLayoutStyle( + Integer.MAX_VALUE, Integer.MAX_VALUE, Collections.emptyList(), Collections.emptyList()); + JRightPadded import1 = new JRightPadded<>( + new J.Import( + randomId(), + Space.EMPTY, + Markers.EMPTY, + new JLeftPadded<>(Space.SINGLE_SPACE, true, Markers.EMPTY), + TypeTree.build("pkg.Clazz.MEMBER_1").withPrefix(Space.SINGLE_SPACE), + null), + Space.EMPTY, + Markers.EMPTY); + JRightPadded import2 = new JRightPadded<>( + new J.Import( + randomId(), + Space.EMPTY, + Markers.EMPTY, + new JLeftPadded<>(Space.SINGLE_SPACE, true, Markers.EMPTY), + TypeTree.build("pkg.Clazz.MEMBER_1").withPrefix(Space.SINGLE_SPACE), + null), + Space.EMPTY, + Markers.EMPTY); + J.Import importToAdd = new J.Import( + randomId(), + Space.EMPTY, + Markers.EMPTY, + new JLeftPadded<>(Space.SINGLE_SPACE, true, Markers.EMPTY), + TypeTree.build("pkg.Clazz.MEMBER_2").withPrefix(Space.SINGLE_SPACE), + null); + assertThat(style.addImport(List.of(import1, import2), importToAdd, null, Collections.emptyList())) + .containsExactlyInAnyOrder( + import1, import1, new JRightPadded<>(importToAdd, Space.EMPTY, Markers.EMPTY)); + } }