diff --git a/java/java-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java index 25d1e4888517..f0f9e54fbc9f 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/resolve/PsiResolveHelperImpl.java @@ -15,6 +15,8 @@ */ package com.intellij.psi.impl.source.resolve; +import com.intellij.codeInsight.ExpectedTypeInfo; +import com.intellij.codeInsight.ExpectedTypesProvider; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.RecursionGuard; @@ -639,6 +641,13 @@ public class PsiResolveHelperImpl implements PsiResolveHelper { final GlobalSearchScope scope = parent.getResolveScope(); PsiType returnType = null; if (constraint == null) { + if (forCompletion && expectedType == null) { + ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes(methodCall, true); + if (expectedTypes.length > 0) { + expectedType = expectedTypes[0].getType(); + } + } + if (expectedType == null) { expectedType = forCompletion ? PsiType.NULL : @@ -647,11 +656,15 @@ public class PsiResolveHelperImpl implements PsiResolveHelper { returnType = ((PsiMethod)typeParameter.getOwner()).getReturnType(); - constraint = getSubstitutionForTypeParameterConstraint(typeParameter, returnType, expectedType, false, PsiUtil.getLanguageLevel(parent)); + constraint = + getSubstitutionForTypeParameterConstraint(typeParameter, returnType, expectedType, false, PsiUtil.getLanguageLevel(parent)); if (constraint != null) { PsiType guess = constraint.getFirst(); - if (guess != null && !guess.equals(PsiType.NULL) && constraint.getSecond() == ConstraintType.SUPERTYPE && guess instanceof PsiIntersectionType) { + if (guess != null && + !guess.equals(PsiType.NULL) && + constraint.getSecond() == ConstraintType.SUPERTYPE && + guess instanceof PsiIntersectionType) { for (PsiType conjuct : ((PsiIntersectionType)guess).getConjuncts()) { if (!conjuct.isAssignableFrom(expectedType)) { return FAILED_INFERENCE; diff --git a/java/java-tests/testData/codeInsight/completion/smartType/IfConditionExpectedType-out.java b/java/java-tests/testData/codeInsight/completion/smartType/IfConditionExpectedType-out.java new file mode 100644 index 000000000000..c8c1ad66e119 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/IfConditionExpectedType-out.java @@ -0,0 +1,19 @@ +interface Computable { + T compute(); +} + +public class Zoo2 { + T run(Computable computable) { + + } + + { + + if (run(new Computable() { + @Override + public Boolean compute() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + })) + } +} diff --git a/java/java-tests/testData/codeInsight/completion/smartType/IfConditionExpectedType.java b/java/java-tests/testData/codeInsight/completion/smartType/IfConditionExpectedType.java new file mode 100644 index 000000000000..d52c2cacad8c --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/IfConditionExpectedType.java @@ -0,0 +1,14 @@ +interface Computable { + T compute(); +} + +public class Zoo2 { + T run(Computable computable) { + + } + + { + + if (run(new )) + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java index ed7b14708ec6..40e581afb46f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java @@ -870,6 +870,8 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase { doTest(); } + public void testIfConditionExpectedType() throws Exception { doTest(); } + public void testUnboundTypeArgs() throws Exception { doTest(); } public void testIDEADEV2668() throws Exception {