Lines 14-21
Link Here
|
14 |
*******************************************************************************/ |
14 |
*******************************************************************************/ |
15 |
package org.eclipse.jdt.core.tests.compiler.regression; |
15 |
package org.eclipse.jdt.core.tests.compiler.regression; |
16 |
|
16 |
|
|
|
17 |
import java.io.IOException; |
18 |
import java.net.URL; |
17 |
import java.util.Map; |
19 |
import java.util.Map; |
18 |
|
20 |
|
|
|
21 |
import org.eclipse.core.runtime.FileLocator; |
22 |
import org.eclipse.core.runtime.Path; |
23 |
import org.eclipse.core.runtime.Platform; |
19 |
import org.eclipse.jdt.core.JavaCore; |
24 |
import org.eclipse.jdt.core.JavaCore; |
20 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
25 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
21 |
|
26 |
|
Lines 23-29
Link Here
|
23 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
28 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
24 |
|
29 |
|
25 |
static { |
30 |
static { |
26 |
// TESTS_NAMES = new String[] { "test056throw"}; |
31 |
// TESTS_NAMES = new String[] { "test063c"}; |
27 |
// TESTS_NUMBERS = new int[] { 50 }; |
32 |
// TESTS_NUMBERS = new int[] { 50 }; |
28 |
// TESTS_RANGE = new int[] { 11, -1 }; |
33 |
// TESTS_RANGE = new int[] { 11, -1 }; |
29 |
} |
34 |
} |
Lines 3713-3723
Link Here
|
3713 |
options); |
3718 |
options); |
3714 |
} |
3719 |
} |
3715 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3720 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
|
|
3721 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
3716 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3722 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3717 |
public void test056e() { |
3723 |
public void test056e() { |
3718 |
Map options = getCompilerOptions(); |
3724 |
Map options = getCompilerOptions(); |
3719 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3725 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3720 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3726 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3721 |
this.runNegativeTest( |
3727 |
this.runNegativeTest( |
3722 |
new String[] { |
3728 |
new String[] { |
3723 |
"X.java", |
3729 |
"X.java", |
Lines 3744-3750
Link Here
|
3744 |
"1. ERROR in X.java (at line 11)\n" + |
3750 |
"1. ERROR in X.java (at line 11)\n" + |
3745 |
" FileReader reader = getReader(\"somefile\");\n" + |
3751 |
" FileReader reader = getReader(\"somefile\");\n" + |
3746 |
" ^^^^^^\n" + |
3752 |
" ^^^^^^\n" + |
3747 |
"Resource leak: 'reader' is never closed\n" + |
3753 |
"Potential resource leak: \'reader\' may not be closed\n" + |
3748 |
"----------\n", |
3754 |
"----------\n", |
3749 |
null, |
3755 |
null, |
3750 |
true, |
3756 |
true, |
Lines 4358-4368
Link Here
|
4358 |
null/*requestor*/); |
4364 |
null/*requestor*/); |
4359 |
} |
4365 |
} |
4360 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4366 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
|
|
4367 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
4361 |
// a method uses an AutoCloseable without ever closing it, type from a type variable |
4368 |
// a method uses an AutoCloseable without ever closing it, type from a type variable |
4362 |
public void test056p() { |
4369 |
public void test056p() { |
4363 |
Map options = getCompilerOptions(); |
4370 |
Map options = getCompilerOptions(); |
4364 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4371 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4365 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
4372 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4366 |
this.runNegativeTest( |
4373 |
this.runNegativeTest( |
4367 |
new String[] { |
4374 |
new String[] { |
4368 |
"X.java", |
4375 |
"X.java", |
Lines 4389-4395
Link Here
|
4389 |
"1. ERROR in X.java (at line 8)\n" + |
4396 |
"1. ERROR in X.java (at line 8)\n" + |
4390 |
" T fileReader = newReader(file);\n" + |
4397 |
" T fileReader = newReader(file);\n" + |
4391 |
" ^^^^^^^^^^\n" + |
4398 |
" ^^^^^^^^^^\n" + |
4392 |
"Resource leak: 'fileReader' is never closed\n" + |
4399 |
"Potential resource leak: \'fileReader\' may not be closed\n" + |
4393 |
"----------\n", |
4400 |
"----------\n", |
4394 |
null, |
4401 |
null, |
4395 |
true, |
4402 |
true, |
Lines 4444-4450
Link Here
|
4444 |
options); |
4451 |
options); |
4445 |
} |
4452 |
} |
4446 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4453 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4447 |
// closed in dead code |
4454 |
// properly closed, dead code in between |
4448 |
public void test056r() { |
4455 |
public void test056r() { |
4449 |
Map options = getCompilerOptions(); |
4456 |
Map options = getCompilerOptions(); |
4450 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4457 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
Lines 5354-5360
Link Here
|
5354 |
"X::~X\n" + |
5361 |
"X::~X\n" + |
5355 |
"true"); |
5362 |
"true"); |
5356 |
} |
5363 |
} |
5357 |
|
5364 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
|
|
5365 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
5366 |
// a resource wrapper is not closed but the underlying resource is |
5367 |
public void test061a() { |
5368 |
Map options = getCompilerOptions(); |
5369 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5370 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5371 |
this.runConformTest( |
5372 |
new String[] { |
5373 |
"X.java", |
5374 |
"import java.io.File;\n" + |
5375 |
"import java.io.BufferedInputStream;\n" + |
5376 |
"import java.io.FileInputStream;\n" + |
5377 |
"import java.io.IOException;\n" + |
5378 |
"public class X {\n" + |
5379 |
" void foo() throws IOException {\n" + |
5380 |
" File file = new File(\"somefile\");\n" + |
5381 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5382 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5383 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5384 |
" System.out.println(bis.available());\n" + |
5385 |
" fileStream.close();\n" + |
5386 |
" }\n" + |
5387 |
" void inline() throws IOException {\n" + |
5388 |
" File file = new File(\"somefile\");\n" + |
5389 |
" FileInputStream fileStream;\n" + |
5390 |
" BufferedInputStream bis = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
5391 |
" System.out.println(bis.available());\n" + |
5392 |
" fileStream.close();\n" + |
5393 |
" }\n" + |
5394 |
" public static void main(String[] args) throws IOException {\n" + |
5395 |
" try {\n" + |
5396 |
" new X().foo();\n" + |
5397 |
" } catch (IOException ex) {" + |
5398 |
" System.out.println(\"Got IO Exception\");\n" + |
5399 |
" }\n" + |
5400 |
" }\n" + |
5401 |
"}\n" |
5402 |
}, |
5403 |
"Got IO Exception", |
5404 |
null, |
5405 |
true, |
5406 |
null, |
5407 |
options, |
5408 |
null); |
5409 |
} |
5410 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5411 |
// a closeable without OS resource is not closed |
5412 |
public void test061b() { |
5413 |
Map options = getCompilerOptions(); |
5414 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5415 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5416 |
this.runConformTest( |
5417 |
new String[] { |
5418 |
"X.java", |
5419 |
"import java.io.StringReader;\n" + |
5420 |
"import java.io.IOException;\n" + |
5421 |
"public class X {\n" + |
5422 |
" void foo() throws IOException {\n" + |
5423 |
" StringReader string = new StringReader(\"content\");\n" + |
5424 |
" System.out.println(string.read());\n" + |
5425 |
" }\n" + |
5426 |
" public static void main(String[] args) throws IOException {\n" + |
5427 |
" new X().foo();\n" + |
5428 |
" }\n" + |
5429 |
"}\n" |
5430 |
}, |
5431 |
"99", // character 'c' |
5432 |
null, |
5433 |
true, |
5434 |
null, |
5435 |
options, |
5436 |
null); |
5437 |
} |
5438 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5439 |
// a resource wrapper is not closed but the underlying closeable is resource-free |
5440 |
public void test061c() { |
5441 |
Map options = getCompilerOptions(); |
5442 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5443 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5444 |
this.runConformTest( |
5445 |
new String[] { |
5446 |
"X.java", |
5447 |
"import java.io.BufferedReader;\n" + |
5448 |
"import java.io.StringReader;\n" + |
5449 |
"import java.io.IOException;\n" + |
5450 |
"public class X {\n" + |
5451 |
" void foo() throws IOException {\n" + |
5452 |
" StringReader input = new StringReader(\"content\");\n" + |
5453 |
" BufferedReader br = new BufferedReader(input);\n" + |
5454 |
" BufferedReader doubleWrap = new BufferedReader(br);\n" + |
5455 |
" System.out.println(br.read());\n" + |
5456 |
" }\n" + |
5457 |
" void inline() throws IOException {\n" + |
5458 |
" BufferedReader br = new BufferedReader(new StringReader(\"content\"));\n" + |
5459 |
" System.out.println(br.read());\n" + |
5460 |
" }\n" + |
5461 |
" public static void main(String[] args) throws IOException {\n" + |
5462 |
" new X().foo();\n" + |
5463 |
" }\n" + |
5464 |
"}\n" |
5465 |
}, |
5466 |
"99", |
5467 |
null, |
5468 |
true, |
5469 |
null, |
5470 |
options, |
5471 |
null); |
5472 |
} |
5473 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5474 |
// a resource wrapper is not closed neither is the underlying resource |
5475 |
public void test061d() { |
5476 |
Map options = getCompilerOptions(); |
5477 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5478 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
5479 |
this.runNegativeTest( |
5480 |
new String[] { |
5481 |
"X.java", |
5482 |
"import java.io.File;\n" + |
5483 |
"import java.io.BufferedInputStream;\n" + |
5484 |
"import java.io.FileInputStream;\n" + |
5485 |
"import java.io.IOException;\n" + |
5486 |
"public class X {\n" + |
5487 |
" void foo() throws IOException {\n" + |
5488 |
" File file = new File(\"somefile\");\n" + |
5489 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5490 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5491 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5492 |
" System.out.println(bis.available());\n" + |
5493 |
" }\n" + |
5494 |
" void inline() throws IOException {\n" + |
5495 |
" File file = new File(\"somefile\");\n" + |
5496 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
5497 |
" System.out.println(bis2.available());\n" + |
5498 |
" }\n" + |
5499 |
" public static void main(String[] args) throws IOException {\n" + |
5500 |
" try {\n" + |
5501 |
" new X().foo();\n" + |
5502 |
" } catch (IOException ex) {" + |
5503 |
" System.out.println(\"Got IO Exception\");\n" + |
5504 |
" }\n" + |
5505 |
" }\n" + |
5506 |
"}\n" |
5507 |
}, |
5508 |
"----------\n" + |
5509 |
"1. ERROR in X.java (at line 10)\n" + |
5510 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5511 |
" ^^^^^^^^^^\n" + |
5512 |
"Resource leak: \'doubleWrap\' is never closed\n" + |
5513 |
"----------\n" + |
5514 |
"2. ERROR in X.java (at line 15)\n" + |
5515 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
5516 |
" ^^^^\n" + |
5517 |
"Resource leak: \'bis2\' is never closed\n" + |
5518 |
"----------\n", |
5519 |
null, |
5520 |
true, |
5521 |
options); |
5522 |
} |
5523 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5524 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
5525 |
// a resource wrapper is closed closing also the underlying resource |
5526 |
public void test061e() { |
5527 |
Map options = getCompilerOptions(); |
5528 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5529 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5530 |
this.runConformTest( |
5531 |
new String[] { |
5532 |
"X.java", |
5533 |
"import java.io.File;\n" + |
5534 |
"import java.io.BufferedInputStream;\n" + |
5535 |
"import java.io.FileInputStream;\n" + |
5536 |
"import java.io.IOException;\n" + |
5537 |
"public class X {\n" + |
5538 |
" FileInputStream fis;" + |
5539 |
" void foo() throws IOException {\n" + |
5540 |
" File file = new File(\"somefile\");\n" + |
5541 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5542 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5543 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5544 |
" System.out.println(bis.available());\n" + |
5545 |
" bis.close();\n" + |
5546 |
" }\n" + |
5547 |
" void inline() throws IOException {\n" + |
5548 |
" File file = new File(\"somefile\");\n" + |
5549 |
" BufferedInputStream bis2 = new BufferedInputStream(fis = new FileInputStream(file));\n" + // field assignment |
5550 |
" System.out.println(bis2.available());\n" + |
5551 |
" bis2.close();\n" + |
5552 |
" FileInputStream fileStream = null;\n" + |
5553 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
5554 |
" System.out.println(bis3.available());\n" + |
5555 |
" bis3.close();\n" + |
5556 |
" }\n" + |
5557 |
" public static void main(String[] args) throws IOException {\n" + |
5558 |
" try {\n" + |
5559 |
" new X().foo();\n" + |
5560 |
" } catch (IOException ex) {" + |
5561 |
" System.out.println(\"Got IO Exception\");\n" + |
5562 |
" }\n" + |
5563 |
" }\n" + |
5564 |
"}\n" |
5565 |
}, |
5566 |
"Got IO Exception", |
5567 |
null, |
5568 |
true, |
5569 |
null, |
5570 |
options, |
5571 |
null); |
5572 |
} |
5573 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5574 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
5575 |
// a resource wrapper is closed closing also the underlying resource - original test case |
5576 |
public void test061f() throws IOException { |
5577 |
Map options = getCompilerOptions(); |
5578 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5579 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5580 |
URL url = FileLocator.toFileURL(FileLocator.find(Platform.getBundle("org.eclipse.jdt.core.tests.compiler"), new Path("META-INF/MANIFEST.MF"), null)); |
5581 |
this.runConformTest( |
5582 |
new String[] { |
5583 |
"X.java", |
5584 |
"import java.io.InputStream;\n" + |
5585 |
"import java.io.InputStreamReader;\n" + |
5586 |
"import java.io.BufferedReader;\n" + |
5587 |
"import java.io.IOException;\n" + |
5588 |
"import java.net.URL;\n" + |
5589 |
"public class X {\n" + |
5590 |
" boolean loadURL(final URL url) throws IOException {\n" + |
5591 |
" InputStream stream = null;\n" + |
5592 |
" BufferedReader reader = null;\n" + |
5593 |
" try {\n" + |
5594 |
" stream = url.openStream();\n" + |
5595 |
" reader = new BufferedReader(new InputStreamReader(stream));\n" + |
5596 |
" System.out.println(reader.readLine());\n" + |
5597 |
" } finally {\n" + |
5598 |
" try {\n" + |
5599 |
" if (reader != null)\n" + |
5600 |
" reader.close();\n" + |
5601 |
" } catch (IOException x) {\n" + |
5602 |
" }\n" + |
5603 |
" }\n" + |
5604 |
" return false; // 'stream' may not be closed at this location\n" + |
5605 |
" }\n" + |
5606 |
" public static void main(String[] args) throws IOException {\n" + |
5607 |
" try {\n" + |
5608 |
" new X().loadURL(new URL(\""+url.toString()+"\"));\n" + |
5609 |
" } catch (IOException ex) {\n" + |
5610 |
" System.out.println(\"Got IO Exception\"+ex);\n" + |
5611 |
" }\n" + |
5612 |
" }\n" + |
5613 |
"}\n" |
5614 |
}, |
5615 |
"Manifest-Version: 1.0", |
5616 |
null, |
5617 |
true, |
5618 |
null, |
5619 |
options, |
5620 |
null); |
5621 |
} |
5622 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5623 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
5624 |
// Different points in a resource chain are closed |
5625 |
public void test061g() { |
5626 |
Map options = getCompilerOptions(); |
5627 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5628 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5629 |
this.runNegativeTest( |
5630 |
new String[] { |
5631 |
"X.java", |
5632 |
"import java.io.File;\n" + |
5633 |
"import java.io.BufferedInputStream;\n" + |
5634 |
"import java.io.FileInputStream;\n" + |
5635 |
"import java.io.IOException;\n" + |
5636 |
"public class X {\n" + |
5637 |
" void closeMiddle() throws IOException {\n" + |
5638 |
" File file = new File(\"somefile\");\n" + |
5639 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5640 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5641 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5642 |
" System.out.println(bis.available());\n" + |
5643 |
" bis.close();\n" + |
5644 |
" }\n" + |
5645 |
" void closeOuter() throws IOException {\n" + |
5646 |
" File file2 = new File(\"somefile\");\n" + |
5647 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
5648 |
" BufferedInputStream bis2 = new BufferedInputStream(fileStream2);\n" + |
5649 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
5650 |
" System.out.println(bis2.available());\n" + |
5651 |
" doubleWrap2.close();\n" + |
5652 |
" }\n" + |
5653 |
" void neverClosed() throws IOException {\n" + |
5654 |
" File file3 = new File(\"somefile\");\n" + |
5655 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
5656 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
5657 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
5658 |
" System.out.println(doubleWrap3.available());\n" + |
5659 |
" }\n" + |
5660 |
"}\n" |
5661 |
}, |
5662 |
"----------\n" + |
5663 |
"1. ERROR in X.java (at line 26)\n" + |
5664 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
5665 |
" ^^^^^^^^^^^\n" + |
5666 |
"Resource leak: \'doubleWrap3\' is never closed\n" + |
5667 |
"----------\n", |
5668 |
null, |
5669 |
true, |
5670 |
options); |
5671 |
} |
5672 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5673 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
5674 |
// Different points in a resource chain are potentially closed |
5675 |
public void test061h() { |
5676 |
Map options = getCompilerOptions(); |
5677 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5678 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5679 |
this.runNegativeTest( |
5680 |
new String[] { |
5681 |
"X.java", |
5682 |
"import java.io.File;\n" + |
5683 |
"import java.io.BufferedInputStream;\n" + |
5684 |
"import java.io.FileInputStream;\n" + |
5685 |
"import java.io.IOException;\n" + |
5686 |
"public class X {\n" + |
5687 |
" void closeMiddle(boolean b) throws IOException {\n" + |
5688 |
" File file = new File(\"somefile\");\n" + |
5689 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5690 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5691 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5692 |
" System.out.println(bis.available());\n" + |
5693 |
" if (b)\n" + |
5694 |
" bis.close();\n" + |
5695 |
" }\n" + |
5696 |
" void closeOuter(boolean b) throws IOException {\n" + |
5697 |
" File file2 = new File(\"somefile\");\n" + |
5698 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
5699 |
" BufferedInputStream dummy;\n" + |
5700 |
" BufferedInputStream bis2 = (dummy = new BufferedInputStream(fileStream2));\n" + |
5701 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
5702 |
" System.out.println(bis2.available());\n" + |
5703 |
" if (b)\n" + |
5704 |
" doubleWrap2.close();\n" + |
5705 |
" }\n" + |
5706 |
" void potAndDef(boolean b) throws IOException {\n" + |
5707 |
" File file3 = new File(\"somefile\");\n" + |
5708 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
5709 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
5710 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
5711 |
" System.out.println(doubleWrap3.available());\n" + |
5712 |
" if (b) bis3.close();\n" + |
5713 |
" fileStream3.close();\n" + |
5714 |
" }\n" + |
5715 |
"}\n" |
5716 |
}, |
5717 |
"----------\n" + |
5718 |
"1. ERROR in X.java (at line 10)\n" + |
5719 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5720 |
" ^^^^^^^^^^\n" + |
5721 |
"Potential resource leak: \'doubleWrap\' may not be closed\n" + |
5722 |
"----------\n" + |
5723 |
"2. ERROR in X.java (at line 20)\n" + |
5724 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
5725 |
" ^^^^^^^^^^^\n" + |
5726 |
"Potential resource leak: \'doubleWrap2\' may not be closed\n" + |
5727 |
"----------\n", |
5728 |
null, |
5729 |
true, |
5730 |
options); |
5731 |
} |
5732 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5733 |
// local var is re-used for two levels of wrappers |
5734 |
public void test061i() { |
5735 |
Map options = getCompilerOptions(); |
5736 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5737 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5738 |
this.runNegativeTest( |
5739 |
new String[] { |
5740 |
"X.java", |
5741 |
"import java.io.File;\n" + |
5742 |
"import java.io.InputStream;\n" + |
5743 |
"import java.io.BufferedInputStream;\n" + |
5744 |
"import java.io.FileInputStream;\n" + |
5745 |
"import java.io.IOException;\n" + |
5746 |
"public class X {\n" + |
5747 |
" void closeMiddle() throws IOException {\n" + |
5748 |
" File file = new File(\"somefile\");\n" + |
5749 |
" InputStream stream = new FileInputStream(file);\n" + |
5750 |
" stream = new BufferedInputStream(stream);\n" + |
5751 |
" InputStream middle;\n" + |
5752 |
" stream = new BufferedInputStream(middle = stream);\n" + |
5753 |
" System.out.println(stream.available());\n" + |
5754 |
" middle.close();\n" + |
5755 |
" }\n" + |
5756 |
" void closeOuter() throws IOException {\n" + |
5757 |
" File file = new File(\"somefile\");\n" + |
5758 |
" InputStream stream2 = new FileInputStream(file);\n" + |
5759 |
" stream2 = new BufferedInputStream(stream2);\n" + |
5760 |
" stream2 = new BufferedInputStream(stream2);\n" + |
5761 |
" System.out.println(stream2.available());\n" + |
5762 |
" stream2.close();\n" + |
5763 |
" }\n" + |
5764 |
" void neverClosed() throws IOException {\n" + |
5765 |
" File file = new File(\"somefile\");\n" + |
5766 |
" InputStream stream3 = new FileInputStream(file);\n" + |
5767 |
" stream3 = new BufferedInputStream(stream3);\n" + |
5768 |
" stream3 = new BufferedInputStream(stream3);\n" + |
5769 |
" System.out.println(stream3.available());\n" + |
5770 |
" }\n" + |
5771 |
"}\n" |
5772 |
}, |
5773 |
"----------\n" + |
5774 |
"1. ERROR in X.java (at line 26)\n" + |
5775 |
" InputStream stream3 = new FileInputStream(file);\n" + |
5776 |
" ^^^^^^^\n" + |
5777 |
"Resource leak: \'stream3\' is never closed\n" + |
5778 |
"----------\n", |
5779 |
null, |
5780 |
true, |
5781 |
options); |
5782 |
} |
5783 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5784 |
// self-wrapping a method argument (caused NPE UnconditionalFlowInfo.markAsDefinitelyNull(..)). |
5785 |
public void test061j() { |
5786 |
Map options = getCompilerOptions(); |
5787 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5788 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5789 |
this.runConformTest( |
5790 |
new String[] { |
5791 |
"X.java", |
5792 |
"import java.io.InputStream;\n" + |
5793 |
"import java.io.BufferedInputStream;\n" + |
5794 |
"import java.io.IOException;\n" + |
5795 |
"public class X {\n" + |
5796 |
" void foo(InputStream stream) throws IOException {\n" + |
5797 |
" stream = new BufferedInputStream(stream);\n" + |
5798 |
" System.out.println(stream.available());\n" + |
5799 |
" stream.close();\n" + |
5800 |
" }\n" + |
5801 |
" void boo(InputStream stream2) throws IOException {\n" + |
5802 |
" stream2 = new BufferedInputStream(stream2);\n" + |
5803 |
" System.out.println(stream2.available());\n" + |
5804 |
" }\n" + |
5805 |
"}\n" |
5806 |
}, |
5807 |
"", |
5808 |
null, |
5809 |
true, |
5810 |
null, |
5811 |
options, |
5812 |
null); |
5813 |
} |
5814 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5815 |
// a wrapper is created in a return statement |
5816 |
public void test061k() throws IOException { |
5817 |
Map options = getCompilerOptions(); |
5818 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5819 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5820 |
this.runConformTest( |
5821 |
new String[] { |
5822 |
"X.java", |
5823 |
"import java.io.File;\n" + |
5824 |
"import java.io.FileInputStream;\n" + |
5825 |
"import java.io.BufferedInputStream;\n" + |
5826 |
"import java.io.IOException;\n" + |
5827 |
"public class X {\n" + |
5828 |
" BufferedInputStream getReader(File file) throws IOException {\n" + |
5829 |
" FileInputStream stream = new FileInputStream(file);\n" + |
5830 |
" return new BufferedInputStream(stream);\n" + |
5831 |
" }\n" + |
5832 |
"}\n" |
5833 |
}, |
5834 |
"", |
5835 |
null, |
5836 |
true, |
5837 |
null, |
5838 |
options, |
5839 |
null); |
5840 |
} |
5841 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5842 |
// a closeable is assigned to a field |
5843 |
public void test061l() throws IOException { |
5844 |
Map options = getCompilerOptions(); |
5845 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5846 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5847 |
this.runConformTest( |
5848 |
new String[] { |
5849 |
"X.java", |
5850 |
"import java.io.File;\n" + |
5851 |
"import java.io.FileInputStream;\n" + |
5852 |
"import java.io.BufferedInputStream;\n" + |
5853 |
"import java.io.IOException;\n" + |
5854 |
"public class X {\n" + |
5855 |
" BufferedInputStream stream;\n" + |
5856 |
" void foo(File file) throws IOException {\n" + |
5857 |
" FileInputStream s = new FileInputStream(file);\n" + |
5858 |
" stream = new BufferedInputStream(s);\n" + |
5859 |
" }\n" + |
5860 |
"}\n" |
5861 |
}, |
5862 |
"", |
5863 |
null, |
5864 |
true, |
5865 |
null, |
5866 |
options, |
5867 |
null); |
5868 |
} |
5869 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
5870 |
// a resource is never assigned |
5871 |
public void test062a() throws IOException { |
5872 |
Map options = getCompilerOptions(); |
5873 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5874 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5875 |
this.runNegativeTest( |
5876 |
new String[] { |
5877 |
"X.java", |
5878 |
"import java.io.File;\n" + |
5879 |
"import java.io.FileOutputStream;\n" + |
5880 |
"import java.io.IOException;\n" + |
5881 |
"public class X {\n" + |
5882 |
" void foo() throws IOException {\n" + |
5883 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).write(1);\n" + |
5884 |
" }\n" + |
5885 |
"}\n" |
5886 |
}, |
5887 |
"----------\n" + |
5888 |
"1. ERROR in X.java (at line 6)\n" + |
5889 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).write(1);\n" + |
5890 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
5891 |
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" + |
5892 |
"----------\n", |
5893 |
null, |
5894 |
true, |
5895 |
options); |
5896 |
} |
5897 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
5898 |
// a freshly allocated resource is immediately closed |
5899 |
public void test062b() throws IOException { |
5900 |
Map options = getCompilerOptions(); |
5901 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5902 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5903 |
this.runConformTest( |
5904 |
new String[] { |
5905 |
"X.java", |
5906 |
"import java.io.File;\n" + |
5907 |
"import java.io.FileOutputStream;\n" + |
5908 |
"import java.io.IOException;\n" + |
5909 |
"public class X {\n" + |
5910 |
" void foo() throws IOException {\n" + |
5911 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).close();\n" + |
5912 |
" }\n" + |
5913 |
"}\n" |
5914 |
}, |
5915 |
"", |
5916 |
null, |
5917 |
true, |
5918 |
null, |
5919 |
options, |
5920 |
null); |
5921 |
} |
5922 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
5923 |
// a resource is directly passed to another method |
5924 |
public void test062c() throws IOException { |
5925 |
Map options = getCompilerOptions(); |
5926 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5927 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5928 |
this.runConformTest( |
5929 |
new String[] { |
5930 |
"X.java", |
5931 |
"import java.io.File;\n" + |
5932 |
"import java.io.FileOutputStream;\n" + |
5933 |
"import java.io.IOException;\n" + |
5934 |
"public class X {\n" + |
5935 |
" void foo() throws IOException {\n" + |
5936 |
" writeIt(new FileOutputStream(new File(\"C:\\temp\\foo.txt\")));\n" + |
5937 |
" }\n" + |
5938 |
" void writeIt(FileOutputStream fos) throws IOException {\n" + |
5939 |
" fos.write(1);\n" + |
5940 |
" fos.close();\n" + |
5941 |
" }\n" + |
5942 |
"}\n" |
5943 |
}, |
5944 |
"", |
5945 |
null, |
5946 |
true, |
5947 |
null, |
5948 |
options, |
5949 |
null); |
5950 |
} |
5951 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
5952 |
// a resource is not used |
5953 |
public void test062d() throws IOException { |
5954 |
Map options = getCompilerOptions(); |
5955 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5956 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5957 |
this.runNegativeTest( |
5958 |
new String[] { |
5959 |
"X.java", |
5960 |
"import java.io.File;\n" + |
5961 |
"import java.io.FileOutputStream;\n" + |
5962 |
"import java.io.IOException;\n" + |
5963 |
"public class X {\n" + |
5964 |
" void foo() throws IOException {\n" + |
5965 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\"));\n" + |
5966 |
" }\n" + |
5967 |
"}\n" |
5968 |
}, |
5969 |
"----------\n" + |
5970 |
"1. ERROR in X.java (at line 6)\n" + |
5971 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\"));\n" + |
5972 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
5973 |
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" + |
5974 |
"----------\n", |
5975 |
null, |
5976 |
true, |
5977 |
options); |
5978 |
} |
5979 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
5980 |
// a wrapper is obtained from another method |
5981 |
public void test063a() throws IOException { |
5982 |
Map options = getCompilerOptions(); |
5983 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5984 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5985 |
this.runNegativeTest( |
5986 |
new String[] { |
5987 |
"X.java", |
5988 |
"import java.io.File;\n" + |
5989 |
"import java.io.FileInputStream;\n" + |
5990 |
"import java.io.BufferedInputStream;\n" + |
5991 |
"import java.io.IOException;\n" + |
5992 |
"public class X {\n" + |
5993 |
" void read(File file) throws IOException {\n" + |
5994 |
" FileInputStream stream = new FileInputStream(file);\n" + |
5995 |
" BufferedInputStream bis = new BufferedInputStream(stream); // never since reassigned\n" + |
5996 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
5997 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
5998 |
" bis.available();\n" + |
5999 |
" }\n" + |
6000 |
" BufferedInputStream getReader(FileInputStream stream) throws IOException {\n" + |
6001 |
" return new BufferedInputStream(stream);\n" + |
6002 |
" }\n" + |
6003 |
"}\n" |
6004 |
}, |
6005 |
"----------\n" + |
6006 |
"1. ERROR in X.java (at line 8)\n" + |
6007 |
" BufferedInputStream bis = new BufferedInputStream(stream); // never since reassigned\n" + |
6008 |
" ^^^\n" + |
6009 |
"Resource leak: \'bis\' is never closed\n" + |
6010 |
"----------\n" + |
6011 |
"2. ERROR in X.java (at line 9)\n" + |
6012 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
6013 |
" ^^^^^^^\n" + |
6014 |
"Potential resource leak: \'stream2\' may not be closed\n" + |
6015 |
"----------\n" + |
6016 |
"3. ERROR in X.java (at line 10)\n" + |
6017 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
6018 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
6019 |
"Potential resource leak: \'bis\' may not be closed\n" + |
6020 |
"----------\n", |
6021 |
null, |
6022 |
true, |
6023 |
options); |
6024 |
} |
6025 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
6026 |
// a wrapper is obtained from a field read |
6027 |
public void test063b() throws IOException { |
6028 |
Map options = getCompilerOptions(); |
6029 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
6030 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
6031 |
this.runNegativeTest( |
6032 |
new String[] { |
6033 |
"X.java", |
6034 |
"import java.io.FileInputStream;\n" + |
6035 |
"import java.io.BufferedInputStream;\n" + |
6036 |
"import java.io.IOException;\n" + |
6037 |
"public class X {\n" + |
6038 |
" FileInputStream stream;\n" + |
6039 |
" void read() throws IOException {\n" + |
6040 |
" FileInputStream s = this.stream;\n" + |
6041 |
" BufferedInputStream bis = new BufferedInputStream(s); // unsure since s is obtained from a field\n" + |
6042 |
" bis.available();\n" + |
6043 |
" }\n" + |
6044 |
"}\n" |
6045 |
}, |
6046 |
"----------\n" + |
6047 |
"1. ERROR in X.java (at line 8)\n" + |
6048 |
" BufferedInputStream bis = new BufferedInputStream(s); // unsure since s is obtained from a field\n" + |
6049 |
" ^^^\n" + |
6050 |
"Potential resource leak: \'bis\' may not be closed\n" + |
6051 |
"----------\n", |
6052 |
null, |
6053 |
true, |
6054 |
options); |
6055 |
} |
6056 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
6057 |
// a wrapper is assigned to a field |
6058 |
public void test063c() throws IOException { |
6059 |
Map options = getCompilerOptions(); |
6060 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
6061 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
6062 |
this.runConformTest( |
6063 |
new String[] { |
6064 |
"X.java", |
6065 |
"import java.io.FileInputStream;\n" + |
6066 |
"import java.io.BufferedInputStream;\n" + |
6067 |
"import java.io.IOException;\n" + |
6068 |
"public class X {\n" + |
6069 |
" BufferedInputStream stream;\n" + |
6070 |
" void read() throws IOException {\n" + |
6071 |
" FileInputStream s = new FileInputStream(\"somefile\");\n" + |
6072 |
" BufferedInputStream bis = new BufferedInputStream(s);\n" + |
6073 |
" this.stream = bis;\n" + |
6074 |
" }\n" + |
6075 |
"}\n" |
6076 |
}, |
6077 |
"", |
6078 |
null, |
6079 |
true, |
6080 |
null, |
6081 |
options, |
6082 |
null); |
6083 |
} |
5358 |
public static Class testClass() { |
6084 |
public static Class testClass() { |
5359 |
return TryWithResourcesStatementTest.class; |
6085 |
return TryWithResourcesStatementTest.class; |
5360 |
} |
6086 |
} |