From 144684ba2278097613ac75359501806ebc2d359d Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Tue, 11 Mar 2014 14:51:15 +0100 Subject: [PATCH] java 8: accept diamonds in method calls (cherry picked from commit 08397e08d34121af89e37901453ea786b3c8612e) --- .../completion/JavaInheritorsGetter.java | 47 +++++++++++++------ .../DiamondsInsideMethodCall-out.java | 12 +++++ .../smartType/DiamondsInsideMethodCall.java | 12 +++++ .../completion/SmartType17CompletionTest.java | 2 +- .../completion/SmartType18CompletionTest.java | 5 ++ 5 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall-out.java create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java index 18ca74b8ff6c..1a14010ec834 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java @@ -18,14 +18,15 @@ package com.intellij.codeInsight.completion; import com.intellij.codeInsight.CodeInsightUtil; import com.intellij.codeInsight.ExpectedTypeInfo; import com.intellij.codeInsight.daemon.impl.analysis.HighlightClassUtil; -import com.intellij.codeInsight.lookup.*; +import com.intellij.codeInsight.lookup.AutoCompletionPolicy; +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.codeInsight.lookup.LookupElementDecorator; +import com.intellij.codeInsight.lookup.PsiTypeLookupItem; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.Comparing; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.filters.getters.ExpectedTypesGetter; import com.intellij.psi.impl.source.PsiClassReferenceType; -import com.intellij.psi.infos.MethodCandidateInfo; import com.intellij.psi.statistics.JavaStatisticsManager; import com.intellij.psi.statistics.StatisticsInfo; import com.intellij.psi.statistics.StatisticsManager; @@ -169,7 +170,7 @@ public class JavaInheritorsGetter extends CompletionProvider -1 || - Comparing.equal(originalPosition.getParent(), methodCallExpression.getArgumentList())) { - final JavaResolveResult resolveResult = methodCallExpression.resolveMethodGenerics(); - final PsiMethod method = (PsiMethod)resolveResult.getElement(); - return method == null || - PsiUtil.getApplicabilityLevel(method, resolveResult.getSubstitutor(), types, PsiUtil.getLanguageLevel(originalPosition)) - != MethodCandidateInfo.ApplicabilityLevel.NOT_APPLICABLE; + if (PsiUtil.isLanguageLevel8OrHigher(methodCallExpression)) { + final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(position, PsiNewExpression.class, false); + if (newExpression != null) { + PsiElement parent = newExpression; + while (parent.getParent() instanceof PsiParenthesizedExpression) { + parent = parent.getParent(); + } + final int idx = ArrayUtil.find(methodCallExpression.getArgumentList().getExpressions(), parent); + if (idx > -1) { + final JavaResolveResult resolveResult = methodCallExpression.resolveMethodGenerics(); + final PsiMethod method = (PsiMethod)resolveResult.getElement(); + if (method != null) { + final PsiParameter[] parameters = method.getParameterList().getParameters(); + if (idx < parameters.length) { + final PsiType expectedType = resolveResult.getSubstitutor().substitute(parameters[idx].getType()); + final PsiClass aClass = PsiUtil.resolveClassInType(expectedType); + if (aClass != null) { + final PsiClassType inferredArg = JavaPsiFacade.getElementFactory(method.getProject()).createType(aClass, types); + LOG.assertTrue(expectedType != null); + return TypeConversionUtil.isAssignable(expectedType, inferredArg); + } + } + } + } + } } + return false; } return true; } diff --git a/java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall-out.java b/java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall-out.java new file mode 100644 index 000000000000..f8768f665909 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall-out.java @@ -0,0 +1,12 @@ +class A { + A(T... t) { + } + + { + bar(new A<>() ); + } + + void bar(A s) {} + + +} diff --git a/java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall.java b/java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall.java new file mode 100644 index 000000000000..9e776bb6baba --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/DiamondsInsideMethodCall.java @@ -0,0 +1,12 @@ +class A { + A(T... t) { + } + + { + bar(new ); + } + + void bar(A s) {} + + +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java index 548f5df1500a..02773d45cd72 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java @@ -35,7 +35,7 @@ public class SmartType17CompletionTest extends LightFixtureCompletionTestCase { @NotNull @Override protected LightProjectDescriptor getProjectDescriptor() { - return JAVA_LATEST; + return JAVA_1_7; } public void testDiamondCollapsed() throws Exception { 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 72840250b795..038d10226f7d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java @@ -96,6 +96,11 @@ public class SmartType18CompletionTest extends LightFixtureCompletionTestCase { } } + public void testDiamondsInsideMethodCall() throws Exception { + configureByFile("/" + getTestName(false) + ".java"); + checkResultByFile("/" + getTestName(false) + "-out.java"); + } + private void doTest() { configureByFile("/" + getTestName(false) + ".java"); assertNotNull(myItems);