From 91e642ac4996871bad99a6fdddf266d4aaf21124 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 18 Feb 2011 15:37:20 +0100 Subject: [PATCH] Project Coin try-with-resources support (exception handling, part 1) --- .../intellij/codeInsight/ExceptionUtil.java | 9 ++++-- .../daemon/impl/analysis/HighlightUtil.java | 22 ++++++++----- .../advHighlighting7/TryWithResources.java | 31 +++++++++++++++++++ .../daemon/LightAdvHighlightingJdk7Test.java | 6 +++- 4 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java diff --git a/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java b/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java index 8b95a5fd982c..c7f3c32476d5 100644 --- a/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java @@ -338,9 +338,9 @@ public class ExceptionUtil { @NotNull private static List getUnhandledExceptions(PsiMethod method, - PsiElement element, - PsiElement topElement, - PsiSubstitutor substitutor) { + PsiElement element, + PsiElement topElement, + PsiSubstitutor substitutor) { if (method == null || isArrayClone(method, element)) { return Collections.emptyList(); } @@ -445,6 +445,9 @@ public class ExceptionUtil { if (tryStatement.getTryBlock() == element && isCaught(tryStatement, exceptionType)) { return true; } + if (tryStatement.getResourceList() == element && isCaught(tryStatement, exceptionType)) { + return true; + } PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock(); if (element instanceof PsiCatchSection && finallyBlock != null && blockCompletesAbruptly(finallyBlock)) { // exception swallowed diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java index 0dc9884a9207..3d26c4e340fc 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -13,14 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * Created by IntelliJ IDEA. - * User: cdr - * Date: Jul 30, 2002 - */ package com.intellij.codeInsight.daemon.impl.analysis; +import com.google.common.collect.Sets; import com.intellij.codeInsight.ExceptionUtil; import com.intellij.codeInsight.daemon.JavaErrorMessages; import com.intellij.codeInsight.daemon.impl.HighlightInfo; @@ -60,6 +55,10 @@ import org.jetbrains.annotations.Nullable; import java.util.*; +/** + * @author cdr + * Date: Jul 30, 2002 + */ public class HighlightUtil { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil"); private static final Map> ourInterfaceIncompatibleModifiers; @@ -857,10 +856,19 @@ public class HighlightUtil { final PsiElement declarationScope = parameter.getDeclarationScope(); if (!(declarationScope instanceof PsiCatchSection)) return null; + final Set thrownTypes = Sets.newHashSet(); + final PsiTryStatement statement = ((PsiCatchSection)declarationScope).getTryStatement(); final PsiCodeBlock tryBlock = statement.getTryBlock(); assert tryBlock != null : statement; - final Collection thrownTypes = ExceptionUtil.collectUnhandledExceptions(tryBlock, tryBlock); + thrownTypes.addAll(ExceptionUtil.collectUnhandledExceptions(tryBlock, tryBlock)); + + final PsiParameterList resources = statement.getResourceList(); + if (resources != null) { + thrownTypes.addAll(ExceptionUtil.collectUnhandledExceptions(resources, resources)); + } + + // todo: add exceptions from resource's close() method final PsiType caughtType = parameter.getType(); if (caughtType instanceof PsiClassType) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java new file mode 100644 index 000000000000..750e9bc5cb0e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java @@ -0,0 +1,31 @@ +import java.io.*; +import java.lang.Exception; + +class C { + void m0() throws Exception { + try (FileReader reader = new FileReader(new File("input.txt"))) { + reader.read(); + } catch (Exception e) { + reader = null; + } + reader = null; + } + + void m1() { + try (final FileReader reader = new FileReader(new File("input.txt"))) { + reader.read(); + } + catch (IOException ignore) { } + + try (final FileReader reader = new FileReader(new File("input.txt"))) { + System.out.println("Try."); + } + catch (IOException ignore) { } + } + + /*void m2() throws IOException { + try (final FileReader reader = new FileReader(new File("input.txt"))) { + reader.read(); + } + }*/ +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java index dcb332113c87..8ce6d97e1c8d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java @@ -153,7 +153,11 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase { public void testMultiCatch() throws Exception { doTest(false, false); } - + + public void testTryWithResources() throws Exception { + doTest(false, false); + } + public void testSafeVarargsApplicability() throws Exception { doTest(true, false); }