diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/CollectionsUtilityMethodsProvider.java b/java/java-impl/src/com/intellij/codeInsight/completion/CollectionsUtilityMethodsProvider.java index 789b565c555e..da8b1fe8a64c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/CollectionsUtilityMethodsProvider.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/CollectionsUtilityMethodsProvider.java @@ -5,6 +5,7 @@ import com.intellij.codeInsight.lookup.AutoCompletionPolicy; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.psi.*; import com.intellij.util.Consumer; +import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -35,6 +36,19 @@ class CollectionsUtilityMethodsProvider { final PsiClass collectionsClass = JavaPsiFacade.getInstance(myElement.getProject()).findClass(JAVA_UTIL_COLLECTIONS, myElement.getResolveScope()); if (collectionsClass == null) return; + PsiJavaFile file = ObjectUtils.tryCast(parent.getContainingFile(), PsiJavaFile.class); + if (file == null) return; + PsiImportList importList = file.getImportList(); + if (importList != null) { + for (PsiImportStaticStatement statement : importList.getImportStaticStatements()) { + PsiClass aClass = statement.resolveTargetClass(); + if (aClass != null && aClass.isEquivalentTo(collectionsClass)) { + // The Collections class is already statically imported; + // should be suggested anyway in JavaStaticMemberProcessor + return; + } + } + } final PsiElement pparent = parent.getParent(); if (showAll || diff --git a/java/java-tests/testData/codeInsight/completion/smartType/NoDuplicateEmptyList.java b/java/java-tests/testData/codeInsight/completion/smartType/NoDuplicateEmptyList.java new file mode 100644 index 000000000000..f87da951d0e0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/NoDuplicateEmptyList.java @@ -0,0 +1,23 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static java.util.Collections.singletonList; + +// IDEA-251394 +public final class SomeUtils { + + private SomeUtils() {} + + public static List getEventsFor(List execution) { + List statuses = new ArrayList<>(); + if (statuses.isEmpty()) { + return + } + + if (execution.size() > 1) { + return Collections.emptyList(); + } + return new ArrayList<>(); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java index 1fa4abd80cd3..1bdc49cc3ccc 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java @@ -346,4 +346,11 @@ public class SmartType18CompletionTest extends LightFixtureCompletionTestCase { public void testFilterInaccessibleConstructors() { doAntiTest(); } public void testCastInToArrayCallWithUnresolvedType() { doAntiTest(); } + + @NeedsIndex.ForStandardLibrary + public void testNoDuplicateEmptyList() { + configureByTestName(); + Lookup lookup = getLookup(); + assertEquals(1, lookup.getItems().stream().filter(item -> item.getLookupString().equals("emptyList")).count()); + } }