ControlFlowAnalyzer#addThrows: avoid converting back to PsiTypes (also avoid possible NPE if SDK is not set)

GitOrigin-RevId: 5a9776725d4f6d006444f25496620eac9a81b240
This commit is contained in:
Tagir Valeev
2019-12-31 19:04:27 +07:00
committed by intellij-monorepo-bot
parent f7a9d32840
commit 6b5916730d

View File

@@ -1555,17 +1555,22 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
} }
private void addThrows(@Nullable PsiElement explicitCall, Collection<? extends PsiType> exceptions) { private void addThrows(@Nullable PsiElement explicitCall, Collection<? extends PsiType> exceptions) {
List<PsiType> allExceptions = new ArrayList<>(exceptions); StreamEx<TypeConstraint> allExceptions = StreamEx.of(JAVA_LANG_ERROR, JAVA_LANG_RUNTIME_EXCEPTION)
allExceptions.add(myExceptionCache.get(JAVA_LANG_ERROR).getThrowable().getPsiType(myProject)); .map(fqn -> myExceptionCache.get(fqn).getThrowable());
allExceptions.add(myExceptionCache.get(JAVA_LANG_RUNTIME_EXCEPTION).getThrowable().getPsiType(myProject)); if (!exceptions.isEmpty()) {
List<PsiType> refs = PsiDisjunctionType.flattenAndRemoveDuplicates(allExceptions); allExceptions = allExceptions.append(StreamEx.of(exceptions).map(TypeConstraints::instanceOf))
for (PsiType ref : refs) { .map(TypeConstraint::tryNegate).nonNull()
pushUnknown(); .reduce(TypeConstraints.TOP, TypeConstraint::meet)
ConditionalGotoInstruction cond = new ConditionalGotoInstruction(null, false, null); .notInstanceOfTypes()
addInstruction(cond); .map(TypeConstraint.Exact::instanceOf);
throwException(ref, explicitCall);
cond.setOffset(myCurrentFlow.getInstructionCount());
} }
allExceptions.forEach(exc -> {
pushUnknown();
ConditionalGotoInstruction cond = new ConditionalGotoInstruction(null, false, null);
addInstruction(cond);
throwException(new ExceptionTransfer(exc), explicitCall);
cond.setOffset(myCurrentFlow.getInstructionCount());
});
} }
void throwException(@Nullable PsiType ref, @Nullable PsiElement anchor) { void throwException(@Nullable PsiType ref, @Nullable PsiElement anchor) {