From 39c8627084f2ce47282c6ed3fc89a07d136be46e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 15 May 2015 14:07:50 +0200 Subject: [PATCH] IG: handle try resource lists correctly in "Overly broad 'catch' block" inspection --- .../TooBroadCatchInspectionBase.java | 1 + .../com/siyeh/ig/psiutils/ExceptionUtils.java | 80 ++++++++----------- .../toobroadcatch/TooBroadCatchBlock.java | 3 + 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/TooBroadCatchInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/TooBroadCatchInspectionBase.java index 8d1aae45eca4..54207590be05 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/TooBroadCatchInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/TooBroadCatchInspectionBase.java @@ -81,6 +81,7 @@ public class TooBroadCatchInspectionBase extends BaseInspection { return; } final Set thrownTypes = ExceptionUtils.calculateExceptionsThrown(tryBlock); + ExceptionUtils.calculateExceptionsThrown(statement.getResourceList(), thrownTypes); final Set caughtTypes = new HashSet(thrownTypes.size()); final PsiCatchSection[] catchSections = statement.getCatchSections(); for (final PsiCatchSection catchSection : catchSections) { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExceptionUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExceptionUtils.java index fa3463a198c4..70baea56d8b4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExceptionUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExceptionUtils.java @@ -21,7 +21,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashSet; -import java.util.List; +import java.util.LinkedHashSet; import java.util.Set; public class ExceptionUtils { @@ -39,12 +39,19 @@ public class ExceptionUtils { @NotNull public static Set calculateExceptionsThrown(@Nullable PsiElement element) { - return calculateExceptionsThrown(element, new HashSet()); + return calculateExceptionsThrown(element, new LinkedHashSet(5)); } @NotNull public static Set calculateExceptionsThrown(@Nullable PsiElement element, @NotNull Set out) { if (element == null) return out; + if (element instanceof PsiResourceList) { + final PsiResourceList resourceList = (PsiResourceList)element; + for (PsiResourceVariable variable : resourceList.getResourceVariables()) { + final PsiMethod method = PsiUtil.getResourceCloserMethod(variable); + collectExceptionsThrown(method, out); + } + } final ExceptionsThrownVisitor visitor = new ExceptionsThrownVisitor(out); element.accept(visitor); return out; @@ -199,6 +206,19 @@ public class ExceptionUtils { return false; } + private static void collectExceptionsThrown(@Nullable PsiMethod method, @NotNull Set out) { + if (method == null) { + return; + } + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject()); + for (PsiJavaCodeReferenceElement referenceElement : method.getThrowsList().getReferenceElements()) { + final PsiClass exceptionClass = (PsiClass)referenceElement.resolve(); + if (exceptionClass != null) { + out.add(factory.createType(exceptionClass)); + } + } + } + private static class ExceptionsThrownVisitor extends JavaRecursiveElementVisitor { private final Set m_exceptionsThrown; @@ -234,53 +254,21 @@ public class ExceptionUtils { @Override public void visitTryStatement(@NotNull PsiTryStatement statement) { final Set exceptionsHandled = getExceptionTypesHandled(statement); - final PsiResourceList resourceList = statement.getResourceList(); - if (resourceList != null) { - final List resourceVariables = resourceList.getResourceVariables(); - for (PsiResourceVariable resourceVariable : resourceVariables) { - final Set resourceExceptions = calculateExceptionsThrown(resourceVariable); - collectExceptionsThrown(PsiUtil.getResourceCloserMethod(resourceVariable), resourceExceptions); - for (PsiType resourceException : resourceExceptions) { - if (!isExceptionHandled(exceptionsHandled, resourceException)) { - m_exceptionsThrown.add(resourceException); - } - } - } - } - final PsiCodeBlock tryBlock = statement.getTryBlock(); - if (tryBlock != null) { - final Set tryExceptions = calculateExceptionsThrown(tryBlock); - for (PsiType tryException : tryExceptions) { - if (!isExceptionHandled(exceptionsHandled, tryException)) { - m_exceptionsThrown.add(tryException); - } - } - } - final PsiCodeBlock finallyBlock = statement.getFinallyBlock(); - if (finallyBlock != null) { - final Set finallyExceptions = calculateExceptionsThrown(finallyBlock); - m_exceptionsThrown.addAll(finallyExceptions); - } - final PsiCodeBlock[] catchBlocks = statement.getCatchBlocks(); - for (PsiCodeBlock catchBlock : catchBlocks) { - final Set catchExceptions = calculateExceptionsThrown(catchBlock); - m_exceptionsThrown.addAll(catchExceptions); - } - } - - private static void collectExceptionsThrown(@Nullable PsiMethod method, @NotNull Set out) { - if (method == null) { - return; - } - final PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject()); - final PsiJavaCodeReferenceElement[] referenceElements = method.getThrowsList().getReferenceElements(); - for (PsiJavaCodeReferenceElement referenceElement : referenceElements) { - final PsiClass exceptionClass = (PsiClass)referenceElement.resolve(); - if (exceptionClass != null) { - out.add(factory.createType(exceptionClass)); + for (PsiType resourceException : calculateExceptionsThrown(statement.getResourceList())) { + if (!isExceptionHandled(exceptionsHandled, resourceException)) { + m_exceptionsThrown.add(resourceException); } } + for (PsiType tryException : calculateExceptionsThrown(statement.getTryBlock())) { + if (!isExceptionHandled(exceptionsHandled, tryException)) { + m_exceptionsThrown.add(tryException); + } + } + calculateExceptionsThrown(statement.getFinallyBlock(), m_exceptionsThrown); + for (PsiCodeBlock catchBlock : statement.getCatchBlocks()) { + calculateExceptionsThrown(catchBlock, m_exceptionsThrown); + } } private static boolean isExceptionHandled(Iterable exceptionsHandled, PsiType thrownType) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/toobroadcatch/TooBroadCatchBlock.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/toobroadcatch/TooBroadCatchBlock.java index 27f777085f96..5468ae045442 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/toobroadcatch/TooBroadCatchBlock.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/toobroadcatch/TooBroadCatchBlock.java @@ -61,6 +61,9 @@ public class TooBroadCatchBlock{ try { try (java.io.FileInputStream in = new java.io.FileInputStream("asdf")) {} } catch (IOException e) {} + try (java.io.InputStream in = new java.io.FileInputStream("")) { + + } catch (Exception e) {} } boolean m() {