IDEA-103576 (correct processing of unhandled multi-catch exceptions)

This commit is contained in:
Roman Shevchenko
2013-03-24 11:24:52 +01:00
parent c64aba450e
commit 992d208806
3 changed files with 35 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,8 +31,8 @@ import java.util.HashSet;
import java.util.Set;
/**
* User: anna
* Date: 15-Nov-2005
* @author anna
* @since 15-Nov-2005
*/
public class RedundantThrowsDeclaration extends BaseJavaLocalInspectionTool {
@NotNull
@@ -66,10 +66,8 @@ public class RedundantThrowsDeclaration extends BaseJavaLocalInspectionTool {
return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]);
}
//@top
private static ProblemDescriptor checkExceptionsNeverThrown(PsiJavaCodeReferenceElement referenceElement, InspectionManager inspectionManager,
private static ProblemDescriptor checkExceptionsNeverThrown(PsiJavaCodeReferenceElement referenceElement,
InspectionManager inspectionManager,
boolean onTheFly) {
if (!(referenceElement.getParent() instanceof PsiReferenceList)) return null;
PsiReferenceList referenceList = (PsiReferenceList)referenceElement.getParent();
@@ -111,8 +109,7 @@ public class RedundantThrowsDeclaration extends BaseJavaLocalInspectionTool {
}
for (PsiClassType unhandledException : unhandled) {
if (unhandledException.isAssignableFrom(exceptionType) ||
exceptionType.isAssignableFrom(unhandledException)) {
if (unhandledException.isAssignableFrom(exceptionType) || exceptionType.isAssignableFrom(unhandledException)) {
return null;
}
}
@@ -120,10 +117,7 @@ public class RedundantThrowsDeclaration extends BaseJavaLocalInspectionTool {
if (HighlightMethodUtil.isSerializationRelatedMethod(method, containingClass)) return null;
String description = JavaErrorMessages.message("exception.is.never.thrown", HighlightUtil.formatType(exceptionType));
final LocalQuickFix quickFixes = new DeleteThrowsFix(method, exceptionType);
return inspectionManager.createProblemDescriptor(referenceElement, description, quickFixes, ProblemHighlightType.LIKE_UNUSED_SYMBOL,
onTheFly);
LocalQuickFix quickFixes = new DeleteThrowsFix(method, exceptionType);
return inspectionManager.createProblemDescriptor(referenceElement, description, quickFixes, ProblemHighlightType.LIKE_UNUSED_SYMBOL, onTheFly);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.NullableFunction;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
@@ -421,21 +422,20 @@ public class ExceptionUtil {
}
@NotNull
public static List<PsiClassType> getUnhandledExceptions(final PsiThrowStatement throwStatement, @Nullable final PsiElement topElement) {
final PsiExpression exception = throwStatement.getException();
final List<PsiType> types = getPreciseThrowTypes(exception);
return ContainerUtil.mapNotNull(types, new NullableFunction<PsiType, PsiClassType>() {
@Override
public PsiClassType fun(PsiType type) {
if (type instanceof PsiClassType) {
final PsiClassType classType = (PsiClassType)type;
public static List<PsiClassType> getUnhandledExceptions(PsiThrowStatement throwStatement, @Nullable PsiElement topElement) {
List<PsiClassType> unhandled = new SmartList<PsiClassType>();
for (PsiType type : getPreciseThrowTypes(throwStatement.getException())) {
List<PsiType> types = type instanceof PsiDisjunctionType ? ((PsiDisjunctionType)type).getDisjunctions() : Collections.singletonList(type);
for (PsiType subType : types) {
if (subType instanceof PsiClassType) {
PsiClassType classType = (PsiClassType)subType;
if (!isUncheckedException(classType) && !isHandled(throwStatement, classType, topElement)) {
return classType;
unhandled.add(classType);
}
}
return null;
}
});
}
return unhandled;
}
@NotNull

View File

@@ -227,4 +227,19 @@ class C {
throw new RuntimeException(e);
}
}
void m14() {
try {
n14(42);
}
catch (E1 | E2 e) {
e.printStackTrace();
<error descr="Unhandled exceptions: C.E1, C.E2">throw e;</error>
}
}
private void n14(int i) throws E1, E2, RuntimeException {
if (i == 1) throw new E1();
if (i == 2) throw new E2();
}
}