Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2011, 2012 GK Software AG and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Stephan Herrmann - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.tests.compiler.regression; |
12 |
|
13 |
import java.io.IOException; |
14 |
import java.net.URL; |
15 |
import java.util.Map; |
16 |
|
17 |
import junit.framework.Test; |
18 |
|
19 |
import org.eclipse.core.runtime.FileLocator; |
20 |
import org.eclipse.core.runtime.Path; |
21 |
import org.eclipse.core.runtime.Platform; |
22 |
import org.eclipse.jdt.core.JavaCore; |
23 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
24 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
25 |
|
26 |
public class ResourceLeakTests extends AbstractRegressionTest { |
27 |
|
28 |
static { |
29 |
// TESTS_NAMES = new String[] { "test061a"}; |
30 |
// TESTS_NUMBERS = new int[] { 50 }; |
31 |
// TESTS_RANGE = new int[] { 11, -1 }; |
32 |
} |
33 |
public ResourceLeakTests(String name) { |
34 |
super(name); |
35 |
} |
36 |
public static Test suite() { |
37 |
return buildAllCompliancesTestSuite(ResourceLeakTests.class); |
38 |
} |
39 |
|
40 |
void runTestsExpectingErrorsOnlyIn17(String[] testFiles, String errorsIn17, Map options) { |
41 |
if (this.complianceLevel >= ClassFileConstants.JDK1_7) |
42 |
runNegativeTest(testFiles, errorsIn17, null, true, options); |
43 |
else |
44 |
runConformTest(testFiles, "", null, true, null, options, null); |
45 |
} |
46 |
|
47 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
48 |
// a method uses an AutoCloseable without ever closing it. |
49 |
public void test056() { |
50 |
Map options = getCompilerOptions(); |
51 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
52 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
53 |
this.runNegativeTest( |
54 |
new String[] { |
55 |
"X.java", |
56 |
"import java.io.File;\n" + |
57 |
"import java.io.FileReader;\n" + |
58 |
"import java.io.IOException;\n" + |
59 |
"public class X {\n" + |
60 |
" void foo() throws IOException {\n" + |
61 |
" File file = new File(\"somefile\");\n" + |
62 |
" FileReader fileReader = new FileReader(file);\n" + |
63 |
// not invoking any methods on FileReader, try to avoid necessary call to superclass() in the compiler |
64 |
// " char[] in = new char[50];\n" + |
65 |
// " fileReader.read(in);\n" + |
66 |
" }\n" + |
67 |
" public static void main(String[] args) throws IOException {\n" + |
68 |
" new X().foo();\n" + |
69 |
" }\n" + |
70 |
"}\n" |
71 |
}, |
72 |
"----------\n" + |
73 |
"1. ERROR in X.java (at line 7)\n" + |
74 |
" FileReader fileReader = new FileReader(file);\n" + |
75 |
" ^^^^^^^^^^\n" + |
76 |
"Resource leak: 'fileReader' is never closed\n" + |
77 |
"----------\n", |
78 |
null, |
79 |
true, |
80 |
options); |
81 |
} |
82 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
83 |
// a method uses an AutoCloseable and closes it but not protected by t-w-r nor regular try-finally |
84 |
public void test056a() { |
85 |
Map options = getCompilerOptions(); |
86 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
87 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
88 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
89 |
runTestsExpectingErrorsOnlyIn17( |
90 |
new String[] { |
91 |
"X.java", |
92 |
"import java.io.File;\n" + |
93 |
"import java.io.FileReader;\n" + |
94 |
"import java.io.IOException;\n" + |
95 |
"public class X {\n" + |
96 |
" void foo() throws IOException {\n" + |
97 |
" File file = new File(\"somefile\");\n" + |
98 |
" FileReader fileReader = new FileReader(file);\n" + |
99 |
" char[] in = new char[50];\n" + |
100 |
" fileReader.read(in);\n" + |
101 |
" fileReader.close();\n" + |
102 |
" }\n" + |
103 |
"}\n" |
104 |
}, |
105 |
"----------\n" + |
106 |
"1. ERROR in X.java (at line 7)\n" + |
107 |
" FileReader fileReader = new FileReader(file);\n" + |
108 |
" ^^^^^^^^^^\n" + |
109 |
"Resource 'fileReader' should be managed by try-with-resource\n" + |
110 |
"----------\n", |
111 |
options); |
112 |
} |
113 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
114 |
// a method uses an AutoCloseable and closes it properly in a finally block |
115 |
public void test056b() { |
116 |
Map options = getCompilerOptions(); |
117 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
118 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
119 |
this.runConformTest( |
120 |
new String[] { |
121 |
"X.java", |
122 |
"import java.io.File;\n" + |
123 |
"import java.io.FileReader;\n" + |
124 |
"import java.io.IOException;\n" + |
125 |
"public class X {\n" + |
126 |
" void foo() throws IOException {\n" + |
127 |
" File file = new File(\"somefile\");\n" + |
128 |
" FileReader fileReader = new FileReader(file);\n" + |
129 |
" try {\n" + |
130 |
" char[] in = new char[50];\n" + |
131 |
" fileReader.read(in);\n" + |
132 |
" } finally {\n" + |
133 |
" fileReader.close();\n" + |
134 |
" }\n" + |
135 |
" }\n" + |
136 |
" public static void main(String[] args) {\n" + |
137 |
" try {\n" + |
138 |
" new X().foo();\n" + |
139 |
" } catch (IOException ioex) {\n" + |
140 |
" System.out.println(\"caught\");\n" + |
141 |
" }\n" + |
142 |
" }\n" + |
143 |
"}\n" |
144 |
}, |
145 |
"caught", /*output*/ |
146 |
null/*classLibs*/, |
147 |
true/*shouldFlush*/, |
148 |
null/*vmargs*/, |
149 |
options, |
150 |
null/*requestor*/); |
151 |
} |
152 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
153 |
// a method uses an AutoCloseable properly within try-with-resources. |
154 |
public void test056c() { |
155 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; // t-w-r used |
156 |
Map options = getCompilerOptions(); |
157 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
158 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
159 |
this.runConformTest( |
160 |
new String[] { |
161 |
"X.java", |
162 |
"import java.io.File;\n" + |
163 |
"import java.io.FileReader;\n" + |
164 |
"import java.io.IOException;\n" + |
165 |
"public class X {\n" + |
166 |
" void foo() throws IOException {\n" + |
167 |
" File file = new File(\"somefile\");\n" + |
168 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
169 |
" char[] in = new char[50];\n" + |
170 |
" fileReader.read(in);\n" + |
171 |
" }\n" + |
172 |
" }\n" + |
173 |
" public static void main(String[] args) {\n" + |
174 |
" try {\n" + |
175 |
" new X().foo();\n" + |
176 |
" } catch (IOException ioex) {\n" + |
177 |
" System.out.println(\"caught\");\n" + |
178 |
" }\n" + |
179 |
" }\n" + |
180 |
"}\n" |
181 |
}, |
182 |
"caught", /*output*/ |
183 |
null/*classLibs*/, |
184 |
true/*shouldFlush*/, |
185 |
null/*vmargs*/, |
186 |
options, |
187 |
null/*requestor*/); |
188 |
} |
189 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
190 |
// a method uses two AutoCloseables (testing independent analysis) |
191 |
//- one closeable may be unclosed at a conditional return |
192 |
//- the other is only conditionally closed |
193 |
public void test056d() { |
194 |
Map options = getCompilerOptions(); |
195 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
196 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
197 |
this.runNegativeTest( |
198 |
new String[] { |
199 |
"X.java", |
200 |
"import java.io.File;\n" + |
201 |
"import java.io.FileReader;\n" + |
202 |
"import java.io.IOException;\n" + |
203 |
"public class X {\n" + |
204 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
205 |
" File file = new File(\"somefile\");\n" + |
206 |
" char[] in = new char[50];\n" + |
207 |
" FileReader fileReader1 = new FileReader(file);\n" + |
208 |
" fileReader1.read(in);\n" + |
209 |
" FileReader fileReader2 = new FileReader(file);\n" + |
210 |
" fileReader2.read(in);\n" + |
211 |
" if (flag1) {\n" + |
212 |
" fileReader2.close();\n" + |
213 |
" return;\n" + |
214 |
" } else if (flag2) {\n" + |
215 |
" fileReader2.close();\n" + |
216 |
" }\n" + |
217 |
" fileReader1.close();\n" + |
218 |
" }\n" + |
219 |
" public static void main(String[] args) throws IOException {\n" + |
220 |
" new X().foo(false, true);\n" + |
221 |
" }\n" + |
222 |
"}\n" |
223 |
}, |
224 |
"----------\n" + |
225 |
"1. WARNING in X.java (at line 10)\n" + |
226 |
" FileReader fileReader2 = new FileReader(file);\n" + |
227 |
" ^^^^^^^^^^^\n" + |
228 |
"Potential resource leak: 'fileReader2' may not be closed\n" + |
229 |
"----------\n" + |
230 |
"2. ERROR in X.java (at line 14)\n" + |
231 |
" return;\n" + |
232 |
" ^^^^^^^\n" + |
233 |
"Resource leak: 'fileReader1' is not closed at this location\n" + |
234 |
"----------\n", |
235 |
null, |
236 |
true, |
237 |
options); |
238 |
} |
239 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
240 |
// a method uses two AutoCloseables (testing independent analysis) |
241 |
//- one closeable may be unclosed at a conditional return |
242 |
//- the other is only conditionally closed |
243 |
public void test056d_suppress() { |
244 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // annotations used |
245 |
Map options = getCompilerOptions(); |
246 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
247 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
248 |
options.put(CompilerOptions.OPTION_SuppressOptionalErrors, CompilerOptions.ENABLED); |
249 |
this.runNegativeTest( |
250 |
new String[] { |
251 |
"X.java", |
252 |
"import java.io.File;\n" + |
253 |
"import java.io.FileReader;\n" + |
254 |
"import java.io.IOException;\n" + |
255 |
"public class X {\n" + |
256 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
257 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
258 |
" char[] in = new char[50];\n" + |
259 |
" FileReader fileReader1 = new FileReader(file);\n" + |
260 |
" fileReader1.read(in);\n" + |
261 |
" @SuppressWarnings(\"resource\") FileReader fileReader2 = new FileReader(file); // useful suppress\n" + |
262 |
" fileReader2.read(in);\n" + |
263 |
" if (flag1) {\n" + |
264 |
" fileReader2.close();\n" + |
265 |
" return; // not suppressed\n" + |
266 |
" } else if (flag2) {\n" + |
267 |
" fileReader2.close();\n" + |
268 |
" }\n" + |
269 |
" fileReader1.close();\n" + |
270 |
" }\n" + |
271 |
" @SuppressWarnings(\"resource\") // useful suppress\n" + |
272 |
" void bar() throws IOException {\n" + |
273 |
" File file = new File(\"somefile\");\n" + |
274 |
" FileReader fileReader = new FileReader(file);\n" + |
275 |
" char[] in = new char[50];\n" + |
276 |
" fileReader.read(in);\n" + |
277 |
" }\n" + |
278 |
" public static void main(String[] args) throws IOException {\n" + |
279 |
" new X().foo(false, true);\n" + |
280 |
" }\n" + |
281 |
"}\n" |
282 |
}, |
283 |
"----------\n" + |
284 |
"1. WARNING in X.java (at line 6)\n" + |
285 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
286 |
" ^^^^^^^^^^\n" + |
287 |
"Unnecessary @SuppressWarnings(\"resource\")\n" + |
288 |
"----------\n" + |
289 |
"2. ERROR in X.java (at line 14)\n" + |
290 |
" return; // not suppressed\n" + |
291 |
" ^^^^^^^\n" + |
292 |
"Resource leak: 'fileReader1' is not closed at this location\n" + |
293 |
"----------\n", |
294 |
null, |
295 |
true, |
296 |
options); |
297 |
} |
298 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
299 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
300 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
301 |
public void test056e() { |
302 |
Map options = getCompilerOptions(); |
303 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
304 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
305 |
this.runNegativeTest( |
306 |
new String[] { |
307 |
"X.java", |
308 |
"import java.io.File;\n" + |
309 |
"import java.io.FileReader;\n" + |
310 |
"import java.io.IOException;\n" + |
311 |
"public class X {\n" + |
312 |
" FileReader getReader(String filename) throws IOException {\n" + |
313 |
" File file = new File(\"somefile\");\n" + |
314 |
" FileReader fileReader = new FileReader(file);\n" + |
315 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
316 |
" }\n" + |
317 |
" void foo() throws IOException {\n" + |
318 |
" FileReader reader = getReader(\"somefile\");\n" + |
319 |
" char[] in = new char[50];\n" + |
320 |
" reader.read(in);\n" + |
321 |
" }\n" + |
322 |
" public static void main(String[] args) throws IOException {\n" + |
323 |
" new X().foo();\n" + |
324 |
" }\n" + |
325 |
"}\n" |
326 |
}, |
327 |
"----------\n" + |
328 |
"1. ERROR in X.java (at line 11)\n" + |
329 |
" FileReader reader = getReader(\"somefile\");\n" + |
330 |
" ^^^^^^\n" + |
331 |
"Potential resource leak: \'reader\' may not be closed\n" + |
332 |
"----------\n", |
333 |
null, |
334 |
true, |
335 |
options); |
336 |
} |
337 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
338 |
// a method explicitly closes its AutoCloseable rather than using t-w-r |
339 |
public void test056f() { |
340 |
Map options = getCompilerOptions(); |
341 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
342 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
343 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
344 |
runTestsExpectingErrorsOnlyIn17( |
345 |
new String[] { |
346 |
"X.java", |
347 |
"import java.io.File;\n" + |
348 |
"import java.io.FileReader;\n" + |
349 |
"import java.io.IOException;\n" + |
350 |
"public class X {\n" + |
351 |
" void foo() throws IOException {\n" + |
352 |
" File file = new File(\"somefile\");\n" + |
353 |
" FileReader fileReader = null;\n" + |
354 |
" try {\n" + |
355 |
" fileReader = new FileReader(file);\n" + |
356 |
" char[] in = new char[50];\n" + |
357 |
" fileReader.read(in);\n" + |
358 |
" } finally {\n" + |
359 |
" fileReader.close();\n" + |
360 |
" }\n" + |
361 |
" }\n" + |
362 |
"}\n" |
363 |
}, |
364 |
"----------\n" + |
365 |
"1. ERROR in X.java (at line 7)\n" + |
366 |
" FileReader fileReader = null;\n" + |
367 |
" ^^^^^^^^^^\n" + |
368 |
"Resource 'fileReader' should be managed by try-with-resource\n" + |
369 |
"----------\n", |
370 |
options); |
371 |
} |
372 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
373 |
// an AutoCloseable local is re-assigned |
374 |
public void test056g() { |
375 |
Map options = getCompilerOptions(); |
376 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
377 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
378 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
379 |
this.runNegativeTest( |
380 |
new String[] { |
381 |
"X.java", |
382 |
"import java.io.File;\n" + |
383 |
"import java.io.FileReader;\n" + |
384 |
"import java.io.IOException;\n" + |
385 |
"public class X {\n" + |
386 |
" void foo() throws IOException {\n" + |
387 |
" File file = new File(\"somefile\");\n" + |
388 |
" FileReader fileReader = new FileReader(file);\n" + |
389 |
" char[] in = new char[50];\n" + |
390 |
" fileReader.read(in);\n" + |
391 |
" fileReader = new FileReader(file);\n" + |
392 |
" fileReader.read(in);\n" + |
393 |
" fileReader.close();\n" + |
394 |
" fileReader = null;\n" + |
395 |
" }\n" + |
396 |
" public static void main(String[] args) throws IOException {\n" + |
397 |
" new X().foo();\n" + |
398 |
" }\n" + |
399 |
"}\n" |
400 |
}, |
401 |
"----------\n" + |
402 |
"1. ERROR in X.java (at line 10)\n" + |
403 |
" fileReader = new FileReader(file);\n" + |
404 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
405 |
"Resource leak: 'fileReader' is not closed at this location\n" + |
406 |
"----------\n", |
407 |
null, |
408 |
true, |
409 |
options); |
410 |
} |
411 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
412 |
// an AutoCloseable local is re-assigned after null-assigned |
413 |
public void test056g2() { |
414 |
Map options = getCompilerOptions(); |
415 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
416 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
417 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
418 |
this.runNegativeTest( |
419 |
new String[] { |
420 |
"X.java", |
421 |
"import java.io.File;\n" + |
422 |
"import java.io.FileReader;\n" + |
423 |
"import java.io.IOException;\n" + |
424 |
"public class X {\n" + |
425 |
" void foo() throws IOException {\n" + |
426 |
" File file = new File(\"somefile\");\n" + |
427 |
" FileReader fileReader = new FileReader(file);\n" + |
428 |
" char[] in = new char[50];\n" + |
429 |
" fileReader.read(in);\n" + |
430 |
" fileReader = null;\n" + |
431 |
" fileReader = new FileReader(file);\n" + // don't complain again, fileReader is null, so nothing can leak here |
432 |
" fileReader.read(in);\n" + |
433 |
" fileReader.close();\n" + |
434 |
" }\n" + |
435 |
" public static void main(String[] args) throws IOException {\n" + |
436 |
" new X().foo();\n" + |
437 |
" }\n" + |
438 |
"}\n" |
439 |
}, |
440 |
"----------\n" + |
441 |
"1. ERROR in X.java (at line 10)\n" + |
442 |
" fileReader = null;\n" + |
443 |
" ^^^^^^^^^^^^^^^^^\n" + |
444 |
"Resource leak: 'fileReader' is not closed at this location\n" + |
445 |
"----------\n", |
446 |
null, |
447 |
true, |
448 |
options); |
449 |
} |
450 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
451 |
// two AutoCloseables at different nesting levels (anonymous local type) |
452 |
public void test056h() { |
453 |
Map options = getCompilerOptions(); |
454 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
455 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
456 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
457 |
this.runNegativeTest( |
458 |
new String[] { |
459 |
"X.java", |
460 |
"import java.io.File;\n" + |
461 |
"import java.io.FileReader;\n" + |
462 |
"import java.io.IOException;\n" + |
463 |
"public class X {\n" + |
464 |
" void foo() throws IOException {\n" + |
465 |
" final File file = new File(\"somefile\");\n" + |
466 |
" final FileReader fileReader = new FileReader(file);\n" + |
467 |
" char[] in = new char[50];\n" + |
468 |
" fileReader.read(in);\n" + |
469 |
" new Runnable() {\n public void run() {\n" + |
470 |
" try {\n" + |
471 |
" fileReader.close();\n" + |
472 |
" FileReader localReader = new FileReader(file);\n" + |
473 |
" } catch (IOException ex) { /* nop */ }\n" + |
474 |
" }}.run();\n" + |
475 |
" }\n" + |
476 |
" public static void main(String[] args) throws IOException {\n" + |
477 |
" new X().foo();\n" + |
478 |
" }\n" + |
479 |
"}\n" |
480 |
}, |
481 |
"----------\n" + |
482 |
"1. WARNING in X.java (at line 7)\n" + |
483 |
" final FileReader fileReader = new FileReader(file);\n" + |
484 |
" ^^^^^^^^^^\n" + |
485 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
486 |
"----------\n" + |
487 |
"2. ERROR in X.java (at line 14)\n" + |
488 |
" FileReader localReader = new FileReader(file);\n" + |
489 |
" ^^^^^^^^^^^\n" + |
490 |
"Resource leak: 'localReader' is never closed\n" + |
491 |
"----------\n", |
492 |
null, |
493 |
true, |
494 |
options); |
495 |
} |
496 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
497 |
// three AutoCloseables in different blocks of the same method |
498 |
public void test056i() { |
499 |
Map options = getCompilerOptions(); |
500 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
501 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
502 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
503 |
this.runNegativeTest( |
504 |
new String[] { |
505 |
"X.java", |
506 |
"import java.io.File;\n" + |
507 |
"import java.io.FileReader;\n" + |
508 |
"import java.io.IOException;\n" + |
509 |
"public class X {\n" + |
510 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
511 |
" File file = new File(\"somefile\");\n" + |
512 |
" if (f1) {\n" + |
513 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
514 |
" char[] in = new char[50];\n" + |
515 |
" fileReader.read(in);\n" + |
516 |
" while (true) {\n" + |
517 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
518 |
" loopReader.close();" + |
519 |
" break;\n" + |
520 |
" }\n" + |
521 |
" } else {\n" + |
522 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
523 |
" if (f2)\n" + |
524 |
" fileReader.close();\n" + |
525 |
" }\n" + |
526 |
" }\n" + |
527 |
" public static void main(String[] args) throws IOException {\n" + |
528 |
" new X().foo(true, true);\n" + |
529 |
" }\n" + |
530 |
"}\n" |
531 |
}, |
532 |
"----------\n" + |
533 |
"1. ERROR in X.java (at line 8)\n" + |
534 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
535 |
" ^^^^^^^^^^\n" + |
536 |
"Resource leak: 'fileReader' is never closed\n" + |
537 |
"----------\n" + |
538 |
"2. WARNING in X.java (at line 16)\n" + |
539 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
540 |
" ^^^^^^^^^^\n" + |
541 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
542 |
"----------\n", |
543 |
null, |
544 |
true, |
545 |
options); |
546 |
} |
547 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
548 |
// three AutoCloseables in different blocks of the same method - problems ignored |
549 |
public void test056i_ignore() { |
550 |
Map options = getCompilerOptions(); |
551 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.IGNORE); |
552 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.IGNORE); |
553 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
554 |
this.runConformTest( |
555 |
new String[] { |
556 |
"X.java", |
557 |
"import java.io.File;\n" + |
558 |
"import java.io.FileReader;\n" + |
559 |
"import java.io.IOException;\n" + |
560 |
"public class X {\n" + |
561 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
562 |
" File file = new File(\"somefile\");\n" + |
563 |
" if (f1) {\n" + |
564 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
565 |
" char[] in = new char[50];\n" + |
566 |
" fileReader.read(in);\n" + |
567 |
" while (true) {\n" + |
568 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
569 |
" loopReader.close();" + |
570 |
" break;\n" + |
571 |
" }\n" + |
572 |
" } else {\n" + |
573 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
574 |
" if (f2)\n" + |
575 |
" fileReader.close();\n" + |
576 |
" }\n" + |
577 |
" }\n" + |
578 |
"}\n" |
579 |
}, |
580 |
"", |
581 |
null, |
582 |
true, |
583 |
null, |
584 |
options, |
585 |
null); |
586 |
} |
587 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
588 |
// three AutoCloseables in different blocks of the same method |
589 |
public void test056i2() { |
590 |
Map options = getCompilerOptions(); |
591 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
592 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
593 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
594 |
this.runNegativeTest( |
595 |
new String[] { |
596 |
"X.java", |
597 |
"import java.io.File;\n" + |
598 |
"import java.io.FileReader;\n" + |
599 |
"import java.io.IOException;\n" + |
600 |
"public class X {\n" + |
601 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
602 |
" File file = new File(\"somefile\");\n" + |
603 |
" if (f1) {\n" + |
604 |
" FileReader fileReader = new FileReader(file); // properly closed\n" + |
605 |
" char[] in = new char[50];\n" + |
606 |
" fileReader.read(in);\n" + |
607 |
" while (true) {\n" + |
608 |
" fileReader.close();\n" + |
609 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
610 |
" loopReader.close();\n" + |
611 |
" break;\n" + |
612 |
" }\n" + |
613 |
" } else {\n" + |
614 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
615 |
" if (f2)\n" + |
616 |
" fileReader.close();\n" + |
617 |
" }\n" + |
618 |
" }\n" + |
619 |
" public static void main(String[] args) throws IOException {\n" + |
620 |
" new X().foo(true, true);\n" + |
621 |
" }\n" + |
622 |
"}\n" |
623 |
}, |
624 |
"----------\n" + |
625 |
"1. ERROR in X.java (at line 18)\n" + |
626 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
627 |
" ^^^^^^^^^^\n" + |
628 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
629 |
"----------\n", |
630 |
null, |
631 |
true, |
632 |
options); |
633 |
} |
634 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
635 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
636 |
public void test056j() { |
637 |
Map options = getCompilerOptions(); |
638 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
639 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
640 |
this.runNegativeTest( |
641 |
new String[] { |
642 |
"X.java", |
643 |
"import java.io.File;\n" + |
644 |
"import java.io.FileReader;\n" + |
645 |
"import java.io.IOException;\n" + |
646 |
"public class X {\n" + |
647 |
" void foo() throws IOException {\n" + |
648 |
" File file = new File(\"somefile\");\n" + |
649 |
" FileReader fileReader = new FileReader(file);\n" + |
650 |
" read(fileReader);\n" + |
651 |
" }\n" + |
652 |
" void read(FileReader reader) { }\n" + |
653 |
" public static void main(String[] args) throws IOException {\n" + |
654 |
" new X().foo();\n" + |
655 |
" }\n" + |
656 |
"}\n" |
657 |
}, |
658 |
"----------\n" + |
659 |
"1. ERROR in X.java (at line 7)\n" + |
660 |
" FileReader fileReader = new FileReader(file);\n" + |
661 |
" ^^^^^^^^^^\n" + |
662 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
663 |
"----------\n", |
664 |
null, |
665 |
true, |
666 |
options); |
667 |
} |
668 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
669 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
670 |
public void test056jconditional() { |
671 |
Map options = getCompilerOptions(); |
672 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
673 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
674 |
this.runNegativeTest( |
675 |
new String[] { |
676 |
"X.java", |
677 |
"import java.io.File;\n" + |
678 |
"import java.io.FileReader;\n" + |
679 |
"import java.io.IOException;\n" + |
680 |
"public class X {\n" + |
681 |
" void foo(boolean b) throws IOException {\n" + |
682 |
" File file = new File(\"somefile\");\n" + |
683 |
" FileReader fileReader = new FileReader(file);\n" + |
684 |
" synchronized (b ? this : new X()) {\n" + |
685 |
" new ReadDelegator(fileReader);\n" + |
686 |
" }\n" + |
687 |
" }\n" + |
688 |
" class ReadDelegator { ReadDelegator(FileReader reader) { } }\n" + |
689 |
" public static void main(String[] args) throws IOException {\n" + |
690 |
" new X().foo(true);\n" + |
691 |
" }\n" + |
692 |
"}\n" |
693 |
}, |
694 |
"----------\n" + |
695 |
"1. ERROR in X.java (at line 7)\n" + |
696 |
" FileReader fileReader = new FileReader(file);\n" + |
697 |
" ^^^^^^^^^^\n" + |
698 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
699 |
"----------\n", |
700 |
null, |
701 |
true, |
702 |
options); |
703 |
} |
704 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
705 |
// many locals, some are AutoCloseable. |
706 |
// Unfortunately analysis cannot respect how exception exits may affect ra3 and rb3, |
707 |
// doing so would create false positives. |
708 |
public void test056k() { |
709 |
Map options = getCompilerOptions(); |
710 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
711 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
712 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
713 |
String expectedProblems = this.complianceLevel < ClassFileConstants.JDK1_7 ? |
714 |
"----------\n" + |
715 |
"1. ERROR in X.java (at line 15)\n" + |
716 |
" ra2 = new FileReader(file);\n" + |
717 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
718 |
"Resource leak: \'ra2\' is never closed\n" + |
719 |
"----------\n" + |
720 |
"2. ERROR in X.java (at line 28)\n" + |
721 |
" rb2 = new FileReader(file);\n" + |
722 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
723 |
"Resource leak: \'rb2\' is never closed\n" + |
724 |
"----------\n" |
725 |
: |
726 |
"----------\n" + |
727 |
"1. ERROR in X.java (at line 12)\n" + |
728 |
" FileReader ra1 = null, ra2 = null;\n" + |
729 |
" ^^^\n" + |
730 |
"Resource 'ra1' should be managed by try-with-resource\n" + |
731 |
"----------\n" + |
732 |
"2. ERROR in X.java (at line 15)\n" + |
733 |
" ra2 = new FileReader(file);\n" + |
734 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
735 |
"Resource leak: 'ra2' is never closed\n" + |
736 |
"----------\n" + |
737 |
"3. ERROR in X.java (at line 16)\n" + |
738 |
" FileReader ra3 = new FileReader(file);\n" + |
739 |
" ^^^\n" + |
740 |
"Resource 'ra3' should be managed by try-with-resource\n" + |
741 |
"----------\n" + |
742 |
"4. ERROR in X.java (at line 25)\n" + |
743 |
" FileReader rb1 = null, rb2 = null;\n" + |
744 |
" ^^^\n" + |
745 |
"Resource 'rb1' should be managed by try-with-resource\n" + |
746 |
"----------\n" + |
747 |
"5. ERROR in X.java (at line 28)\n" + |
748 |
" rb2 = new FileReader(file);\n" + |
749 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
750 |
"Resource leak: 'rb2' is never closed\n" + |
751 |
"----------\n" + |
752 |
"6. ERROR in X.java (at line 29)\n" + |
753 |
" FileReader rb3 = new FileReader(file);\n" + |
754 |
" ^^^\n" + |
755 |
"Resource 'rb3' should be managed by try-with-resource\n" + |
756 |
"----------\n"; |
757 |
this.runNegativeTest( |
758 |
new String[] { |
759 |
"X.java", |
760 |
"import java.io.File;\n" + |
761 |
"import java.io.FileReader;\n" + |
762 |
"import java.io.IOException;\n" + |
763 |
"public class X {\n" + |
764 |
" void foo() throws IOException {\n" + |
765 |
" int i01, i02, i03, i04, i05, i06, i07, i08, i09,\n" + |
766 |
" i11, i12, i13, i14, i15, i16, i17, i18, i19,\n" + |
767 |
" i21, i22, i23, i24, i25, i26, i27, i28, i29,\n" + |
768 |
" i31, i32, i33, i34, i35, i36, i37, i38, i39,\n" + |
769 |
" i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" + |
770 |
" File file = new File(\"somefile\");\n" + |
771 |
" FileReader ra1 = null, ra2 = null;\n" + |
772 |
" try {\n" + |
773 |
" ra1 = new FileReader(file);\n" + |
774 |
" ra2 = new FileReader(file);\n" + |
775 |
" FileReader ra3 = new FileReader(file);\n" + |
776 |
" char[] in = new char[50];\n" + |
777 |
" ra1.read(in);\n" + |
778 |
" ra2.read(in);\n" + |
779 |
" ra3.close();\n" + |
780 |
" } finally {\n" + |
781 |
" ra1.close();\n" + |
782 |
" }\n" + |
783 |
" int i51, i52, i53, i54, i55, i56, i57, i58, i59, i60;\n" + // beyond this point locals are analyzed using extraBits |
784 |
" FileReader rb1 = null, rb2 = null;\n" + |
785 |
" try {\n" + |
786 |
" rb1 = new FileReader(file);\n" + |
787 |
" rb2 = new FileReader(file);\n" + |
788 |
" FileReader rb3 = new FileReader(file);\n" + |
789 |
" char[] in = new char[50];\n" + |
790 |
" rb1.read(in);\n" + |
791 |
" rb2.read(in);\n" + |
792 |
" rb3.close();\n" + |
793 |
" } finally {\n" + |
794 |
" rb1.close();\n" + |
795 |
" }\n" + |
796 |
" }\n" + |
797 |
" public static void main(String[] args) throws IOException {\n" + |
798 |
" new X().foo();\n" + |
799 |
" }\n" + |
800 |
"}\n" |
801 |
}, |
802 |
expectedProblems, |
803 |
null, |
804 |
true, |
805 |
options); |
806 |
} |
807 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
808 |
// various non-problems |
809 |
public void test056l() { |
810 |
Map options = getCompilerOptions(); |
811 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
812 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
813 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
814 |
String expectedProblems = this.complianceLevel >= ClassFileConstants.JDK1_7 ? |
815 |
"----------\n" + |
816 |
"1. ERROR in X.java (at line 8)\n" + |
817 |
" FileReader fileReader = getReader();\n" + |
818 |
" ^^^^^^^^^^\n" + |
819 |
"Resource 'fileReader' should be managed by try-with-resource\n" + |
820 |
"----------\n" + |
821 |
"2. ERROR in X.java (at line 11)\n" + |
822 |
" FileReader r3 = getReader();\n" + |
823 |
" ^^\n" + |
824 |
"Resource 'r3' should be managed by try-with-resource\n" + |
825 |
"----------\n" + |
826 |
"3. ERROR in X.java (at line 24)\n" + |
827 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
828 |
" ^^\n" + |
829 |
"Potential resource leak: 'r2' may not be closed\n" + |
830 |
"----------\n" |
831 |
: |
832 |
"----------\n" + |
833 |
"1. ERROR in X.java (at line 24)\n" + |
834 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
835 |
" ^^\n" + |
836 |
"Potential resource leak: 'r2' may not be closed\n" + |
837 |
"----------\n"; |
838 |
this.runNegativeTest( |
839 |
new String[] { |
840 |
"X.java", |
841 |
"import java.io.File;\n" + |
842 |
"import java.io.FileReader;\n" + |
843 |
"import java.io.IOException;\n" + |
844 |
"public class X {\n" + |
845 |
" X(FileReader r0) {}\n" + // don't complain against argument |
846 |
" FileReader getReader() { return null; }\n" + |
847 |
" void foo(FileReader r1) throws IOException {\n" + |
848 |
" FileReader fileReader = getReader();\n" + |
849 |
" if (fileReader == null)\n" + |
850 |
" return;\n" + // don't complain, resource is actually null |
851 |
" FileReader r3 = getReader();\n" + |
852 |
" if (r3 == null)\n" + |
853 |
" r3 = new FileReader(new File(\"absent\"));\n" + // don't complain, previous resource is actually null |
854 |
" try {\n" + |
855 |
" char[] in = new char[50];\n" + |
856 |
" fileReader.read(in);\n" + |
857 |
" r1.read(in);\n" + |
858 |
" } finally {\n" + |
859 |
" fileReader.close();\n" + |
860 |
" r3.close();\n" + // the effect of this close() call might be spoiled by exception in fileReader.close() above, but we ignore exception exits in the analysis |
861 |
" }\n" + |
862 |
" }\n" + |
863 |
" public static void main(String[] args) throws IOException {\n" + |
864 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
865 |
" new X(r2).foo(new FileReader(new File(\"notthere\")));\n" + |
866 |
" }\n" + |
867 |
"}\n" |
868 |
}, |
869 |
expectedProblems, |
870 |
null, |
871 |
true, |
872 |
options); |
873 |
} |
874 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
875 |
// nested try with early exit |
876 |
public void test056m() { |
877 |
Map options = getCompilerOptions(); |
878 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
879 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
880 |
this.runConformTest( |
881 |
new String[] { |
882 |
"X.java", |
883 |
"import java.io.File;\n" + |
884 |
"import java.io.FileReader;\n" + |
885 |
"import java.io.IOException;\n" + |
886 |
"public class X {\n" + |
887 |
" void foo() {\n" + |
888 |
" File file = new File(\"somefile\");" + |
889 |
" try {\n" + |
890 |
" FileReader fileReader = new FileReader(file);\n" + |
891 |
" try {\n" + |
892 |
" char[] in = new char[50];\n" + |
893 |
" if (fileReader.read(in)==0)\n" + |
894 |
" return;\n" + |
895 |
" } finally {\n" + |
896 |
" fileReader.close();\n" + |
897 |
" }\n" + |
898 |
" } catch (IOException e) {\n" + |
899 |
" System.out.println(\"caught\");\n" + |
900 |
" }\n" + |
901 |
" }\n" + |
902 |
" public static void main(String[] args) {\n" + |
903 |
" new X().foo();\n" + |
904 |
" }\n" + |
905 |
"}\n" |
906 |
}, |
907 |
"caught", /*output*/ |
908 |
null/*classLibs*/, |
909 |
true/*shouldFlush*/, |
910 |
null/*vmargs*/, |
911 |
options, |
912 |
null/*requestor*/); |
913 |
} |
914 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
915 |
// nested try should not interfere with earlier analysis. |
916 |
public void test056n() { |
917 |
Map options = getCompilerOptions(); |
918 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
919 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
920 |
this.runConformTest( |
921 |
new String[] { |
922 |
"X.java", |
923 |
"import java.io.File;\n" + |
924 |
"import java.io.FileReader;\n" + |
925 |
"import java.io.IOException;\n" + |
926 |
"import java.io.FileNotFoundException;\n" + |
927 |
"public class X {\n" + |
928 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
929 |
" FileReader fr1 = new FileReader(someFile);\n" + |
930 |
" try {\n" + |
931 |
" fr1.read(buf);\n" + |
932 |
" } finally {\n" + |
933 |
" fr1.close();\n" + |
934 |
" }\n" + |
935 |
" try {\n" + |
936 |
" FileReader fr3 = new FileReader(someFile);\n" + |
937 |
" try {\n" + |
938 |
" } finally {\n" + |
939 |
" fr3.close();\n" + |
940 |
" }\n" + |
941 |
" } catch (IOException e) {\n" + |
942 |
" }\n" + |
943 |
" }\n" + |
944 |
" public static void main(String[] args) throws IOException {\n" + |
945 |
" try {\n" + |
946 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
947 |
" } catch (FileNotFoundException e) {\n" + |
948 |
" System.out.println(\"caught\");\n" + |
949 |
" }\n" + |
950 |
" }\n" + |
951 |
"}\n" |
952 |
}, |
953 |
"caught", /*output*/ |
954 |
null/*classLibs*/, |
955 |
true/*shouldFlush*/, |
956 |
null/*vmargs*/, |
957 |
options, |
958 |
null/*requestor*/); |
959 |
} |
960 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
961 |
// if close is guarded by null check this should still be recognized as definitely closed |
962 |
public void test056o() { |
963 |
Map options = getCompilerOptions(); |
964 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
965 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
966 |
this.runConformTest( |
967 |
new String[] { |
968 |
"X.java", |
969 |
"import java.io.File;\n" + |
970 |
"import java.io.FileReader;\n" + |
971 |
"import java.io.IOException;\n" + |
972 |
"import java.io.FileNotFoundException;\n" + |
973 |
"public class X {\n" + |
974 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
975 |
" FileReader fr1 = null;\n" + |
976 |
" try {\n" + |
977 |
" fr1 = new FileReader(someFile);" + |
978 |
" fr1.read(buf);\n" + |
979 |
" } finally {\n" + |
980 |
" if (fr1 != null)\n" + |
981 |
" try {\n" + |
982 |
" fr1.close();\n" + |
983 |
" } catch (IOException e) { /*do nothing*/ }\n" + |
984 |
" }\n" + |
985 |
" }\n" + |
986 |
" public static void main(String[] args) throws IOException {\n" + |
987 |
" try {\n" + |
988 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
989 |
" } catch (FileNotFoundException e) {\n" + |
990 |
" System.out.println(\"caught\");\n" + |
991 |
" }\n" + |
992 |
" }\n" + |
993 |
"}\n" |
994 |
}, |
995 |
"caught", /*output*/ |
996 |
null/*classLibs*/, |
997 |
true/*shouldFlush*/, |
998 |
null/*vmargs*/, |
999 |
options, |
1000 |
null/*requestor*/); |
1001 |
} |
1002 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1003 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
1004 |
// a method uses an AutoCloseable without ever closing it, type from a type variable |
1005 |
public void test056p() { |
1006 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // generics used |
1007 |
Map options = getCompilerOptions(); |
1008 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
1009 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
1010 |
this.runNegativeTest( |
1011 |
new String[] { |
1012 |
"X.java", |
1013 |
"import java.io.File;\n" + |
1014 |
"import java.io.FileReader;\n" + |
1015 |
"import java.io.Reader;\n" + |
1016 |
"import java.io.IOException;\n" + |
1017 |
"public abstract class X <T extends Reader> {\n" + |
1018 |
" void foo() throws IOException {\n" + |
1019 |
" File file = new File(\"somefile\");\n" + |
1020 |
" T fileReader = newReader(file);\n" + |
1021 |
" char[] in = new char[50];\n" + |
1022 |
" fileReader.read(in);\n" + |
1023 |
" }\n" + |
1024 |
" abstract T newReader(File file) throws IOException;\n" + |
1025 |
" public static void main(String[] args) throws IOException {\n" + |
1026 |
" new X<FileReader>() {\n" + |
1027 |
" FileReader newReader(File f) throws IOException { return new FileReader(f); }\n" + |
1028 |
" }.foo();\n" + |
1029 |
" }\n" + |
1030 |
"}\n" |
1031 |
}, |
1032 |
"----------\n" + |
1033 |
"1. ERROR in X.java (at line 8)\n" + |
1034 |
" T fileReader = newReader(file);\n" + |
1035 |
" ^^^^^^^^^^\n" + |
1036 |
"Potential resource leak: \'fileReader\' may not be closed\n" + |
1037 |
"----------\n", |
1038 |
null, |
1039 |
true, |
1040 |
options); |
1041 |
} |
1042 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1043 |
// closed in dead code |
1044 |
public void test056q() { |
1045 |
Map options = getCompilerOptions(); |
1046 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1047 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1048 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
1049 |
this.runNegativeTest( |
1050 |
new String[] { |
1051 |
"X.java", |
1052 |
"import java.io.File;\n" + |
1053 |
"import java.io.FileReader;\n" + |
1054 |
"import java.io.IOException;\n" + |
1055 |
"public class X {\n" + |
1056 |
" void foo() throws IOException {\n" + |
1057 |
" File file = new File(\"somefile\");\n" + |
1058 |
" FileReader fileReader = new FileReader(file);\n" + |
1059 |
" char[] in = new char[50];\n" + |
1060 |
" fileReader.read(in);\n" + |
1061 |
" if (2*2 == 4)\n" + |
1062 |
" return;\n" + |
1063 |
" fileReader.close();\n" + |
1064 |
" }\n" + |
1065 |
" public static void main(String[] args) throws IOException {\n" + |
1066 |
" new X().foo();\n" + |
1067 |
" }\n" + |
1068 |
"}\n" |
1069 |
}, |
1070 |
"----------\n" + |
1071 |
"1. ERROR in X.java (at line 7)\n" + |
1072 |
" FileReader fileReader = new FileReader(file);\n" + |
1073 |
" ^^^^^^^^^^\n" + |
1074 |
"Resource leak: 'fileReader' is never closed\n" + |
1075 |
"----------\n" + |
1076 |
"2. WARNING in X.java (at line 10)\n" + |
1077 |
" if (2*2 == 4)\n" + |
1078 |
" ^^^^^^^^\n" + |
1079 |
"Comparing identical expressions\n" + |
1080 |
"----------\n" + |
1081 |
"3. WARNING in X.java (at line 12)\n" + |
1082 |
" fileReader.close();\n" + |
1083 |
" ^^^^^^^^^^^^^^^^^^\n" + |
1084 |
"Dead code\n" + |
1085 |
"----------\n", |
1086 |
null, |
1087 |
true, |
1088 |
options); |
1089 |
} |
1090 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1091 |
// properly closed, dead code in between |
1092 |
public void test056r() { |
1093 |
Map options = getCompilerOptions(); |
1094 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1095 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1096 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
1097 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1098 |
this.runNegativeTest( |
1099 |
new String[] { |
1100 |
"X.java", |
1101 |
"import java.io.File;\n" + |
1102 |
"import java.io.FileReader;\n" + |
1103 |
"import java.io.IOException;\n" + |
1104 |
"public class X {\n" + |
1105 |
" void foo() throws IOException {\n" + |
1106 |
" File file = new File(\"somefile\");\n" + |
1107 |
" FileReader fr = new FileReader(file);\n" + |
1108 |
" Object b = null;\n" + |
1109 |
" fr.close();\n" + |
1110 |
" if (b != null) {\n" + |
1111 |
" fr = new FileReader(file);\n" + |
1112 |
" return;\n" + |
1113 |
" } else {\n" + |
1114 |
" System.out.print(42);\n" + |
1115 |
" }\n" + |
1116 |
" return; // Should not complain about fr\n" + |
1117 |
" }\n" + |
1118 |
" public static void main(String[] args) throws IOException {\n" + |
1119 |
" new X().foo();\n" + |
1120 |
" }\n" + |
1121 |
"}\n" |
1122 |
}, |
1123 |
"----------\n" + |
1124 |
"1. ERROR in X.java (at line 10)\n" + |
1125 |
" if (b != null) {\n" + |
1126 |
" fr = new FileReader(file);\n" + |
1127 |
" return;\n" + |
1128 |
" } else {\n" + |
1129 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
1130 |
"Dead code\n" + |
1131 |
"----------\n" + |
1132 |
"2. WARNING in X.java (at line 13)\n" + |
1133 |
" } else {\n" + |
1134 |
" System.out.print(42);\n" + |
1135 |
" }\n" + |
1136 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
1137 |
"Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally\n" + |
1138 |
"----------\n", |
1139 |
null, |
1140 |
true, |
1141 |
options); |
1142 |
} |
1143 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1144 |
// resource inside t-w-r is re-assigned, shouldn't even record an errorLocation |
1145 |
public void test056s() { |
1146 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; // t-w-r used |
1147 |
Map options = getCompilerOptions(); |
1148 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1149 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1150 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
1151 |
this.runNegativeTest( |
1152 |
new String[] { |
1153 |
"X.java", |
1154 |
"import java.io.File;\n" + |
1155 |
"import java.io.FileReader;\n" + |
1156 |
"import java.io.IOException;\n" + |
1157 |
"public class X {\n" + |
1158 |
" void foo() throws IOException {\n" + |
1159 |
" File file = new File(\"somefile\");\n" + |
1160 |
" try (FileReader fileReader = new FileReader(file);) {\n" + |
1161 |
" char[] in = new char[50];\n" + |
1162 |
" fileReader.read(in);\n" + |
1163 |
" fileReader = new FileReader(file); // debug here\n" + |
1164 |
" fileReader.read(in);\n" + |
1165 |
" }\n" + |
1166 |
" }\n" + |
1167 |
" public static void main(String[] args) throws IOException {\n" + |
1168 |
" new X().foo();\n" + |
1169 |
" }\n" + |
1170 |
"}\n" |
1171 |
}, |
1172 |
"----------\n" + |
1173 |
"1. ERROR in X.java (at line 10)\n" + |
1174 |
" fileReader = new FileReader(file); // debug here\n" + |
1175 |
" ^^^^^^^^^^\n" + |
1176 |
"The resource fileReader of a try-with-resources statement cannot be assigned\n" + |
1177 |
"----------\n", |
1178 |
null, |
1179 |
true, |
1180 |
options); |
1181 |
} |
1182 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1183 |
// resource is closed, dead code follows |
1184 |
public void test056t() { |
1185 |
Map options = getCompilerOptions(); |
1186 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1187 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1188 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1189 |
this.runNegativeTest( |
1190 |
new String[] { |
1191 |
"X.java", |
1192 |
"import java.io.FileReader;\n" + |
1193 |
"import java.io.IOException;\n" + |
1194 |
"public class X {\n" + |
1195 |
" void foo31() throws IOException {\n" + |
1196 |
" FileReader reader = new FileReader(\"file\"); //warning\n" + |
1197 |
" if (reader != null) {\n" + |
1198 |
" reader.close();\n" + |
1199 |
" } else {\n" + |
1200 |
" // nop\n" + |
1201 |
" }\n" + |
1202 |
" }\n" + |
1203 |
" public static void main(String[] args) throws IOException {\n" + |
1204 |
" new X().foo31();\n" + |
1205 |
" }\n" + |
1206 |
"}\n" |
1207 |
}, |
1208 |
"----------\n" + |
1209 |
"1. ERROR in X.java (at line 8)\n" + |
1210 |
" } else {\n" + |
1211 |
" // nop\n" + |
1212 |
" }\n" + |
1213 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
1214 |
"Dead code\n" + |
1215 |
"----------\n", |
1216 |
null, |
1217 |
true, |
1218 |
options); |
1219 |
} |
1220 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1221 |
// resource is reassigned within t-w-r with different resource |
1222 |
// was initially broken due to https://bugs.eclipse.org/358827 |
1223 |
public void test056u() { |
1224 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; // t-w-r used |
1225 |
Map options = getCompilerOptions(); |
1226 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1227 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1228 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1229 |
this.runNegativeTest( |
1230 |
new String[] { |
1231 |
"X.java", |
1232 |
"import java.io.FileReader;\n" + |
1233 |
"public class X {\n" + |
1234 |
" void foo() throws Exception {\n" + |
1235 |
" FileReader reader1 = new FileReader(\"file1\");\n" + |
1236 |
" FileReader reader2 = new FileReader(\"file2\");\n" + |
1237 |
" reader2 = reader1;// this disconnects reader 2\n" + |
1238 |
" try (FileReader reader3 = new FileReader(\"file3\")) {\n" + |
1239 |
" int ch;\n" + |
1240 |
" while ((ch = reader2.read()) != -1) {\n" + |
1241 |
" System.out.println(ch);\n" + |
1242 |
" reader1.read();\n" + |
1243 |
" }\n" + |
1244 |
" reader2 = reader1; // warning 1 regarding original reader1\n" + // this warning was missing |
1245 |
" reader2 = reader1; // warning 2 regarding original reader1\n" + |
1246 |
" } finally {\n" + |
1247 |
" if (reader2 != null) {\n" + |
1248 |
" reader2.close();\n" + |
1249 |
" } else {\n" + |
1250 |
" System.out.println();\n" + |
1251 |
" }\n" + |
1252 |
" }\n" + |
1253 |
" }\n" + |
1254 |
"}\n" |
1255 |
}, |
1256 |
"----------\n" + |
1257 |
"1. ERROR in X.java (at line 5)\n" + |
1258 |
" FileReader reader2 = new FileReader(\"file2\");\n" + |
1259 |
" ^^^^^^^\n" + |
1260 |
"Resource leak: 'reader2' is never closed\n" + |
1261 |
"----------\n" + |
1262 |
"2. ERROR in X.java (at line 13)\n" + |
1263 |
" reader2 = reader1; // warning 1 regarding original reader1\n" + |
1264 |
" ^^^^^^^^^^^^^^^^^\n" + |
1265 |
"Resource leak: 'reader1' is not closed at this location\n" + |
1266 |
"----------\n" + |
1267 |
"3. ERROR in X.java (at line 14)\n" + |
1268 |
" reader2 = reader1; // warning 2 regarding original reader1\n" + |
1269 |
" ^^^^^^^^^^^^^^^^^\n" + |
1270 |
"Resource leak: 'reader1' is not closed at this location\n" + |
1271 |
"----------\n", |
1272 |
null, |
1273 |
true, |
1274 |
options); |
1275 |
} |
1276 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1277 |
// scope-related pbs reported in https://bugs.eclipse.org/349326#c70 and https://bugs.eclipse.org/349326#c82 |
1278 |
public void test056v() { |
1279 |
Map options = getCompilerOptions(); |
1280 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1281 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1282 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.WARNING); |
1283 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1284 |
String expectedProblems = this.complianceLevel >= ClassFileConstants.JDK1_7 ? |
1285 |
"----------\n" + |
1286 |
"1. ERROR in X.java (at line 4)\n" + |
1287 |
" FileReader reader = new FileReader(\"file\");\n" + |
1288 |
" ^^^^^^\n" + |
1289 |
"Resource leak: 'reader' is never closed\n" + |
1290 |
"----------\n" + |
1291 |
"2. WARNING in X.java (at line 19)\n" + |
1292 |
" FileReader reader111 = new FileReader(\"file2\");\n" + |
1293 |
" ^^^^^^^^^\n" + |
1294 |
"Resource 'reader111' should be managed by try-with-resource\n" + |
1295 |
"----------\n" + |
1296 |
"3. ERROR in X.java (at line 42)\n" + |
1297 |
" return;\n" + |
1298 |
" ^^^^^^^\n" + |
1299 |
"Resource leak: 'reader2' is not closed at this location\n" + |
1300 |
"----------\n" |
1301 |
: |
1302 |
"----------\n" + |
1303 |
"1. ERROR in X.java (at line 4)\n" + |
1304 |
" FileReader reader = new FileReader(\"file\");\n" + |
1305 |
" ^^^^^^\n" + |
1306 |
"Resource leak: 'reader' is never closed\n" + |
1307 |
"----------\n" + |
1308 |
"2. ERROR in X.java (at line 42)\n" + |
1309 |
" return;\n" + |
1310 |
" ^^^^^^^\n" + |
1311 |
"Resource leak: 'reader2' is not closed at this location\n" + |
1312 |
"----------\n"; |
1313 |
this.runNegativeTest( |
1314 |
new String[] { |
1315 |
"X.java", |
1316 |
"import java.io.FileReader;\n" + |
1317 |
"public class X {\n" + |
1318 |
" boolean foo1() throws Exception {\n" + |
1319 |
" FileReader reader = new FileReader(\"file\");\n" + |
1320 |
" try {\n" + |
1321 |
" int ch;\n" + |
1322 |
" while ((ch = reader.read()) != -1) {\n" + |
1323 |
" System.out.println(ch);\n" + |
1324 |
" reader.read();\n" + |
1325 |
" }\n" + |
1326 |
" if (ch > 10) {\n" + |
1327 |
" return true;\n" + |
1328 |
" }\n" + |
1329 |
" return false;\n" + // return while resource from enclosing scope remains unclosed |
1330 |
" } finally {\n" + |
1331 |
" }\n" + |
1332 |
" }\n" + |
1333 |
" void foo111() throws Exception {\n" + |
1334 |
" FileReader reader111 = new FileReader(\"file2\");\n" + |
1335 |
" try {\n" + |
1336 |
" int ch;\n" + |
1337 |
" while ((ch = reader111.read()) != -1) {\n" + |
1338 |
" System.out.println(ch);\n" + |
1339 |
" reader111.read();\n" + |
1340 |
" }\n" + |
1341 |
" return;\n" + // this shouldn't spoil the warning "should be managed with t-w-r" |
1342 |
" } finally {\n" + |
1343 |
" if (reader111 != null) {\n" + |
1344 |
" reader111.close();\n" + |
1345 |
" }\n" + |
1346 |
" }\n" + |
1347 |
" }\n" + |
1348 |
" void foo2() throws Exception {\n" + |
1349 |
" FileReader reader2 = new FileReader(\"file\");\n" + |
1350 |
" try {\n" + |
1351 |
" int ch;\n" + |
1352 |
" while ((ch = reader2.read()) != -1) {\n" + |
1353 |
" System.out.println(ch);\n" + |
1354 |
" reader2.read();\n" + |
1355 |
" }\n" + |
1356 |
" if (ch > 10) {\n" + |
1357 |
" return;\n" + // potential leak |
1358 |
" }\n" + |
1359 |
" } finally {\n" + |
1360 |
" }\n" + |
1361 |
" reader2.close();\n" + // due to this close we don't say "never closed" |
1362 |
" }\n" + |
1363 |
"}\n" |
1364 |
}, |
1365 |
expectedProblems, |
1366 |
null, |
1367 |
true, |
1368 |
options); |
1369 |
} |
1370 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1371 |
// end of method is dead end, but before we have both a close() and an early return |
1372 |
public void test056w() { |
1373 |
Map options = getCompilerOptions(); |
1374 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1375 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1376 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1377 |
this.runNegativeTest( |
1378 |
new String[] { |
1379 |
"X.java", |
1380 |
"import java.io.FileReader;\n" + |
1381 |
"public class X {\n" + |
1382 |
" boolean foo1() throws Exception {\n" + |
1383 |
" FileReader reader = new FileReader(\"file\");\n" + |
1384 |
" try {\n" + |
1385 |
" int ch;\n" + |
1386 |
" while ((ch = reader.read()) != -1) {\n" + |
1387 |
" System.out.println(ch);\n" + |
1388 |
" reader.read();\n" + |
1389 |
" }\n" + |
1390 |
" if (ch > 10) {\n" + |
1391 |
" reader.close();\n" + |
1392 |
" return true;\n" + |
1393 |
" }\n" + |
1394 |
" return false;\n" + |
1395 |
" } finally {\n" + |
1396 |
" }\n" + |
1397 |
" }\n" + |
1398 |
"}\n" |
1399 |
}, |
1400 |
"----------\n" + |
1401 |
"1. ERROR in X.java (at line 15)\n" + |
1402 |
" return false;\n" + |
1403 |
" ^^^^^^^^^^^^^\n" + |
1404 |
"Resource leak: 'reader' is not closed at this location\n" + |
1405 |
"----------\n", |
1406 |
null, |
1407 |
true, |
1408 |
options); |
1409 |
} |
1410 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1411 |
// different early exits, if no close seen report as definitely unclosed |
1412 |
public void test056x() { |
1413 |
Map options = getCompilerOptions(); |
1414 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1415 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1416 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1417 |
this.runNegativeTest( |
1418 |
new String[] { |
1419 |
"X.java", |
1420 |
"import java.io.FileReader;\n" + |
1421 |
"public class X {\n" + |
1422 |
" void foo31(boolean b) throws Exception {\n" + |
1423 |
" FileReader reader = new FileReader(\"file\");\n" + |
1424 |
" if (b) {\n" + |
1425 |
" reader.close();\n" + |
1426 |
" } else {\n" + |
1427 |
" return; // warning\n" + |
1428 |
" }\n" + |
1429 |
" }\n" + |
1430 |
" void foo32(boolean b) throws Exception {\n" + |
1431 |
" FileReader reader = new FileReader(\"file\"); // warn here\n" + |
1432 |
" return;\n" + |
1433 |
" }\n" + |
1434 |
"}\n" |
1435 |
}, |
1436 |
"----------\n" + |
1437 |
"1. ERROR in X.java (at line 8)\n" + |
1438 |
" return; // warning\n" + |
1439 |
" ^^^^^^^\n" + |
1440 |
"Resource leak: 'reader' is not closed at this location\n" + |
1441 |
"----------\n" + |
1442 |
"2. ERROR in X.java (at line 12)\n" + |
1443 |
" FileReader reader = new FileReader(\"file\"); // warn here\n" + |
1444 |
" ^^^^^^\n" + |
1445 |
"Resource leak: 'reader' is never closed\n" + |
1446 |
"----------\n", |
1447 |
null, |
1448 |
true, |
1449 |
options); |
1450 |
} |
1451 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1452 |
// nested method passes the resource to outside code |
1453 |
public void test056y() { |
1454 |
Map options = getCompilerOptions(); |
1455 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1456 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
1457 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1458 |
this.runNegativeTest( |
1459 |
new String[] { |
1460 |
"X.java", |
1461 |
"import java.io.FileReader;\n" + |
1462 |
"public class X {\n" + |
1463 |
" void foo31(boolean b) throws Exception {\n" + |
1464 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
1465 |
" new Runnable() {\n" + |
1466 |
" public void run() {\n" + |
1467 |
" foo18(reader31);\n" + |
1468 |
" }\n" + |
1469 |
" }.run();\n" + |
1470 |
" }\n" + |
1471 |
" void foo18(FileReader r18) {\n" + |
1472 |
" // could theoretically close r18;\n" + |
1473 |
" }\n" + |
1474 |
" abstract class ResourceProvider {\n" + |
1475 |
" abstract FileReader provide();" + |
1476 |
" }\n" + |
1477 |
" ResourceProvider provider;" + |
1478 |
" void foo23() throws Exception {\n" + |
1479 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
1480 |
" provider = new ResourceProvider() {\n" + |
1481 |
" public FileReader provide() {\n" + |
1482 |
" return reader23;\n" + |
1483 |
" }\n" + |
1484 |
" };\n" + |
1485 |
" }\n" + |
1486 |
"}\n" |
1487 |
}, |
1488 |
"----------\n" + |
1489 |
"1. WARNING in X.java (at line 4)\n" + |
1490 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
1491 |
" ^^^^^^^^\n" + |
1492 |
"Potential resource leak: 'reader31' may not be closed\n" + |
1493 |
"----------\n" + |
1494 |
"2. WARNING in X.java (at line 17)\n" + |
1495 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
1496 |
" ^^^^^^^^\n" + |
1497 |
"Potential resource leak: 'reader23' may not be closed\n" + |
1498 |
"----------\n", |
1499 |
null, |
1500 |
true, |
1501 |
options); |
1502 |
} |
1503 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1504 |
// resource assigned to second local and is (potentially) closed on the latter |
1505 |
public void test056z() { |
1506 |
Map options = getCompilerOptions(); |
1507 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1508 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1509 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1510 |
this.runNegativeTest( |
1511 |
new String[] { |
1512 |
"X.java", |
1513 |
"import java.io.FileReader;\n" + |
1514 |
"public class X {\n" + |
1515 |
" void foo17() throws Exception {\n" + |
1516 |
" FileReader reader17 = new FileReader(\"file\");\n" + |
1517 |
" final FileReader readerCopy = reader17;\n" + |
1518 |
" readerCopy.close();\n" + |
1519 |
" }\n" + |
1520 |
" void foo17a() throws Exception {\n" + |
1521 |
" FileReader reader17a = new FileReader(\"file\");\n" + |
1522 |
" FileReader readerCopya;" + |
1523 |
" readerCopya = reader17a;\n" + |
1524 |
" bar(readerCopya);\n" + // potentially closes |
1525 |
" }\n" + |
1526 |
" void bar(FileReader r) {}\n" + |
1527 |
"}\n" |
1528 |
}, |
1529 |
"----------\n" + |
1530 |
"1. ERROR in X.java (at line 9)\n" + |
1531 |
" FileReader reader17a = new FileReader(\"file\");\n" + |
1532 |
" ^^^^^^^^^\n" + |
1533 |
"Potential resource leak: 'reader17a' may not be closed\n" + |
1534 |
"----------\n", |
1535 |
null, |
1536 |
true, |
1537 |
options); |
1538 |
} |
1539 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1540 |
// multiple early exists from nested scopes (always closed) |
1541 |
public void test056zz() { |
1542 |
Map options = getCompilerOptions(); |
1543 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1544 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1545 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
1546 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1547 |
runTestsExpectingErrorsOnlyIn17( |
1548 |
new String[] { |
1549 |
"X.java", |
1550 |
"import java.io.FileReader;\n" + |
1551 |
"public class X {\n" + |
1552 |
" void foo16() throws Exception {\n" + |
1553 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
1554 |
" try {\n" + |
1555 |
" reader16.close();\n " + |
1556 |
" return;\n" + |
1557 |
" } catch (RuntimeException re) {\n" + |
1558 |
" return;\n" + |
1559 |
" } catch (Error e) {\n" + |
1560 |
" return;\n" + |
1561 |
" } finally {\n" + |
1562 |
" reader16.close();\n " + |
1563 |
" }\n" + |
1564 |
" }\n" + |
1565 |
"}\n" |
1566 |
}, |
1567 |
"----------\n" + |
1568 |
"1. ERROR in X.java (at line 4)\n" + |
1569 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
1570 |
" ^^^^^^^^\n" + |
1571 |
"Resource 'reader16' should be managed by try-with-resource\n" + |
1572 |
"----------\n", |
1573 |
options); |
1574 |
} |
1575 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
1576 |
// multiple early exists from nested scopes (never closed) |
1577 |
public void test056zzz() { |
1578 |
Map options = getCompilerOptions(); |
1579 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1580 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1581 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
1582 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1583 |
this.runNegativeTest( |
1584 |
new String[] { |
1585 |
"X.java", |
1586 |
"import java.io.FileReader;\n" + |
1587 |
"public class X {\n" + |
1588 |
" void foo16() throws Exception {\n" + |
1589 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
1590 |
" try {\n" + |
1591 |
" return;\n" + |
1592 |
" } catch (RuntimeException re) {\n" + |
1593 |
" return;\n" + |
1594 |
" } catch (Error e) {\n" + |
1595 |
" return;\n" + |
1596 |
" } finally {\n" + |
1597 |
" System.out.println();\n " + |
1598 |
" }\n" + |
1599 |
" }\n" + |
1600 |
"}\n" |
1601 |
}, |
1602 |
"----------\n" + |
1603 |
"1. ERROR in X.java (at line 4)\n" + |
1604 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
1605 |
" ^^^^^^^^\n" + |
1606 |
"Resource leak: 'reader16' is never closed\n" + |
1607 |
"----------\n", |
1608 |
null, |
1609 |
true, |
1610 |
options); |
1611 |
} |
1612 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
1613 |
// explicit throw is a true method exit here |
1614 |
public void test056throw1() { |
1615 |
Map options = getCompilerOptions(); |
1616 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1617 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1618 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
1619 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1620 |
this.runNegativeTest( |
1621 |
new String[] { |
1622 |
"X.java", |
1623 |
"import java.io.FileReader;\n" + |
1624 |
"public class X {\n" + |
1625 |
" void foo2(boolean a, boolean b, boolean c) throws Exception {\n" + |
1626 |
" FileReader reader = new FileReader(\"file\");\n" + |
1627 |
" if(a)\n" + |
1628 |
" throw new Exception(); //warning 1\n" + |
1629 |
" else if (b)\n" + |
1630 |
" reader.close();\n" + |
1631 |
" else if(c)\n" + |
1632 |
" throw new Exception(); //warning 2\n" + |
1633 |
" reader.close();\n" + |
1634 |
" }\n" + |
1635 |
"}\n" |
1636 |
}, |
1637 |
"----------\n" + |
1638 |
"1. ERROR in X.java (at line 6)\n" + |
1639 |
" throw new Exception(); //warning 1\n" + |
1640 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
1641 |
"Resource leak: 'reader' is not closed at this location\n" + |
1642 |
"----------\n" + |
1643 |
"2. ERROR in X.java (at line 10)\n" + |
1644 |
" throw new Exception(); //warning 2\n" + |
1645 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
1646 |
"Resource leak: 'reader' is not closed at this location\n" + |
1647 |
"----------\n", |
1648 |
null, |
1649 |
true, |
1650 |
options); |
1651 |
} |
1652 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
1653 |
// close() within finally provides protection for throw |
1654 |
public void test056throw2() { |
1655 |
Map options = getCompilerOptions(); |
1656 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1657 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1658 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
1659 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1660 |
runTestsExpectingErrorsOnlyIn17( |
1661 |
new String[] { |
1662 |
"X.java", |
1663 |
"import java.io.FileReader;\n" + |
1664 |
"public class X {\n" + |
1665 |
" void foo1() throws Exception {\n" + |
1666 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1667 |
" try {\n" + |
1668 |
" reader.read();\n" + |
1669 |
" return;\n" + |
1670 |
" } catch (Exception e) {\n" + |
1671 |
" throw new Exception();\n" + |
1672 |
" } finally {\n" + |
1673 |
" reader.close();\n" + |
1674 |
" }\n" + |
1675 |
" }\n" + |
1676 |
"\n" + |
1677 |
" void foo2() throws Exception {\n" + |
1678 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1679 |
" try {\n" + |
1680 |
" reader.read();\n" + |
1681 |
" throw new Exception(); // should not warn here\n" + |
1682 |
" } catch (Exception e) {\n" + |
1683 |
" throw new Exception();\n" + |
1684 |
" } finally {\n" + |
1685 |
" reader.close();\n" + |
1686 |
" }\n" + |
1687 |
" }\n" + |
1688 |
"\n" + |
1689 |
" void foo3() throws Exception {\n" + |
1690 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1691 |
" try {\n" + |
1692 |
" reader.read();\n" + |
1693 |
" throw new Exception();\n" + |
1694 |
" } finally {\n" + |
1695 |
" reader.close();\n" + |
1696 |
" }\n" + |
1697 |
" }\n" + |
1698 |
"}\n" |
1699 |
}, |
1700 |
"----------\n" + |
1701 |
"1. ERROR in X.java (at line 4)\n" + |
1702 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1703 |
" ^^^^^^\n" + |
1704 |
"Resource 'reader' should be managed by try-with-resource\n" + |
1705 |
"----------\n" + |
1706 |
"2. ERROR in X.java (at line 16)\n" + |
1707 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1708 |
" ^^^^^^\n" + |
1709 |
"Resource 'reader' should be managed by try-with-resource\n" + |
1710 |
"----------\n" + |
1711 |
"3. ERROR in X.java (at line 28)\n" + |
1712 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1713 |
" ^^^^^^\n" + |
1714 |
"Resource 'reader' should be managed by try-with-resource\n" + |
1715 |
"----------\n", |
1716 |
options); |
1717 |
} |
1718 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
1719 |
// close() nested within finally provides protection for throw |
1720 |
public void test056throw3() { |
1721 |
Map options = getCompilerOptions(); |
1722 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1723 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1724 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
1725 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1726 |
runTestsExpectingErrorsOnlyIn17( |
1727 |
new String[] { |
1728 |
"X.java", |
1729 |
"import java.io.FileReader;\n" + |
1730 |
"public class X {\n" + |
1731 |
" void foo2x() throws Exception {\n" + |
1732 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1733 |
" try {\n" + |
1734 |
" reader.read();\n" + |
1735 |
" throw new Exception(); // should not warn here\n" + |
1736 |
" } catch (Exception e) {\n" + |
1737 |
" throw new Exception();\n" + |
1738 |
" } finally {\n" + |
1739 |
" if (reader != null)\n" + |
1740 |
" try {\n" + |
1741 |
" reader.close();\n" + |
1742 |
" } catch (java.io.IOException io) {}\n" + |
1743 |
" }\n" + |
1744 |
" }\n" + |
1745 |
"}\n" |
1746 |
}, |
1747 |
"----------\n" + |
1748 |
"1. ERROR in X.java (at line 4)\n" + |
1749 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
1750 |
" ^^^^^^\n" + |
1751 |
"Resource 'reader' should be managed by try-with-resource\n" + |
1752 |
"----------\n", |
1753 |
options); |
1754 |
} |
1755 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
1756 |
// additional boolean should shed doubt on whether we reach the close() call |
1757 |
public void test056throw4() { |
1758 |
Map options = getCompilerOptions(); |
1759 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1760 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1761 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
1762 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1763 |
this.runNegativeTest( |
1764 |
new String[] { |
1765 |
"X.java", |
1766 |
"import java.io.FileReader;\n" + |
1767 |
"public class X {\n" + |
1768 |
" void foo2x(boolean b) throws Exception {\n" + |
1769 |
" FileReader reader = new FileReader(\"file\");\n" + |
1770 |
" try {\n" + |
1771 |
" reader.read();\n" + |
1772 |
" throw new Exception(); // should warn here\n" + |
1773 |
" } catch (Exception e) {\n" + |
1774 |
" throw new Exception(); // should warn here\n" + |
1775 |
" } finally {\n" + |
1776 |
" if (reader != null && b)\n" + // this condition is too strong to protect reader |
1777 |
" try {\n" + |
1778 |
" reader.close();\n" + |
1779 |
" } catch (java.io.IOException io) {}\n" + |
1780 |
" }\n" + |
1781 |
" }\n" + |
1782 |
"}\n" |
1783 |
}, |
1784 |
"----------\n" + |
1785 |
"1. ERROR in X.java (at line 7)\n" + |
1786 |
" throw new Exception(); // should warn here\n" + |
1787 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
1788 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
1789 |
"----------\n" + |
1790 |
"2. ERROR in X.java (at line 9)\n" + |
1791 |
" throw new Exception(); // should warn here\n" + |
1792 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
1793 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
1794 |
"----------\n", |
1795 |
null, |
1796 |
true, |
1797 |
options); |
1798 |
} |
1799 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
1800 |
// similar to test056throw3() but indirectly calling close(), so doubts remain. |
1801 |
public void test056throw5() { |
1802 |
Map options = getCompilerOptions(); |
1803 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1804 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
1805 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
1806 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
1807 |
this.runNegativeTest( |
1808 |
new String[] { |
1809 |
"X.java", |
1810 |
"import java.io.FileReader;\n" + |
1811 |
"public class X {\n" + |
1812 |
" void foo2x() throws Exception {\n" + |
1813 |
" FileReader reader = new FileReader(\"file\");\n" + |
1814 |
" try {\n" + |
1815 |
" reader.read();\n" + |
1816 |
" throw new Exception(); // should warn 'may not' here\n" + |
1817 |
" } catch (Exception e) {\n" + |
1818 |
" throw new Exception(); // should warn 'may not' here\n" + |
1819 |
" } finally {\n" + |
1820 |
" doClose(reader);\n" + |
1821 |
" }\n" + |
1822 |
" }\n" + |
1823 |
" void doClose(FileReader r) { try { r.close(); } catch (java.io.IOException ex) {}}\n" + |
1824 |
"}\n" |
1825 |
}, |
1826 |
"----------\n" + |
1827 |
"1. ERROR in X.java (at line 7)\n" + |
1828 |
" throw new Exception(); // should warn \'may not\' here\n" + |
1829 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
1830 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
1831 |
"----------\n" + |
1832 |
"2. ERROR in X.java (at line 9)\n" + |
1833 |
" throw new Exception(); // should warn \'may not\' here\n" + |
1834 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
1835 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
1836 |
"----------\n", |
1837 |
null, |
1838 |
true, |
1839 |
options); |
1840 |
} |
1841 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
1842 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
1843 |
// a resource wrapper is not closed but the underlying resource is |
1844 |
public void test061a() { |
1845 |
Map options = getCompilerOptions(); |
1846 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
1847 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
1848 |
this.runConformTest( |
1849 |
new String[] { |
1850 |
"X.java", |
1851 |
"import java.io.File;\n" + |
1852 |
"import java.io.BufferedInputStream;\n" + |
1853 |
"import java.io.FileInputStream;\n" + |
1854 |
"import java.io.IOException;\n" + |
1855 |
"public class X {\n" + |
1856 |
" void foo() throws IOException {\n" + |
1857 |
" File file = new File(\"somefile\");\n" + |
1858 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
1859 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
1860 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
1861 |
" System.out.println(bis.available());\n" + |
1862 |
" fileStream.close();\n" + |
1863 |
" }\n" + |
1864 |
" void inline() throws IOException {\n" + |
1865 |
" File file = new File(\"somefile\");\n" + |
1866 |
" FileInputStream fileStream;\n" + |
1867 |
" BufferedInputStream bis = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
1868 |
" System.out.println(bis.available());\n" + |
1869 |
" fileStream.close();\n" + |
1870 |
" }\n" + |
1871 |
" public static void main(String[] args) throws IOException {\n" + |
1872 |
" try {\n" + |
1873 |
" new X().foo();\n" + |
1874 |
" } catch (IOException ex) {" + |
1875 |
" System.out.println(\"Got IO Exception\");\n" + |
1876 |
" }\n" + |
1877 |
" }\n" + |
1878 |
"}\n" |
1879 |
}, |
1880 |
"Got IO Exception", |
1881 |
null, |
1882 |
true, |
1883 |
null, |
1884 |
options, |
1885 |
null); |
1886 |
} |
1887 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
1888 |
// a closeable without OS resource is not closed |
1889 |
public void test061b() { |
1890 |
Map options = getCompilerOptions(); |
1891 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
1892 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
1893 |
this.runConformTest( |
1894 |
new String[] { |
1895 |
"X.java", |
1896 |
"import java.io.StringReader;\n" + |
1897 |
"import java.io.IOException;\n" + |
1898 |
"public class X {\n" + |
1899 |
" void foo() throws IOException {\n" + |
1900 |
" StringReader string = new StringReader(\"content\");\n" + |
1901 |
" System.out.println(string.read());\n" + |
1902 |
" }\n" + |
1903 |
" public static void main(String[] args) throws IOException {\n" + |
1904 |
" new X().foo();\n" + |
1905 |
" }\n" + |
1906 |
"}\n" |
1907 |
}, |
1908 |
"99", // character 'c' |
1909 |
null, |
1910 |
true, |
1911 |
null, |
1912 |
options, |
1913 |
null); |
1914 |
} |
1915 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
1916 |
// a resource wrapper is not closed but the underlying closeable is resource-free |
1917 |
public void test061c() { |
1918 |
Map options = getCompilerOptions(); |
1919 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
1920 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
1921 |
this.runConformTest( |
1922 |
new String[] { |
1923 |
"X.java", |
1924 |
"import java.io.BufferedReader;\n" + |
1925 |
"import java.io.StringReader;\n" + |
1926 |
"import java.io.IOException;\n" + |
1927 |
"public class X {\n" + |
1928 |
" void foo() throws IOException {\n" + |
1929 |
" StringReader input = new StringReader(\"content\");\n" + |
1930 |
" BufferedReader br = new BufferedReader(input);\n" + |
1931 |
" BufferedReader doubleWrap = new BufferedReader(br);\n" + |
1932 |
" System.out.println(br.read());\n" + |
1933 |
" }\n" + |
1934 |
" void inline() throws IOException {\n" + |
1935 |
" BufferedReader br = new BufferedReader(new StringReader(\"content\"));\n" + |
1936 |
" System.out.println(br.read());\n" + |
1937 |
" }\n" + |
1938 |
" public static void main(String[] args) throws IOException {\n" + |
1939 |
" new X().foo();\n" + |
1940 |
" }\n" + |
1941 |
"}\n" |
1942 |
}, |
1943 |
"99", |
1944 |
null, |
1945 |
true, |
1946 |
null, |
1947 |
options, |
1948 |
null); |
1949 |
} |
1950 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
1951 |
// a resource wrapper is not closed neither is the underlying resource |
1952 |
public void test061d() { |
1953 |
Map options = getCompilerOptions(); |
1954 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
1955 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
1956 |
this.runNegativeTest( |
1957 |
new String[] { |
1958 |
"X.java", |
1959 |
"import java.io.File;\n" + |
1960 |
"import java.io.BufferedInputStream;\n" + |
1961 |
"import java.io.FileInputStream;\n" + |
1962 |
"import java.io.IOException;\n" + |
1963 |
"public class X {\n" + |
1964 |
" void foo() throws IOException {\n" + |
1965 |
" File file = new File(\"somefile\");\n" + |
1966 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
1967 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
1968 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
1969 |
" System.out.println(bis.available());\n" + |
1970 |
" }\n" + |
1971 |
" void inline() throws IOException {\n" + |
1972 |
" File file = new File(\"somefile\");\n" + |
1973 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
1974 |
" System.out.println(bis2.available());\n" + |
1975 |
" }\n" + |
1976 |
" public static void main(String[] args) throws IOException {\n" + |
1977 |
" try {\n" + |
1978 |
" new X().foo();\n" + |
1979 |
" } catch (IOException ex) {" + |
1980 |
" System.out.println(\"Got IO Exception\");\n" + |
1981 |
" }\n" + |
1982 |
" }\n" + |
1983 |
"}\n" |
1984 |
}, |
1985 |
"----------\n" + |
1986 |
"1. ERROR in X.java (at line 10)\n" + |
1987 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
1988 |
" ^^^^^^^^^^\n" + |
1989 |
"Resource leak: \'doubleWrap\' is never closed\n" + |
1990 |
"----------\n" + |
1991 |
"2. ERROR in X.java (at line 15)\n" + |
1992 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
1993 |
" ^^^^\n" + |
1994 |
"Resource leak: \'bis2\' is never closed\n" + |
1995 |
"----------\n", |
1996 |
null, |
1997 |
true, |
1998 |
options); |
1999 |
} |
2000 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2001 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
2002 |
// a resource wrapper is closed closing also the underlying resource |
2003 |
public void test061e() { |
2004 |
Map options = getCompilerOptions(); |
2005 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2006 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2007 |
this.runConformTest( |
2008 |
new String[] { |
2009 |
"X.java", |
2010 |
"import java.io.File;\n" + |
2011 |
"import java.io.BufferedInputStream;\n" + |
2012 |
"import java.io.FileInputStream;\n" + |
2013 |
"import java.io.IOException;\n" + |
2014 |
"public class X {\n" + |
2015 |
" FileInputStream fis;" + |
2016 |
" void foo() throws IOException {\n" + |
2017 |
" File file = new File(\"somefile\");\n" + |
2018 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2019 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
2020 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
2021 |
" System.out.println(bis.available());\n" + |
2022 |
" bis.close();\n" + |
2023 |
" }\n" + |
2024 |
" void inline() throws IOException {\n" + |
2025 |
" File file = new File(\"somefile\");\n" + |
2026 |
" BufferedInputStream bis2 = new BufferedInputStream(fis = new FileInputStream(file));\n" + // field assignment |
2027 |
" System.out.println(bis2.available());\n" + |
2028 |
" bis2.close();\n" + |
2029 |
" FileInputStream fileStream = null;\n" + |
2030 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
2031 |
" System.out.println(bis3.available());\n" + |
2032 |
" bis3.close();\n" + |
2033 |
" }\n" + |
2034 |
" public static void main(String[] args) throws IOException {\n" + |
2035 |
" try {\n" + |
2036 |
" new X().foo();\n" + |
2037 |
" } catch (IOException ex) {" + |
2038 |
" System.out.println(\"Got IO Exception\");\n" + |
2039 |
" }\n" + |
2040 |
" }\n" + |
2041 |
"}\n" |
2042 |
}, |
2043 |
"Got IO Exception", |
2044 |
null, |
2045 |
true, |
2046 |
null, |
2047 |
options, |
2048 |
null); |
2049 |
} |
2050 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2051 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
2052 |
// a resource wrapper is closed closing also the underlying resource - original test case |
2053 |
public void test061f() throws IOException { |
2054 |
Map options = getCompilerOptions(); |
2055 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2056 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2057 |
URL url = FileLocator.toFileURL(FileLocator.find(Platform.getBundle("org.eclipse.jdt.core.tests.compiler"), new Path("META-INF/MANIFEST.MF"), null)); |
2058 |
this.runConformTest( |
2059 |
new String[] { |
2060 |
"X.java", |
2061 |
"import java.io.InputStream;\n" + |
2062 |
"import java.io.InputStreamReader;\n" + |
2063 |
"import java.io.BufferedReader;\n" + |
2064 |
"import java.io.IOException;\n" + |
2065 |
"import java.net.URL;\n" + |
2066 |
"public class X {\n" + |
2067 |
" boolean loadURL(final URL url) throws IOException {\n" + |
2068 |
" InputStream stream = null;\n" + |
2069 |
" BufferedReader reader = null;\n" + |
2070 |
" try {\n" + |
2071 |
" stream = url.openStream();\n" + |
2072 |
" reader = new BufferedReader(new InputStreamReader(stream));\n" + |
2073 |
" System.out.println(reader.readLine());\n" + |
2074 |
" } finally {\n" + |
2075 |
" try {\n" + |
2076 |
" if (reader != null)\n" + |
2077 |
" reader.close();\n" + |
2078 |
" } catch (IOException x) {\n" + |
2079 |
" }\n" + |
2080 |
" }\n" + |
2081 |
" return false; // 'stream' may not be closed at this location\n" + |
2082 |
" }\n" + |
2083 |
" public static void main(String[] args) throws IOException {\n" + |
2084 |
" try {\n" + |
2085 |
" new X().loadURL(new URL(\""+url.toString()+"\"));\n" + |
2086 |
" } catch (IOException ex) {\n" + |
2087 |
" System.out.println(\"Got IO Exception\"+ex);\n" + |
2088 |
" }\n" + |
2089 |
" }\n" + |
2090 |
"}\n" |
2091 |
}, |
2092 |
"Manifest-Version: 1.0", |
2093 |
null, |
2094 |
true, |
2095 |
null, |
2096 |
options, |
2097 |
null); |
2098 |
} |
2099 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2100 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
2101 |
// a resource wrapper is closed closing also the underlying resource - from a real-world example |
2102 |
public void test061f2() throws IOException { |
2103 |
Map options = getCompilerOptions(); |
2104 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2105 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2106 |
this.runConformTest( |
2107 |
new String[] { |
2108 |
"X.java", |
2109 |
"import java.io.OutputStream;\n" + |
2110 |
"import java.io.FileOutputStream;\n" + |
2111 |
"import java.io.BufferedOutputStream;\n" + |
2112 |
"import java.io.IOException;\n" + |
2113 |
"public class X {\n" + |
2114 |
" void zork() throws IOException {\n" + |
2115 |
" try {\n" + |
2116 |
" OutputStream os = null;\n" + |
2117 |
" try {\n" + |
2118 |
" os = new BufferedOutputStream(new FileOutputStream(\"somefile\"));\n" + |
2119 |
" String externalForm = \"externalPath\";\n" + |
2120 |
" } finally {\n" + |
2121 |
" if (os != null)\n" + |
2122 |
" os.close();\n" + |
2123 |
" }\n" + |
2124 |
" } catch (IOException e) {\n" + |
2125 |
" e.printStackTrace();\n" + |
2126 |
" }\n" + |
2127 |
" }\n" + |
2128 |
"}\n" |
2129 |
}, |
2130 |
"", |
2131 |
null, |
2132 |
true, |
2133 |
null, |
2134 |
options, |
2135 |
null); |
2136 |
} |
2137 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2138 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
2139 |
// a resource wrapper is sent to another method affecting also the underlying resource - from a real-world example |
2140 |
public void test061f3() throws IOException { |
2141 |
Map options = getCompilerOptions(); |
2142 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2143 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2144 |
this.runNegativeTest( |
2145 |
new String[] { |
2146 |
"X.java", |
2147 |
"import java.io.File;\n" + |
2148 |
"import java.io.FileInputStream;\n" + |
2149 |
"import java.io.FileNotFoundException;\n" + |
2150 |
"import java.io.InputStream;\n" + |
2151 |
"import java.io.BufferedInputStream;\n" + |
2152 |
"public class X {\n" + |
2153 |
" String loadProfile(File profileFile) {\n" + |
2154 |
" try {\n" + |
2155 |
" InputStream stream = new BufferedInputStream(new FileInputStream(profileFile));\n" + |
2156 |
" return loadProfile(stream);\n" + |
2157 |
" } catch (FileNotFoundException e) {\n" + |
2158 |
" //null\n" + |
2159 |
" }\n" + |
2160 |
" return null;\n" + |
2161 |
" }\n" + |
2162 |
" private String loadProfile(InputStream stream) {\n" + |
2163 |
" return null;\n" + |
2164 |
" }\n" + |
2165 |
"}\n" |
2166 |
}, |
2167 |
"----------\n" + |
2168 |
"1. ERROR in X.java (at line 10)\n" + |
2169 |
" return loadProfile(stream);\n" + |
2170 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2171 |
"Potential resource leak: \'stream\' may not be closed at this location\n" + |
2172 |
"----------\n", |
2173 |
null, |
2174 |
true, |
2175 |
options); |
2176 |
} |
2177 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2178 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
2179 |
// Different points in a resource chain are closed |
2180 |
public void test061g() { |
2181 |
Map options = getCompilerOptions(); |
2182 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2183 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2184 |
this.runNegativeTest( |
2185 |
new String[] { |
2186 |
"X.java", |
2187 |
"import java.io.File;\n" + |
2188 |
"import java.io.BufferedInputStream;\n" + |
2189 |
"import java.io.FileInputStream;\n" + |
2190 |
"import java.io.IOException;\n" + |
2191 |
"public class X {\n" + |
2192 |
" void closeMiddle() throws IOException {\n" + |
2193 |
" File file = new File(\"somefile\");\n" + |
2194 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2195 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
2196 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
2197 |
" System.out.println(bis.available());\n" + |
2198 |
" bis.close();\n" + |
2199 |
" }\n" + |
2200 |
" void closeOuter() throws IOException {\n" + |
2201 |
" File file2 = new File(\"somefile\");\n" + |
2202 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
2203 |
" BufferedInputStream bis2 = new BufferedInputStream(fileStream2);\n" + |
2204 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
2205 |
" System.out.println(bis2.available());\n" + |
2206 |
" doubleWrap2.close();\n" + |
2207 |
" }\n" + |
2208 |
" void neverClosed() throws IOException {\n" + |
2209 |
" File file3 = new File(\"somefile\");\n" + |
2210 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
2211 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
2212 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
2213 |
" System.out.println(doubleWrap3.available());\n" + |
2214 |
" }\n" + |
2215 |
"}\n" |
2216 |
}, |
2217 |
"----------\n" + |
2218 |
"1. ERROR in X.java (at line 26)\n" + |
2219 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
2220 |
" ^^^^^^^^^^^\n" + |
2221 |
"Resource leak: \'doubleWrap3\' is never closed\n" + |
2222 |
"----------\n", |
2223 |
null, |
2224 |
true, |
2225 |
options); |
2226 |
} |
2227 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2228 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
2229 |
// Different points in a resource chain are potentially closed |
2230 |
public void test061h() { |
2231 |
Map options = getCompilerOptions(); |
2232 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2233 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2234 |
this.runNegativeTest( |
2235 |
new String[] { |
2236 |
"X.java", |
2237 |
"import java.io.File;\n" + |
2238 |
"import java.io.BufferedInputStream;\n" + |
2239 |
"import java.io.FileInputStream;\n" + |
2240 |
"import java.io.IOException;\n" + |
2241 |
"public class X {\n" + |
2242 |
" void closeMiddle(boolean b) throws IOException {\n" + |
2243 |
" File file = new File(\"somefile\");\n" + |
2244 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2245 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
2246 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
2247 |
" System.out.println(bis.available());\n" + |
2248 |
" if (b)\n" + |
2249 |
" bis.close();\n" + |
2250 |
" }\n" + |
2251 |
" void closeOuter(boolean b) throws IOException {\n" + |
2252 |
" File file2 = new File(\"somefile\");\n" + |
2253 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
2254 |
" BufferedInputStream dummy;\n" + |
2255 |
" BufferedInputStream bis2 = (dummy = new BufferedInputStream(fileStream2));\n" + |
2256 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
2257 |
" System.out.println(bis2.available());\n" + |
2258 |
" if (b)\n" + |
2259 |
" doubleWrap2.close();\n" + |
2260 |
" }\n" + |
2261 |
" void potAndDef(boolean b) throws IOException {\n" + |
2262 |
" File file3 = new File(\"somefile\");\n" + |
2263 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
2264 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
2265 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
2266 |
" System.out.println(doubleWrap3.available());\n" + |
2267 |
" if (b) bis3.close();\n" + |
2268 |
" fileStream3.close();\n" + |
2269 |
" }\n" + |
2270 |
"}\n" |
2271 |
}, |
2272 |
"----------\n" + |
2273 |
"1. ERROR in X.java (at line 10)\n" + |
2274 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
2275 |
" ^^^^^^^^^^\n" + |
2276 |
"Potential resource leak: \'doubleWrap\' may not be closed\n" + |
2277 |
"----------\n" + |
2278 |
"2. ERROR in X.java (at line 20)\n" + |
2279 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
2280 |
" ^^^^^^^^^^^\n" + |
2281 |
"Potential resource leak: \'doubleWrap2\' may not be closed\n" + |
2282 |
"----------\n", |
2283 |
null, |
2284 |
true, |
2285 |
options); |
2286 |
} |
2287 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2288 |
// local var is re-used for two levels of wrappers |
2289 |
public void test061i() { |
2290 |
Map options = getCompilerOptions(); |
2291 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2292 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2293 |
this.runNegativeTest( |
2294 |
new String[] { |
2295 |
"X.java", |
2296 |
"import java.io.File;\n" + |
2297 |
"import java.io.InputStream;\n" + |
2298 |
"import java.io.BufferedInputStream;\n" + |
2299 |
"import java.io.FileInputStream;\n" + |
2300 |
"import java.io.IOException;\n" + |
2301 |
"public class X {\n" + |
2302 |
" void closeMiddle() throws IOException {\n" + |
2303 |
" File file = new File(\"somefile\");\n" + |
2304 |
" InputStream stream = new FileInputStream(file);\n" + |
2305 |
" stream = new BufferedInputStream(stream);\n" + |
2306 |
" InputStream middle;\n" + |
2307 |
" stream = new BufferedInputStream(middle = stream);\n" + |
2308 |
" System.out.println(stream.available());\n" + |
2309 |
" middle.close();\n" + |
2310 |
" }\n" + |
2311 |
" void closeOuter() throws IOException {\n" + |
2312 |
" File file = new File(\"somefile\");\n" + |
2313 |
" InputStream stream2 = new FileInputStream(file);\n" + |
2314 |
" stream2 = new BufferedInputStream(stream2);\n" + |
2315 |
" stream2 = new BufferedInputStream(stream2);\n" + |
2316 |
" System.out.println(stream2.available());\n" + |
2317 |
" stream2.close();\n" + |
2318 |
" }\n" + |
2319 |
" void neverClosed() throws IOException {\n" + |
2320 |
" File file = new File(\"somefile\");\n" + |
2321 |
" InputStream stream3 = new FileInputStream(file);\n" + |
2322 |
" stream3 = new BufferedInputStream(stream3);\n" + |
2323 |
" stream3 = new BufferedInputStream(stream3);\n" + |
2324 |
" System.out.println(stream3.available());\n" + |
2325 |
" }\n" + |
2326 |
"}\n" |
2327 |
}, |
2328 |
"----------\n" + |
2329 |
"1. ERROR in X.java (at line 26)\n" + |
2330 |
" InputStream stream3 = new FileInputStream(file);\n" + |
2331 |
" ^^^^^^^\n" + |
2332 |
"Resource leak: \'stream3\' is never closed\n" + |
2333 |
"----------\n", |
2334 |
null, |
2335 |
true, |
2336 |
options); |
2337 |
} |
2338 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2339 |
// self-wrapping a method argument (caused NPE UnconditionalFlowInfo.markAsDefinitelyNull(..)). |
2340 |
public void test061j() { |
2341 |
Map options = getCompilerOptions(); |
2342 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2343 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2344 |
this.runConformTest( |
2345 |
new String[] { |
2346 |
"X.java", |
2347 |
"import java.io.InputStream;\n" + |
2348 |
"import java.io.BufferedInputStream;\n" + |
2349 |
"import java.io.IOException;\n" + |
2350 |
"public class X {\n" + |
2351 |
" void foo(InputStream stream) throws IOException {\n" + |
2352 |
" stream = new BufferedInputStream(stream);\n" + |
2353 |
" System.out.println(stream.available());\n" + |
2354 |
" stream.close();\n" + |
2355 |
" }\n" + |
2356 |
" void boo(InputStream stream2) throws IOException {\n" + |
2357 |
" stream2 = new BufferedInputStream(stream2);\n" + |
2358 |
" System.out.println(stream2.available());\n" + |
2359 |
" }\n" + |
2360 |
"}\n" |
2361 |
}, |
2362 |
"", |
2363 |
null, |
2364 |
true, |
2365 |
null, |
2366 |
options, |
2367 |
null); |
2368 |
} |
2369 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2370 |
// a wrapper is created in a return statement |
2371 |
public void test061k() throws IOException { |
2372 |
Map options = getCompilerOptions(); |
2373 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2374 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2375 |
this.runConformTest( |
2376 |
new String[] { |
2377 |
"X.java", |
2378 |
"import java.io.File;\n" + |
2379 |
"import java.io.FileInputStream;\n" + |
2380 |
"import java.io.BufferedInputStream;\n" + |
2381 |
"import java.io.IOException;\n" + |
2382 |
"public class X {\n" + |
2383 |
" BufferedInputStream getReader(File file) throws IOException {\n" + |
2384 |
" FileInputStream stream = new FileInputStream(file);\n" + |
2385 |
" return new BufferedInputStream(stream);\n" + |
2386 |
" }\n" + |
2387 |
"}\n" |
2388 |
}, |
2389 |
"", |
2390 |
null, |
2391 |
true, |
2392 |
null, |
2393 |
options, |
2394 |
null); |
2395 |
} |
2396 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2397 |
// a closeable is assigned to a field |
2398 |
public void test061l() throws IOException { |
2399 |
Map options = getCompilerOptions(); |
2400 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2401 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2402 |
this.runConformTest( |
2403 |
new String[] { |
2404 |
"X.java", |
2405 |
"import java.io.File;\n" + |
2406 |
"import java.io.FileInputStream;\n" + |
2407 |
"import java.io.BufferedInputStream;\n" + |
2408 |
"import java.io.IOException;\n" + |
2409 |
"public class X {\n" + |
2410 |
" BufferedInputStream stream;\n" + |
2411 |
" void foo(File file) throws IOException {\n" + |
2412 |
" FileInputStream s = new FileInputStream(file);\n" + |
2413 |
" stream = new BufferedInputStream(s);\n" + |
2414 |
" }\n" + |
2415 |
"}\n" |
2416 |
}, |
2417 |
"", |
2418 |
null, |
2419 |
true, |
2420 |
null, |
2421 |
options, |
2422 |
null); |
2423 |
} |
2424 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2425 |
// a closeable is passed to another method in a return statement |
2426 |
// example constructed after org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository#getArtifact(..) |
2427 |
public void test061m() throws IOException { |
2428 |
Map options = getCompilerOptions(); |
2429 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2430 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2431 |
this.runNegativeTest( |
2432 |
new String[] { |
2433 |
"X.java", |
2434 |
"import java.io.File;\n" + |
2435 |
"import java.io.FileInputStream;\n" + |
2436 |
"import java.io.BufferedInputStream;\n" + |
2437 |
"import java.io.InputStream;\n" + |
2438 |
"import java.io.IOException;\n" + |
2439 |
"public class X {\n" + |
2440 |
" BufferedInputStream stream;\n" + |
2441 |
" BufferedInputStream foo(File file) throws IOException {\n" + |
2442 |
" FileInputStream s = new FileInputStream(file);\n" + |
2443 |
" return check(new BufferedInputStream(s));\n" + |
2444 |
" }\n" + |
2445 |
" BufferedInputStream foo2(FileInputStream s, File file) throws IOException {\n" + |
2446 |
" s = new FileInputStream(file);\n" + |
2447 |
" return check(s);\n" + |
2448 |
" }\n" + |
2449 |
" BufferedInputStream foo3(InputStream s) throws IOException {\n" + |
2450 |
" s = check(s);\n" + |
2451 |
" return check(s);\n" + |
2452 |
" }\n" + |
2453 |
" BufferedInputStream check(InputStream s) { return null; }\n" + |
2454 |
"}\n" |
2455 |
}, |
2456 |
// TODO: also these warnings *might* be avoidable by detecting check(s) as a wrapper creation?? |
2457 |
"----------\n" + |
2458 |
"1. ERROR in X.java (at line 14)\n" + |
2459 |
" return check(s);\n" + |
2460 |
" ^^^^^^^^^^^^^^^^\n" + |
2461 |
"Potential resource leak: \'s\' may not be closed at this location\n" + |
2462 |
"----------\n" + |
2463 |
"2. ERROR in X.java (at line 18)\n" + |
2464 |
" return check(s);\n" + |
2465 |
" ^^^^^^^^^^^^^^^^\n" + |
2466 |
"Potential resource leak: \'s\' may not be closed at this location\n" + |
2467 |
"----------\n", |
2468 |
null, |
2469 |
true, |
2470 |
options); |
2471 |
} |
2472 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2473 |
// a resource wrapper does not wrap any provided resource |
2474 |
public void test061n() { |
2475 |
Map options = getCompilerOptions(); |
2476 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2477 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2478 |
this.runNegativeTest( |
2479 |
new String[] { |
2480 |
"X.java", |
2481 |
"import java.io.PrintWriter;\n" + |
2482 |
"import java.io.IOException;\n" + |
2483 |
"public class X {\n" + |
2484 |
" void foo() throws IOException {\n" + |
2485 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2486 |
" writer.write(1);\n" + |
2487 |
" }\n" + |
2488 |
"}\n" |
2489 |
}, |
2490 |
"----------\n" + |
2491 |
"1. ERROR in X.java (at line 5)\n" + |
2492 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2493 |
" ^^^^^^\n" + |
2494 |
"Resource leak: \'writer\' is never closed\n" + |
2495 |
"----------\n", |
2496 |
null, |
2497 |
true, |
2498 |
options); |
2499 |
} |
2500 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2501 |
// a resource wrapper is closed only in its local block, underlying resource may leak |
2502 |
public void test061o() { |
2503 |
Map options = getCompilerOptions(); |
2504 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2505 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2506 |
this.runNegativeTest( |
2507 |
new String[] { |
2508 |
"X.java", |
2509 |
"import java.io.File;\n" + |
2510 |
"import java.io.FileInputStream;\n" + |
2511 |
"import java.io.BufferedInputStream;\n" + |
2512 |
"import java.io.IOException;\n" + |
2513 |
"public class X {\n" + |
2514 |
" void foo(boolean bar) throws IOException {\n" + |
2515 |
" File file = new File(\"somefil\");\n" + |
2516 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2517 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
2518 |
" if (bar) {\n" + |
2519 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
2520 |
" doubleWrap.close();\n" + |
2521 |
" }\n" + |
2522 |
" }\n" + |
2523 |
"}\n" |
2524 |
}, |
2525 |
"----------\n" + |
2526 |
"1. ERROR in X.java (at line 9)\n" + |
2527 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
2528 |
" ^^^\n" + |
2529 |
"Potential resource leak: \'bis\' may not be closed\n" + |
2530 |
"----------\n", |
2531 |
null, |
2532 |
true, |
2533 |
options); |
2534 |
} |
2535 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2536 |
// a resource wrapper is conditionally allocated but not closed - from a real-world example |
2537 |
public void test061f4() throws IOException { |
2538 |
Map options = getCompilerOptions(); |
2539 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2540 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2541 |
this.runNegativeTest( |
2542 |
new String[] { |
2543 |
"X.java", |
2544 |
"import java.io.File;\n" + |
2545 |
"import java.io.FileInputStream;\n" + |
2546 |
"import java.io.FileNotFoundException;\n" + |
2547 |
"import java.io.InputStream;\n" + |
2548 |
"import java.io.BufferedInputStream;\n" + |
2549 |
"public class X {\n" + |
2550 |
" void foo(File location, String adviceFilePath) throws FileNotFoundException {\n" + |
2551 |
" InputStream stream = null;\n" + |
2552 |
" if (location.isDirectory()) {\n" + |
2553 |
" File adviceFile = new File(location, adviceFilePath);\n" + |
2554 |
" stream = new BufferedInputStream(new FileInputStream(adviceFile));\n" + |
2555 |
" }\n" + |
2556 |
" }\n" + |
2557 |
"}\n" |
2558 |
}, |
2559 |
"----------\n" + |
2560 |
"1. ERROR in X.java (at line 11)\n" + |
2561 |
" stream = new BufferedInputStream(new FileInputStream(adviceFile));\n" + |
2562 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2563 |
"Potential resource leak: \'stream\' may not be closed\n" + // message could be stronger, but the enclosing if blurs the picture |
2564 |
"----------\n", |
2565 |
null, |
2566 |
true, |
2567 |
options); |
2568 |
} |
2569 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2570 |
// a t-w-r wraps an existing resource |
2571 |
public void test061p() { |
2572 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; |
2573 |
Map options = getCompilerOptions(); |
2574 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2575 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2576 |
this.runConformTest( |
2577 |
new String[] { |
2578 |
"X.java", |
2579 |
"import java.io.PrintWriter;\n" + |
2580 |
"import java.io.BufferedWriter;\n" + |
2581 |
"import java.io.IOException;\n" + |
2582 |
"public class X {\n" + |
2583 |
" void foo() throws IOException {\n" + |
2584 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2585 |
" try (BufferedWriter bw = new BufferedWriter(writer)) {\n" + |
2586 |
" bw.write(1);\n" + |
2587 |
" }\n" + |
2588 |
" }\n" + |
2589 |
"}\n" |
2590 |
}, |
2591 |
"", |
2592 |
null, |
2593 |
true, |
2594 |
null, |
2595 |
options, |
2596 |
null); |
2597 |
} |
2598 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2599 |
// a t-w-r potentially wraps an existing resource |
2600 |
// DISABLED, fails because we currently don't include t-w-r managed resources in the analysis |
2601 |
public void _test061q() { |
2602 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; |
2603 |
Map options = getCompilerOptions(); |
2604 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2605 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2606 |
this.runNegativeTest( |
2607 |
new String[] { |
2608 |
"X.java", |
2609 |
"import java.io.PrintWriter;\n" + |
2610 |
"import java.io.BufferedWriter;\n" + |
2611 |
"import java.io.IOException;\n" + |
2612 |
"public class X {\n" + |
2613 |
" void foo(boolean b) throws IOException {\n" + |
2614 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2615 |
" if (b)\n" + |
2616 |
" try (BufferedWriter bw = new BufferedWriter(writer)) {\n" + |
2617 |
" bw.write(1);\n" + |
2618 |
" }\n" + |
2619 |
" }\n" + |
2620 |
"}\n" |
2621 |
}, |
2622 |
"----------\n" + |
2623 |
"1. ERROR in X.java (at line 6)\n" + |
2624 |
" PrintWriter writer = new PrintWriter(\\\"filename\\\");\n" + |
2625 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2626 |
"Potential resource leak: \'writer\' may not be closed\n" + |
2627 |
"----------\n", |
2628 |
null, |
2629 |
true, |
2630 |
options); |
2631 |
} |
2632 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2633 |
// the inner from a wrapper is returned |
2634 |
public void test061r() { |
2635 |
Map options = getCompilerOptions(); |
2636 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2637 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2638 |
this.runConformTest( |
2639 |
new String[] { |
2640 |
"X.java", |
2641 |
"import java.io.FileInputStream;\n" + |
2642 |
"import java.io.File;\n" + |
2643 |
"import java.io.BufferedInputStream;\n" + |
2644 |
"import java.io.IOException;\n" + |
2645 |
"public class X {\n" + |
2646 |
" FileInputStream foo() throws IOException {\n" + |
2647 |
" File file = new File(\"somefil\");\n" + |
2648 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2649 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
2650 |
" return fileStream;\n" + |
2651 |
" }\n" + |
2652 |
"}\n" |
2653 |
}, |
2654 |
"", |
2655 |
null, |
2656 |
true, |
2657 |
null, |
2658 |
options, |
2659 |
null); |
2660 |
} |
2661 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2662 |
// a wrapper is forgotten, the inner is closed afterwards |
2663 |
public void test061s() { |
2664 |
Map options = getCompilerOptions(); |
2665 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2666 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2667 |
this.runConformTest( |
2668 |
new String[] { |
2669 |
"X.java", |
2670 |
"import java.io.FileInputStream;\n" + |
2671 |
"import java.io.File;\n" + |
2672 |
"import java.io.BufferedInputStream;\n" + |
2673 |
"import java.io.IOException;\n" + |
2674 |
"public class X {\n" + |
2675 |
" void foo() throws IOException {\n" + |
2676 |
" File file = new File(\"somefil\");\n" + |
2677 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2678 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
2679 |
" bis = null;\n" + |
2680 |
" fileStream.close();\n" + |
2681 |
" }\n" + |
2682 |
"}\n" |
2683 |
}, |
2684 |
"", |
2685 |
null, |
2686 |
true, |
2687 |
null, |
2688 |
options, |
2689 |
null); |
2690 |
} |
2691 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
2692 |
// a resource is never assigned |
2693 |
public void test062a() throws IOException { |
2694 |
Map options = getCompilerOptions(); |
2695 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2696 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2697 |
this.runNegativeTest( |
2698 |
new String[] { |
2699 |
"X.java", |
2700 |
"import java.io.File;\n" + |
2701 |
"import java.io.FileOutputStream;\n" + |
2702 |
"import java.io.IOException;\n" + |
2703 |
"public class X {\n" + |
2704 |
" void foo() throws IOException {\n" + |
2705 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).write(1);\n" + |
2706 |
" }\n" + |
2707 |
"}\n" |
2708 |
}, |
2709 |
"----------\n" + |
2710 |
"1. ERROR in X.java (at line 6)\n" + |
2711 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).write(1);\n" + |
2712 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2713 |
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" + |
2714 |
"----------\n", |
2715 |
null, |
2716 |
true, |
2717 |
options); |
2718 |
} |
2719 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
2720 |
// a freshly allocated resource is immediately closed |
2721 |
public void test062b() throws IOException { |
2722 |
Map options = getCompilerOptions(); |
2723 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2724 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2725 |
this.runConformTest( |
2726 |
new String[] { |
2727 |
"X.java", |
2728 |
"import java.io.File;\n" + |
2729 |
"import java.io.FileOutputStream;\n" + |
2730 |
"import java.io.IOException;\n" + |
2731 |
"public class X {\n" + |
2732 |
" void foo() throws IOException {\n" + |
2733 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).close();\n" + |
2734 |
" }\n" + |
2735 |
"}\n" |
2736 |
}, |
2737 |
"", |
2738 |
null, |
2739 |
true, |
2740 |
null, |
2741 |
options, |
2742 |
null); |
2743 |
} |
2744 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
2745 |
// a resource is directly passed to another method |
2746 |
public void test062c() throws IOException { |
2747 |
Map options = getCompilerOptions(); |
2748 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2749 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2750 |
this.runConformTest( |
2751 |
new String[] { |
2752 |
"X.java", |
2753 |
"import java.io.File;\n" + |
2754 |
"import java.io.FileOutputStream;\n" + |
2755 |
"import java.io.IOException;\n" + |
2756 |
"public class X {\n" + |
2757 |
" void foo() throws IOException {\n" + |
2758 |
" writeIt(new FileOutputStream(new File(\"C:\\temp\\foo.txt\")));\n" + |
2759 |
" }\n" + |
2760 |
" void writeIt(FileOutputStream fos) throws IOException {\n" + |
2761 |
" fos.write(1);\n" + |
2762 |
" fos.close();\n" + |
2763 |
" }\n" + |
2764 |
"}\n" |
2765 |
}, |
2766 |
"", |
2767 |
null, |
2768 |
true, |
2769 |
null, |
2770 |
options, |
2771 |
null); |
2772 |
} |
2773 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
2774 |
// a resource is not used |
2775 |
public void test062d() throws IOException { |
2776 |
Map options = getCompilerOptions(); |
2777 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2778 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2779 |
this.runNegativeTest( |
2780 |
new String[] { |
2781 |
"X.java", |
2782 |
"import java.io.File;\n" + |
2783 |
"import java.io.FileOutputStream;\n" + |
2784 |
"import java.io.IOException;\n" + |
2785 |
"public class X {\n" + |
2786 |
" void foo() throws IOException {\n" + |
2787 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\"));\n" + |
2788 |
" }\n" + |
2789 |
"}\n" |
2790 |
}, |
2791 |
"----------\n" + |
2792 |
"1. ERROR in X.java (at line 6)\n" + |
2793 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\"));\n" + |
2794 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2795 |
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" + |
2796 |
"----------\n", |
2797 |
null, |
2798 |
true, |
2799 |
options); |
2800 |
} |
2801 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
2802 |
// a wrapper is obtained from another method |
2803 |
public void test063a() throws IOException { |
2804 |
Map options = getCompilerOptions(); |
2805 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2806 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2807 |
this.runNegativeTest( |
2808 |
new String[] { |
2809 |
"X.java", |
2810 |
"import java.io.File;\n" + |
2811 |
"import java.io.FileInputStream;\n" + |
2812 |
"import java.io.BufferedInputStream;\n" + |
2813 |
"import java.io.IOException;\n" + |
2814 |
"public class X {\n" + |
2815 |
" void read(File file) throws IOException {\n" + |
2816 |
" FileInputStream stream = new FileInputStream(file);\n" + |
2817 |
" BufferedInputStream bis = new BufferedInputStream(stream); // never since reassigned\n" + |
2818 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
2819 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
2820 |
" bis.available();\n" + |
2821 |
" }\n" + |
2822 |
" BufferedInputStream getReader(FileInputStream stream) throws IOException {\n" + |
2823 |
" return new BufferedInputStream(stream);\n" + |
2824 |
" }\n" + |
2825 |
"}\n" |
2826 |
}, |
2827 |
"----------\n" + |
2828 |
"1. ERROR in X.java (at line 7)\n" + |
2829 |
" FileInputStream stream = new FileInputStream(file);\n" + |
2830 |
" ^^^^^^\n" + |
2831 |
"Resource leak: \'stream\' is never closed\n" + |
2832 |
"----------\n" + |
2833 |
"2. ERROR in X.java (at line 9)\n" + |
2834 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
2835 |
" ^^^^^^^\n" + |
2836 |
"Potential resource leak: \'stream2\' may not be closed\n" + |
2837 |
"----------\n" + |
2838 |
"3. ERROR in X.java (at line 10)\n" + |
2839 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
2840 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2841 |
"Potential resource leak: \'bis\' may not be closed\n" + |
2842 |
"----------\n", |
2843 |
null, |
2844 |
true, |
2845 |
options); |
2846 |
} |
2847 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
2848 |
// a wrapper is obtained from a field read |
2849 |
public void test063b() throws IOException { |
2850 |
Map options = getCompilerOptions(); |
2851 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2852 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2853 |
this.runConformTest( |
2854 |
new String[] { |
2855 |
"X.java", |
2856 |
"import java.io.FileInputStream;\n" + |
2857 |
"import java.io.BufferedInputStream;\n" + |
2858 |
"import java.io.IOException;\n" + |
2859 |
"public class X {\n" + |
2860 |
" FileInputStream stream;\n" + |
2861 |
" void read() throws IOException {\n" + |
2862 |
" FileInputStream s = this.stream;\n" + |
2863 |
" BufferedInputStream bis = new BufferedInputStream(s); // don't complain since s is obtained from a field\n" + |
2864 |
" bis.available();\n" + |
2865 |
" }\n" + |
2866 |
"}\n" |
2867 |
}, |
2868 |
"", |
2869 |
null, |
2870 |
true, |
2871 |
null, |
2872 |
options, |
2873 |
null); |
2874 |
} |
2875 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
2876 |
// a wrapper is assigned to a field |
2877 |
public void test063c() throws IOException { |
2878 |
Map options = getCompilerOptions(); |
2879 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2880 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2881 |
this.runConformTest( |
2882 |
new String[] { |
2883 |
"X.java", |
2884 |
"import java.io.FileInputStream;\n" + |
2885 |
"import java.io.BufferedInputStream;\n" + |
2886 |
"import java.io.IOException;\n" + |
2887 |
"public class X {\n" + |
2888 |
" BufferedInputStream stream;\n" + |
2889 |
" void read() throws IOException {\n" + |
2890 |
" FileInputStream s = new FileInputStream(\"somefile\");\n" + |
2891 |
" BufferedInputStream bis = new BufferedInputStream(s);\n" + |
2892 |
" this.stream = bis;\n" + |
2893 |
" }\n" + |
2894 |
"}\n" |
2895 |
}, |
2896 |
"", |
2897 |
null, |
2898 |
true, |
2899 |
null, |
2900 |
options, |
2901 |
null); |
2902 |
} |
2903 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
2904 |
// a resource is obtained as a method argument and/or assigned with a cast |
2905 |
public void test063d() throws IOException { |
2906 |
Map options = getCompilerOptions(); |
2907 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2908 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2909 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
2910 |
runTestsExpectingErrorsOnlyIn17( |
2911 |
new String[] { |
2912 |
"X.java", |
2913 |
"import java.io.FileInputStream;\n" + |
2914 |
"import java.io.BufferedInputStream;\n" + |
2915 |
"import java.io.InputStream;\n" + |
2916 |
"import java.io.IOException;\n" + |
2917 |
"public class X {\n" + |
2918 |
" void foo( InputStream input) throws IOException {\n" + |
2919 |
" FileInputStream input1 = (FileInputStream)input;\n" + |
2920 |
" System.out.println(input1.read());\n" + |
2921 |
" input.close();\n" + // don't propose t-w-r for argument |
2922 |
" }\n" + |
2923 |
" void foo() throws IOException {\n" + |
2924 |
" InputStream input = new FileInputStream(\"somefile\");\n" + |
2925 |
" FileInputStream input1 = (FileInputStream)input;\n" + |
2926 |
" System.out.println(input1.read());\n" + |
2927 |
" input.close();\n" + // do propose t-w-r, not from a method argument |
2928 |
" }\n" + |
2929 |
" void foo3( InputStream input, InputStream input2) throws IOException {\n" + |
2930 |
" FileInputStream input1 = (FileInputStream)input;\n" + // still don't claim because obtained from outside |
2931 |
" System.out.println(input1.read());\n" + |
2932 |
" BufferedInputStream bis = new BufferedInputStream(input2);\n" + |
2933 |
" System.out.println(bis.read());\n" + |
2934 |
" }\n" + |
2935 |
"}\n" |
2936 |
}, |
2937 |
"----------\n" + |
2938 |
"1. ERROR in X.java (at line 12)\n" + |
2939 |
" InputStream input = new FileInputStream(\"somefile\");\n" + |
2940 |
" ^^^^^\n" + |
2941 |
"Resource \'input\' should be managed by try-with-resource\n" + |
2942 |
"----------\n", |
2943 |
options); |
2944 |
} |
2945 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
2946 |
// a resource is obtained from a field read, then re-assigned |
2947 |
public void test063e() { |
2948 |
Map options = getCompilerOptions(); |
2949 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2950 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2951 |
this.runConformTest( |
2952 |
new String[] { |
2953 |
"X.java", |
2954 |
"import java.io.FileInputStream;\n" + |
2955 |
"import java.io.IOException;\n" + |
2956 |
"public class X {\n" + |
2957 |
" FileInputStream input1;\n" + |
2958 |
" public void foo() throws IOException {\n" + |
2959 |
" FileInputStream input = input1;\n" + |
2960 |
" input = new FileInputStream(\"adfafd\");\n" + |
2961 |
" input.close();\n" + |
2962 |
" }\n" + |
2963 |
"}\n" |
2964 |
}, |
2965 |
"", |
2966 |
null, |
2967 |
true, |
2968 |
null, |
2969 |
options, |
2970 |
null); |
2971 |
} |
2972 |
|
2973 |
} |