diff options
2 files changed, 63 insertions, 3 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java index 031432dcdb..d8aced88e0 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java @@ -26,7 +26,7 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; public class ResourceLeakTests extends AbstractRegressionTest { static { -// TESTS_NAMES = new String[] { "testBug388996" }; +// TESTS_NAMES = new String[] { "testBug386534" }; // TESTS_NUMBERS = new int[] { 50 }; // TESTS_RANGE = new int[] { 11, -1 }; } @@ -3870,4 +3870,57 @@ public void testBug388996() { options, null); } + +// https://bugs.eclipse.org/386534 - [compiler][resource] "Potential resource leak" false positive warning +public void testBug386534() { + Map options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); + runConformTest( + new String[] { + "Bug386534.java", + "import java.io.FileNotFoundException;\n" + + "import java.io.IOException;\n" + + "import java.io.OutputStream;\n" + + "\n" + + "public class Bug386534 {\n" + + " private static final String DETAILS_FILE_NAME = null;\n" + + " private static final String LOG_TAG = null;\n" + + " private static Context sContext;\n" + + " static void saveDetails(byte[] detailsData) {\n" + + " OutputStream os = null;\n" + + " try {\n" + + " os = sContext.openFileOutput(DETAILS_FILE_NAME,\n" + + " Context.MODE_PRIVATE);\n" + + " os.write(detailsData);\n" + + " } catch (IOException e) {\n" + + " Log.w(LOG_TAG, \"Unable to save details\", e);\n" + + " } finally {\n" + + " if (os != null) {\n" + + " try {\n" + + " os.close();\n" + + " } catch (IOException ignored) {\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " static class Context {\n" + + " public static final String MODE_PRIVATE = null;\n" + + " public OutputStream openFileOutput(String detailsFileName,\n" + + " String modePrivate) throws FileNotFoundException{\n" + + " return null;\n" + + " }\n" + + " }\n" + + " static class Log {\n" + + " public static void w(String logTag, String string, IOException e) {\n" + + " }\n" + + " }\n" + + "}\n" + }, + "", + null, + true, + null, + options, + null); +} } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java index 20dde0b661..ca8439311b 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java @@ -91,8 +91,15 @@ public class FakedTrackingVariable extends LocalDeclaration { ((long)this.sourceStart <<32)+this.sourceEnd); this.methodScope = original.declaringScope.methodScope(); this.originalBinding = original; - if (flowContext instanceof FinallyFlowContext) - this.tryContext = ((FinallyFlowContext) flowContext).tryContext; + // inside a finally block? + while (flowContext != null) { + if (flowContext instanceof FinallyFlowContext) { + // yes -> connect to the corresponding try block: + this.tryContext = ((FinallyFlowContext) flowContext).tryContext; + break; + } + flowContext = flowContext.parent; + } resolve(original.declaringScope); } |