diff --git a/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java b/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java index 16802d09d1a0..350b24646f99 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java @@ -48,6 +48,11 @@ public class CompletionParameterTypeInferencePolicy extends ProcessCandidatePara } } + @Override + public boolean inferRuntimeExceptionForThrownBoundWithNoConstraints() { + return false; + } + @Override public PsiType adjustInferredType(PsiManager manager, PsiType guess, ConstraintType constraintType) { if (guess != null && !(guess instanceof PsiWildcardType)) { diff --git a/java/java-psi-api/src/com/intellij/psi/impl/source/resolve/ParameterTypeInferencePolicy.java b/java/java-psi-api/src/com/intellij/psi/impl/source/resolve/ParameterTypeInferencePolicy.java index 1ef76cb07a1e..9b9cfce7ab72 100644 --- a/java/java-psi-api/src/com/intellij/psi/impl/source/resolve/ParameterTypeInferencePolicy.java +++ b/java/java-psi-api/src/com/intellij/psi/impl/source/resolve/ParameterTypeInferencePolicy.java @@ -33,6 +33,10 @@ public abstract class ParameterTypeInferencePolicy { public abstract Pair getInferredTypeWithNoConstraint(PsiManager manager, PsiType superType); + public boolean inferRuntimeExceptionForThrownBoundWithNoConstraints() { + return true; + } + public abstract PsiType adjustInferredType(PsiManager manager, PsiType guess, ConstraintType second); public boolean isVarargsIgnored() { diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java index 21ee7d3fe78c..7fa5f60c7793 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java @@ -1259,7 +1259,7 @@ public class InferenceSession { } if (type == PsiType.NULL) { - if (var.isThrownBound() && isThrowable(var.getBounds(InferenceBound.UPPER))) { + if (var.isThrownBound() && myPolicy.inferRuntimeExceptionForThrownBoundWithNoConstraints() && isThrowable(var.getBounds(InferenceBound.UPPER))) { type = PsiType.getJavaLangRuntimeException(myManager, GlobalSearchScope.allScope(myManager.getProject())); } else { diff --git a/java/java-tests/testData/codeInsight/completion/smartType/InferThrowableBoundInCompletion-out.java b/java/java-tests/testData/codeInsight/completion/smartType/InferThrowableBoundInCompletion-out.java new file mode 100644 index 000000000000..1f381f05fadd --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/InferThrowableBoundInCompletion-out.java @@ -0,0 +1,15 @@ +import java.io.IOException; +import java.util.function.Function; + +class LocalDateTime {} +class Launcher { + public static final Function TO_STRING = (message) -> new IOException(message.toString()); + + public static void main(String[] args) throws Throwable{ + throwException(new LocalDateTime(), TO_STRING); + } + + public static void throwException(LocalDateTime str, Function exceptionBuilder) throws Ex { + throw exceptionBuilder.apply(str); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/InferThrowableBoundInCompletion.java b/java/java-tests/testData/codeInsight/completion/smartType/InferThrowableBoundInCompletion.java new file mode 100644 index 000000000000..92d4926d6d1e --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/InferThrowableBoundInCompletion.java @@ -0,0 +1,15 @@ +import java.io.IOException; +import java.util.function.Function; + +class LocalDateTime {} +class Launcher { + public static final Function TO_STRING = (message) -> new IOException(message.toString()); + + public static void main(String[] args) throws Throwable{ + throwException(new LocalDateTime(), TO); + } + + public static void throwException(LocalDateTime str, Function exceptionBuilder) throws Ex { + throw exceptionBuilder.apply(str); + } +} \ 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 27f834df4314..bca92e33b24c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType18CompletionTest.java @@ -177,6 +177,12 @@ public void testConvertToObjectStream() { checkResultByFile("/" + getTestName(false) + "-out.java"); } + public void testInferThrowableBoundInCompletion() { + configureByTestName(); + myFixture.complete(CompletionType.SMART, 1); + checkResultByFile("/" + getTestName(false) + "-out.java"); + } + public void testInsideNewExpressionWithDiamondAndOverloadConstructors() throws Exception { configureByTestName(); myFixture.complete(CompletionType.SMART, 1);