From cebb5b89361a71c2d747aed943a0dbb49a84428a Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 30 Sep 2013 14:20:35 +0200 Subject: [PATCH] fix dfa offset management when handling possible exceptions in finally --- .../dataFlow/ControlFlowAnalyzer.java | 4 +++- .../fixture/ExceptionFromFinally.java | 21 +++++++++++++++++++ .../DataFlowInspectionTest.java | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/ExceptionFromFinally.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java index dcf3a3817210..fd640f0fbde0 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java @@ -720,11 +720,12 @@ class ControlFlowAnalyzer extends JavaElementVisitor { if (exceptionClass == null) return; for (int i = myCatchStack.size() - 1; i >= 0; i--) { CatchDescriptor cd = myCatchStack.get(i); - flushVariablesInsideTry(cd); if (cd.isFinally()) { + flushVariablesInsideTry(cd); addInstruction(new GosubInstruction(cd.getJumpOffset(this))); } else if (cd.getType().isAssignableFrom(exceptionClass)) { // Definite catch. + flushVariablesInsideTry(cd); addGotoCatch(cd); return; } @@ -733,6 +734,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor { pushUnknown(); final ConditionalGotoInstruction branch = new ConditionalGotoInstruction(null, false, null); addInstruction(branch); + flushVariablesInsideTry(cd); addGotoCatch(cd); branch.setOffset(myCurrentFlow.getInstructionCount()); } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ExceptionFromFinally.java b/java/java-tests/testData/inspection/dataFlow/fixture/ExceptionFromFinally.java new file mode 100644 index 000000000000..a8ba74e516ae --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/ExceptionFromFinally.java @@ -0,0 +1,21 @@ +import java.io.*; + +class Foo { + + public void read() { + try { + final FileInputStream input = new FileInputStream(new File("foo")); + try { + } + finally { + input.close(); + } + } + catch (FileNotFoundException ignored) { + } + catch (IOException e) { + } + } + + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index c5158a430b7b..44aa3be42ffa 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -68,6 +68,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { public void testTernaryInWhileNotComplex() throws Throwable { doTest(); } public void testTryCatchInForNotComplex() throws Throwable { doTest(); } public void testNestedTryInWhileNotComplex() throws Throwable { doTest(); } + public void testExceptionFromFinally() throws Throwable { doTest(); } public void testFieldChangedBetweenSynchronizedBlocks() throws Throwable { doTest(); } public void testGeneratedEquals() throws Throwable { doTest(); }