Project Coin try-with-resources support (exception handling, part 1)

This commit is contained in:
Roman Shevchenko
2011-02-18 15:37:38 +01:00
parent 40046e9177
commit 91e642ac49
4 changed files with 57 additions and 11 deletions
@@ -338,9 +338,9 @@ public class ExceptionUtil {
@NotNull
private static List<PsiClassType> 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
@@ -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<String, Set<String>> ourInterfaceIncompatibleModifiers;
@@ -857,10 +856,19 @@ public class HighlightUtil {
final PsiElement declarationScope = parameter.getDeclarationScope();
if (!(declarationScope instanceof PsiCatchSection)) return null;
final Set<PsiClassType> thrownTypes = Sets.newHashSet();
final PsiTryStatement statement = ((PsiCatchSection)declarationScope).getTryStatement();
final PsiCodeBlock tryBlock = statement.getTryBlock();
assert tryBlock != null : statement;
final Collection<PsiClassType> 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) {
@@ -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) {
<error descr="Cannot resolve symbol 'reader'">reader</error> = null;
}
<error descr="Cannot resolve symbol 'reader'">reader</error> = 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();
}
}*/
}
@@ -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);
}