From 1ab76a3b95f86f0d764905581d5542c670f1d537 Mon Sep 17 00:00:00 2001 From: Peter Gromov Date: Wed, 17 Jun 2020 18:10:17 +0200 Subject: [PATCH] Skip some invalid/meaningless `.stream().*` completion suggestions (IDEA-243790) GitOrigin-RevId: e02ac9f63fd6384ffe3f27f5c4825ddc3a5af58e --- .../codeInsight/completion/StreamConversion.java | 15 ++++++++++----- .../normal/SuggestOnlyAccessibleStreamMethod.java | 9 +++++++++ .../completion/Normal8CompletionTest.groovy | 2 ++ 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/completion/normal/SuggestOnlyAccessibleStreamMethod.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/StreamConversion.java b/java/java-impl/src/com/intellij/codeInsight/completion/StreamConversion.java index d29be4a325b7..10ff837eeca8 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/StreamConversion.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/StreamConversion.java @@ -37,12 +37,17 @@ class StreamConversion { PsiType type = qualifier.getType(); if (type instanceof PsiClassType) { PsiClass qualifierClass = ((PsiClassType)type).resolve(); - if (qualifierClass == null) return Collections.emptyList(); + if (qualifierClass == null || InheritanceUtil.isInheritor(qualifierClass, JAVA_UTIL_STREAM_BASE_STREAM)) { + return Collections.emptyList(); + } - PsiMethod streamMethod = ContainerUtil.find(qualifierClass.findMethodsByName("stream", true), m -> - !m.hasParameters() && - InheritanceUtil.isInheritor(m.getReturnType(), JAVA_UTIL_STREAM_BASE_STREAM)); - if (streamMethod == null) return Collections.emptyList(); + PsiMethod streamMethod = ContainerUtil.find(qualifierClass.findMethodsByName("stream", true), m -> !m.hasParameters()); + if (streamMethod == null || + streamMethod.hasModifierProperty(PsiModifier.STATIC) || + !PsiUtil.isAccessible(streamMethod, ref, null) || + !InheritanceUtil.isInheritor(streamMethod.getReturnType(), JAVA_UTIL_STREAM_BASE_STREAM)) { + return Collections.emptyList(); + } return generateStreamSuggestions(parameters, qualifier, qualifier.getText() + ".stream()", context -> { String space = getSpace(CodeStyle.getLanguageSettings(context.getFile()).SPACE_WITHIN_EMPTY_METHOD_CALL_PARENTHESES); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/completion/normal/SuggestOnlyAccessibleStreamMethod.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/completion/normal/SuggestOnlyAccessibleStreamMethod.java new file mode 100644 index 000000000000..9feedc61f6e5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/completion/normal/SuggestOnlyAccessibleStreamMethod.java @@ -0,0 +1,9 @@ +class Foo { + void foo(MyStream list) { + list.ma + } +} + +class MyStream { + private java.util.stream.Stream stream() {} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal8CompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal8CompletionTest.groovy index 07d9281f2fae..7b02958efe4a 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal8CompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal8CompletionTest.groovy @@ -458,6 +458,8 @@ class Test88 { checkResultByFileName() } + void testSuggestOnlyAccessibleStreamMethod() { doAntiTest() } + @NeedsIndex.ForStandardLibrary void testStreamMethodsOnArray() { configureByTestName()