Lines 23-29
Link Here
|
23 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
23 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
24 |
|
24 |
|
25 |
static { |
25 |
static { |
26 |
// TESTS_NAMES = new String[] { "test056zz"}; |
26 |
// TESTS_NAMES = new String[] { "test056throw"}; |
27 |
// TESTS_NUMBERS = new int[] { 50 }; |
27 |
// TESTS_NUMBERS = new int[] { 50 }; |
28 |
// TESTS_RANGE = new int[] { 11, -1 }; |
28 |
// TESTS_RANGE = new int[] { 11, -1 }; |
29 |
} |
29 |
} |
Lines 4944-4949
Link Here
|
4944 |
options); |
4944 |
options); |
4945 |
} |
4945 |
} |
4946 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
4946 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
|
|
4947 |
// explicit throw is a true method exit here |
4947 |
public void test056throw1() { |
4948 |
public void test056throw1() { |
4948 |
Map options = getCompilerOptions(); |
4949 |
Map options = getCompilerOptions(); |
4949 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4950 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
Lines 4982-4987
Link Here
|
4982 |
true, |
4983 |
true, |
4983 |
options); |
4984 |
options); |
4984 |
} |
4985 |
} |
|
|
4986 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
4987 |
// close() within finally provides protection for throw |
4988 |
public void test056throw2() { |
4989 |
Map options = getCompilerOptions(); |
4990 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4991 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4992 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
4993 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4994 |
this.runNegativeTest( |
4995 |
new String[] { |
4996 |
"X.java", |
4997 |
"import java.io.FileReader;\n" + |
4998 |
"public class X {\n" + |
4999 |
" void foo1() throws Exception {\n" + |
5000 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5001 |
" try {\n" + |
5002 |
" reader.read();\n" + |
5003 |
" return;\n" + |
5004 |
" } catch (Exception e) {\n" + |
5005 |
" throw new Exception();\n" + |
5006 |
" } finally {\n" + |
5007 |
" reader.close();\n" + |
5008 |
" }\n" + |
5009 |
" }\n" + |
5010 |
"\n" + |
5011 |
" void foo2() throws Exception {\n" + |
5012 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5013 |
" try {\n" + |
5014 |
" reader.read();\n" + |
5015 |
" throw new Exception(); // should not warn here\n" + |
5016 |
" } catch (Exception e) {\n" + |
5017 |
" throw new Exception();\n" + |
5018 |
" } finally {\n" + |
5019 |
" reader.close();\n" + |
5020 |
" }\n" + |
5021 |
" }\n" + |
5022 |
"\n" + |
5023 |
" void foo3() throws Exception {\n" + |
5024 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5025 |
" try {\n" + |
5026 |
" reader.read();\n" + |
5027 |
" throw new Exception();\n" + |
5028 |
" } finally {\n" + |
5029 |
" reader.close();\n" + |
5030 |
" }\n" + |
5031 |
" }\n" + |
5032 |
"}\n" |
5033 |
}, |
5034 |
"----------\n" + |
5035 |
"1. ERROR in X.java (at line 4)\n" + |
5036 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5037 |
" ^^^^^^\n" + |
5038 |
"Resource \'reader\' should be managed by try-with-resource\n" + |
5039 |
"----------\n" + |
5040 |
"2. ERROR in X.java (at line 16)\n" + |
5041 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5042 |
" ^^^^^^\n" + |
5043 |
"Resource \'reader\' should be managed by try-with-resource\n" + |
5044 |
"----------\n" + |
5045 |
"3. ERROR in X.java (at line 28)\n" + |
5046 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5047 |
" ^^^^^^\n" + |
5048 |
"Resource \'reader\' should be managed by try-with-resource\n" + |
5049 |
"----------\n", |
5050 |
null, |
5051 |
true, |
5052 |
options); |
5053 |
} |
5054 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
5055 |
// close() nested within finally provides protection for throw |
5056 |
public void test056throw3() { |
5057 |
Map options = getCompilerOptions(); |
5058 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
5059 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
5060 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
5061 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
5062 |
this.runNegativeTest( |
5063 |
new String[] { |
5064 |
"X.java", |
5065 |
"import java.io.FileReader;\n" + |
5066 |
"public class X {\n" + |
5067 |
" void foo2x() throws Exception {\n" + |
5068 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5069 |
" try {\n" + |
5070 |
" reader.read();\n" + |
5071 |
" throw new Exception(); // should not warn here\n" + |
5072 |
" } catch (Exception e) {\n" + |
5073 |
" throw new Exception();\n" + |
5074 |
" } finally {\n" + |
5075 |
" if (reader != null)\n" + |
5076 |
" try {\n" + |
5077 |
" reader.close();\n" + |
5078 |
" } catch (java.io.IOException io) {}\n" + |
5079 |
" }\n" + |
5080 |
" }\n" + |
5081 |
"}\n" |
5082 |
}, |
5083 |
"----------\n" + |
5084 |
"1. ERROR in X.java (at line 4)\n" + |
5085 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
5086 |
" ^^^^^^\n" + |
5087 |
"Resource \'reader\' should be managed by try-with-resource\n" + |
5088 |
"----------\n", |
5089 |
null, |
5090 |
true, |
5091 |
options); |
5092 |
} |
5093 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
5094 |
// additional boolean should shed doubt on whether we reach the close() call |
5095 |
public void test056throw4() { |
5096 |
Map options = getCompilerOptions(); |
5097 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
5098 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
5099 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
5100 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
5101 |
this.runNegativeTest( |
5102 |
new String[] { |
5103 |
"X.java", |
5104 |
"import java.io.FileReader;\n" + |
5105 |
"public class X {\n" + |
5106 |
" void foo2x(boolean b) throws Exception {\n" + |
5107 |
" FileReader reader = new FileReader(\"file\");\n" + |
5108 |
" try {\n" + |
5109 |
" reader.read();\n" + |
5110 |
" throw new Exception(); // should warn here\n" + |
5111 |
" } catch (Exception e) {\n" + |
5112 |
" throw new Exception(); // should warn here\n" + |
5113 |
" } finally {\n" + |
5114 |
" if (reader != null && b)\n" + // this condition is too strong to protect reader |
5115 |
" try {\n" + |
5116 |
" reader.close();\n" + |
5117 |
" } catch (java.io.IOException io) {}\n" + |
5118 |
" }\n" + |
5119 |
" }\n" + |
5120 |
"}\n" |
5121 |
}, |
5122 |
"----------\n" + |
5123 |
"1. ERROR in X.java (at line 7)\n" + |
5124 |
" throw new Exception(); // should warn here\n" + |
5125 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
5126 |
"Resource leak: \'reader\' is not closed at this location\n" + |
5127 |
"----------\n" + |
5128 |
"2. ERROR in X.java (at line 9)\n" + |
5129 |
" throw new Exception(); // should warn here\n" + |
5130 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
5131 |
"Potential resource leak: \'reader\' may not be closed at this location\n" + |
5132 |
"----------\n", |
5133 |
null, |
5134 |
true, |
5135 |
options); |
5136 |
} |
4985 |
public static Class testClass() { |
5137 |
public static Class testClass() { |
4986 |
return TryWithResourcesStatementTest.class; |
5138 |
return TryWithResourcesStatementTest.class; |
4987 |
} |
5139 |
} |