From 375bdad5697e5b10204b2660a985cc3ecf42982c Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 16 Aug 2015 17:06:49 +0200 Subject: [PATCH] fix Collections.emptyMap not being suggested in completion if jdk8 is used --- .../completion/SmartCompletionDecorator.java | 20 ++++++------------- .../smartType/CollectionsEmptyMap-out.java | 9 +++++++++ .../smartType/CollectionsEmptyMap.java | 8 ++++++++ .../completion/SmartType18CompletionTest.java | 2 ++ 4 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap-out.java create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/SmartCompletionDecorator.java b/java/java-impl/src/com/intellij/codeInsight/completion/SmartCompletionDecorator.java index 3e95372ded9c..9c65736fefd3 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/SmartCompletionDecorator.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/SmartCompletionDecorator.java @@ -23,9 +23,9 @@ import com.intellij.codeInsight.lookup.Lookup; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupItem; import com.intellij.codeInsight.lookup.TailTypeDecorator; +import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.TypeConversionUtil; import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; @@ -177,20 +177,12 @@ public class SmartCompletionDecorator extends TailTypeDecorator { return false; } - public static PsiSubstitutor calculateMethodReturnTypeSubstitutor(PsiMethod method, final PsiType expected) { - PsiSubstitutor substitutor = PsiSubstitutor.EMPTY; - PsiResolveHelper helper = JavaPsiFacade.getInstance(method.getProject()).getResolveHelper(); - final PsiTypeParameter[] typeParameters = method.getTypeParameters(); - for (PsiTypeParameter typeParameter : typeParameters) { - PsiType substitution = helper.getSubstitutionForTypeParameter(typeParameter, method.getReturnType(), expected, - false, PsiUtil.getLanguageLevel(method)); - if (PsiType.NULL.equals(substitution)) { - substitution = TypeConversionUtil.typeParameterErasure(typeParameter); - } + public static PsiSubstitutor calculateMethodReturnTypeSubstitutor(@NotNull PsiMethod method, @NotNull final PsiType expected) { + PsiType returnType = method.getReturnType(); + if (returnType == null) return PsiSubstitutor.EMPTY; - substitutor = substitutor.put(typeParameter, substitution); - } - return substitutor; + PsiResolveHelper helper = JavaPsiFacade.getInstance(method.getProject()).getResolveHelper(); + return helper.inferTypeArguments(method.getTypeParameters(), new PsiType[]{expected}, new PsiType[]{returnType}, LanguageLevel.HIGHEST); } @Nullable diff --git a/java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap-out.java b/java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap-out.java new file mode 100644 index 000000000000..d025533d03d4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap-out.java @@ -0,0 +1,9 @@ +import java.util.Collections; +import java.util.Map; + +class Foo { + void m() { + Map m = Collections.emptyMap(); + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap.java b/java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap.java new file mode 100644 index 000000000000..3c02ea507508 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/CollectionsEmptyMap.java @@ -0,0 +1,8 @@ +import java.util.Map; + +class Foo { + void m() { + Map m = emptyM + } + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java index e4c414b596ab..3e8dbb5aea28 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java @@ -150,6 +150,8 @@ public void testConvertToObjectStream() { doTest(false); } + public void testCollectionsEmptyMap() { doTest(true); } + private void doTest() { doTest(true); }