diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MethodVerifyTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MethodVerifyTest.java index 0deb894..f1d190b 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MethodVerifyTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MethodVerifyTest.java @@ -11238,7 +11238,6 @@ } //https://bugs.eclipse.org/bugs/show_bug.cgi?id=285088 public void test200() { - Map options = getCompilerOptions(); String errorMessage = "----------\n" + "1. ERROR in X.java (at line 3)\n" + diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java index be3fcc3..542911f 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java @@ -10,6 +10,7 @@ * Stephan Herrmann - Contributions for * bug 358827 - [1.7] exception analysis for t-w-r spoils null analysis * bug 349326 - [1.7] new warning for missing try-with-resources + * bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -4942,6 +4943,45 @@ true, options); } +// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points +public void test056throw1() { + Map options = getCompilerOptions(); + options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); + options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); + options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); + options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + "import java.io.FileReader;\n" + + "public class X {\n" + + " void foo2(boolean a, boolean b, boolean c) throws Exception {\n" + + " FileReader reader = new FileReader(\"file\");\n" + + " if(a)\n" + + " throw new Exception(); //warning 1\n" + + " else if (b)\n" + + " reader.close();\n" + + " else if(c)\n" + + " throw new Exception(); //warning 2\n" + + " reader.close();\n" + + " }\n" + + "}\n" + }, + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " throw new Exception(); //warning 1\n" + + " ^^^^^^^^^^^^^^^^^^^^^^\n" + + "Resource leak: \'reader\' is not closed at this location\n" + + "----------\n" + + "2. ERROR in X.java (at line 10)\n" + + " throw new Exception(); //warning 2\n" + + " ^^^^^^^^^^^^^^^^^^^^^^\n" + + "Resource leak: \'reader\' is not closed at this location\n" + + "----------\n", + null, + true, + options); +} public static Class testClass() { return TryWithResourcesStatementTest.class; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThrowStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThrowStatement.java index 7e3d4c2..2e2fe4e 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThrowStatement.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThrowStatement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. + * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Stephan Herrmann - Contribution for bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -35,6 +36,7 @@ this.exception.checkNPE(currentScope, flowContext, flowInfo); // need to check that exception thrown is actually caught somewhere flowContext.checkExceptionHandlers(this.exceptionType, this, flowInfo, currentScope); + currentScope.checkUnclosedCloseables(flowInfo, null/*ignore exception exits from flowContext*/, this); return FlowInfo.DEAD_END; }