completion: infer Throwable for thrown bounds in completion so more candidates would be applicable (IDEA-162710)

This commit is contained in:
Anna.Kozlova
2016-10-20 13:14:23 +02:00
parent f455e517e0
commit 8952680a73
6 changed files with 46 additions and 1 deletions
@@ -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)) {
@@ -33,6 +33,10 @@ public abstract class ParameterTypeInferencePolicy {
public abstract Pair<PsiType, ConstraintType> getInferredTypeWithNoConstraint(PsiManager manager, PsiType superType);
public boolean inferRuntimeExceptionForThrownBoundWithNoConstraints() {
return true;
}
public abstract PsiType adjustInferredType(PsiManager manager, PsiType guess, ConstraintType second);
public boolean isVarargsIgnored() {
@@ -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 {
@@ -0,0 +1,15 @@
import java.io.IOException;
import java.util.function.Function;
class LocalDateTime {}
class Launcher {
public static final Function<LocalDateTime, IOException> TO_STRING = (message) -> new IOException(message.toString());
public static void main(String[] args) throws Throwable{
throwException(new LocalDateTime(), TO_STRING);
}
public static <Ex extends Throwable> void throwException(LocalDateTime str, Function<LocalDateTime, Ex> exceptionBuilder) throws Ex {
throw exceptionBuilder.apply(str);
}
}
@@ -0,0 +1,15 @@
import java.io.IOException;
import java.util.function.Function;
class LocalDateTime {}
class Launcher {
public static final Function<LocalDateTime, IOException> TO_STRING = (message) -> new IOException(message.toString());
public static void main(String[] args) throws Throwable{
throwException(new LocalDateTime(), TO<caret>);
}
public static <Ex extends Throwable> void throwException(LocalDateTime str, Function<LocalDateTime, Ex> exceptionBuilder) throws Ex {
throw exceptionBuilder.apply(str);
}
}
@@ -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);