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