Lines 7-25
Link Here
|
7 |
* |
7 |
* |
8 |
* Contributors: |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
10 |
* Stephan Herrmann - Contribution for bug 358827 - [1.7] exception analysis for t-w-r spoils null analysis |
10 |
* Stephan Herrmann - Contributions for |
|
|
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 |
11 |
*******************************************************************************/ |
13 |
*******************************************************************************/ |
12 |
package org.eclipse.jdt.core.tests.compiler.regression; |
14 |
package org.eclipse.jdt.core.tests.compiler.regression; |
13 |
|
15 |
|
14 |
import java.util.Map; |
16 |
import java.util.Map; |
15 |
|
17 |
|
|
|
18 |
import org.eclipse.jdt.core.JavaCore; |
16 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
19 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
17 |
|
20 |
|
18 |
import junit.framework.Test; |
21 |
import junit.framework.Test; |
19 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
22 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
20 |
|
23 |
|
21 |
static { |
24 |
static { |
22 |
// TESTS_NAMES = new String[] { "test055", "test055a" }; |
25 |
// TESTS_NAMES = new String[] { "test056zz"}; |
23 |
// TESTS_NUMBERS = new int[] { 50 }; |
26 |
// TESTS_NUMBERS = new int[] { 50 }; |
24 |
// TESTS_RANGE = new int[] { 11, -1 }; |
27 |
// TESTS_RANGE = new int[] { 11, -1 }; |
25 |
} |
28 |
} |
Lines 486-497
Link Here
|
486 |
" ^^\n" + |
489 |
" ^^\n" + |
487 |
"Dead code\n" + |
490 |
"Dead code\n" + |
488 |
"----------\n" + |
491 |
"----------\n" + |
489 |
"3. ERROR in X.java (at line 5)\n" + |
492 |
"3. WARNING in X.java (at line 5)\n" + |
|
|
493 |
" Y why = new Y();\n" + |
494 |
" ^^^\n" + |
495 |
"Resource leak: 'why' is never closed\n" + |
496 |
"----------\n" + |
497 |
"4. ERROR in X.java (at line 5)\n" + |
490 |
" Y why = new Y();\n" + |
498 |
" Y why = new Y();\n" + |
491 |
" ^^^^^^^\n" + |
499 |
" ^^^^^^^\n" + |
492 |
"Unhandled exception type WeirdException\n" + |
500 |
"Unhandled exception type WeirdException\n" + |
493 |
"----------\n" + |
501 |
"----------\n" + |
494 |
"4. WARNING in X.java (at line 22)\n" + |
502 |
"5. WARNING in X.java (at line 22)\n" + |
495 |
" class WeirdException extends Throwable {}\n" + |
503 |
" class WeirdException extends Throwable {}\n" + |
496 |
" ^^^^^^^^^^^^^^\n" + |
504 |
" ^^^^^^^^^^^^^^\n" + |
497 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
505 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
Lines 559-570
Link Here
|
559 |
" ^^\n" + |
567 |
" ^^\n" + |
560 |
"Dead code\n" + |
568 |
"Dead code\n" + |
561 |
"----------\n" + |
569 |
"----------\n" + |
562 |
"3. ERROR in X.java (at line 5)\n" + |
570 |
"3. WARNING in X.java (at line 5)\n" + |
|
|
571 |
" Y why = new Y();\n" + |
572 |
" ^^^\n" + |
573 |
"Resource leak: 'why' is never closed\n" + |
574 |
"----------\n" + |
575 |
"4. ERROR in X.java (at line 5)\n" + |
563 |
" Y why = new Y();\n" + |
576 |
" Y why = new Y();\n" + |
564 |
" ^^^^^^^\n" + |
577 |
" ^^^^^^^\n" + |
565 |
"Unhandled exception type WeirdException\n" + |
578 |
"Unhandled exception type WeirdException\n" + |
566 |
"----------\n" + |
579 |
"----------\n" + |
567 |
"4. WARNING in X.java (at line 20)\n" + |
580 |
"5. WARNING in X.java (at line 20)\n" + |
568 |
" class WeirdException extends Throwable {}\n" + |
581 |
" class WeirdException extends Throwable {}\n" + |
569 |
" ^^^^^^^^^^^^^^\n" + |
582 |
" ^^^^^^^^^^^^^^\n" + |
570 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
583 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
Lines 3432-3437
Link Here
|
3432 |
}, |
3445 |
}, |
3433 |
"Done"); |
3446 |
"Done"); |
3434 |
} |
3447 |
} |
|
|
3448 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3449 |
// a method uses an AutoCloseable without ever closing it. |
3450 |
public void test056() { |
3451 |
Map options = getCompilerOptions(); |
3452 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3453 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3454 |
this.runNegativeTest( |
3455 |
new String[] { |
3456 |
"X.java", |
3457 |
"import java.io.File;\n" + |
3458 |
"import java.io.FileReader;\n" + |
3459 |
"import java.io.IOException;\n" + |
3460 |
"public class X {\n" + |
3461 |
" void foo() throws IOException {\n" + |
3462 |
" File file = new File(\"somefile\");\n" + |
3463 |
" FileReader fileReader = new FileReader(file);\n" + |
3464 |
// not invoking any methods on FileReader, try to avoid necessary call to superclass() in the compiler |
3465 |
// " char[] in = new char[50];\n" + |
3466 |
// " fileReader.read(in);\n" + |
3467 |
" }\n" + |
3468 |
" public static void main(String[] args) throws IOException {\n" + |
3469 |
" new X().foo();\n" + |
3470 |
" }\n" + |
3471 |
"}\n" |
3472 |
}, |
3473 |
"----------\n" + |
3474 |
"1. ERROR in X.java (at line 7)\n" + |
3475 |
" FileReader fileReader = new FileReader(file);\n" + |
3476 |
" ^^^^^^^^^^\n" + |
3477 |
"Resource leak: 'fileReader' is never closed\n" + |
3478 |
"----------\n", |
3479 |
null, |
3480 |
true, |
3481 |
options); |
3482 |
} |
3483 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3484 |
// a method uses an AutoCloseable and closes it but not protected by t-w-r nor regular try-finally |
3485 |
public void test056a() { |
3486 |
Map options = getCompilerOptions(); |
3487 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3488 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3489 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3490 |
this.runNegativeTest( |
3491 |
new String[] { |
3492 |
"X.java", |
3493 |
"import java.io.File;\n" + |
3494 |
"import java.io.FileReader;\n" + |
3495 |
"import java.io.IOException;\n" + |
3496 |
"public class X {\n" + |
3497 |
" void foo() throws IOException {\n" + |
3498 |
" File file = new File(\"somefile\");\n" + |
3499 |
" FileReader fileReader = new FileReader(file);\n" + |
3500 |
" char[] in = new char[50];\n" + |
3501 |
" fileReader.read(in);\n" + |
3502 |
" fileReader.close();\n" + |
3503 |
" }\n" + |
3504 |
" public static void main(String[] args) {\n" + |
3505 |
" try {\n" + |
3506 |
" new X().foo();\n" + |
3507 |
" } catch (IOException ioex) {\n" + |
3508 |
" System.out.println(\"caught\");\n" + |
3509 |
" }\n" + |
3510 |
" }\n" + |
3511 |
"}\n" |
3512 |
}, |
3513 |
"----------\n" + |
3514 |
"1. ERROR in X.java (at line 7)\n" + |
3515 |
" FileReader fileReader = new FileReader(file);\n" + |
3516 |
" ^^^^^^^^^^\n" + |
3517 |
"Resource 'fileReader' should be managed by try-with-resource\n" + |
3518 |
"----------\n", |
3519 |
null, |
3520 |
true, |
3521 |
options); |
3522 |
} |
3523 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3524 |
// a method uses an AutoCloseable and closes it properly in a finally block |
3525 |
public void test056b() { |
3526 |
Map options = getCompilerOptions(); |
3527 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3528 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3529 |
this.runConformTest( |
3530 |
new String[] { |
3531 |
"X.java", |
3532 |
"import java.io.File;\n" + |
3533 |
"import java.io.FileReader;\n" + |
3534 |
"import java.io.IOException;\n" + |
3535 |
"public class X {\n" + |
3536 |
" void foo() throws IOException {\n" + |
3537 |
" File file = new File(\"somefile\");\n" + |
3538 |
" FileReader fileReader = new FileReader(file);\n" + |
3539 |
" try {\n" + |
3540 |
" char[] in = new char[50];\n" + |
3541 |
" fileReader.read(in);\n" + |
3542 |
" } finally {\n" + |
3543 |
" fileReader.close();\n" + |
3544 |
" }\n" + |
3545 |
" }\n" + |
3546 |
" public static void main(String[] args) {\n" + |
3547 |
" try {\n" + |
3548 |
" new X().foo();\n" + |
3549 |
" } catch (IOException ioex) {\n" + |
3550 |
" System.out.println(\"caught\");\n" + |
3551 |
" }\n" + |
3552 |
" }\n" + |
3553 |
"}\n" |
3554 |
}, |
3555 |
"caught", /*output*/ |
3556 |
null/*classLibs*/, |
3557 |
true/*shouldFlush*/, |
3558 |
null/*vmargs*/, |
3559 |
options, |
3560 |
null/*requestor*/); |
3561 |
} |
3562 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3563 |
// a method uses an AutoCloseable properly within try-with-resources. |
3564 |
public void test056c() { |
3565 |
Map options = getCompilerOptions(); |
3566 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3567 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3568 |
this.runConformTest( |
3569 |
new String[] { |
3570 |
"X.java", |
3571 |
"import java.io.File;\n" + |
3572 |
"import java.io.FileReader;\n" + |
3573 |
"import java.io.IOException;\n" + |
3574 |
"public class X {\n" + |
3575 |
" void foo() throws IOException {\n" + |
3576 |
" File file = new File(\"somefile\");\n" + |
3577 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
3578 |
" char[] in = new char[50];\n" + |
3579 |
" fileReader.read(in);\n" + |
3580 |
" }\n" + |
3581 |
" }\n" + |
3582 |
" public static void main(String[] args) {\n" + |
3583 |
" try {\n" + |
3584 |
" new X().foo();\n" + |
3585 |
" } catch (IOException ioex) {\n" + |
3586 |
" System.out.println(\"caught\");\n" + |
3587 |
" }\n" + |
3588 |
" }\n" + |
3589 |
"}\n" |
3590 |
}, |
3591 |
"caught", /*output*/ |
3592 |
null/*classLibs*/, |
3593 |
true/*shouldFlush*/, |
3594 |
null/*vmargs*/, |
3595 |
options, |
3596 |
null/*requestor*/); |
3597 |
} |
3598 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3599 |
// a method uses two AutoCloseables (testing independent analysis) |
3600 |
// - one closeable may be unclosed at a conditional return |
3601 |
// - the other is only conditionally closed |
3602 |
public void test056d() { |
3603 |
Map options = getCompilerOptions(); |
3604 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3605 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3606 |
this.runNegativeTest( |
3607 |
new String[] { |
3608 |
"X.java", |
3609 |
"import java.io.File;\n" + |
3610 |
"import java.io.FileReader;\n" + |
3611 |
"import java.io.IOException;\n" + |
3612 |
"public class X {\n" + |
3613 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
3614 |
" File file = new File(\"somefile\");\n" + |
3615 |
" char[] in = new char[50];\n" + |
3616 |
" FileReader fileReader1 = new FileReader(file);\n" + |
3617 |
" fileReader1.read(in);\n" + |
3618 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3619 |
" fileReader2.read(in);\n" + |
3620 |
" if (flag1) {\n" + |
3621 |
" fileReader2.close();\n" + |
3622 |
" return;\n" + |
3623 |
" } else if (flag2) {\n" + |
3624 |
" fileReader2.close();\n" + |
3625 |
" }\n" + |
3626 |
" fileReader1.close();\n" + |
3627 |
" }\n" + |
3628 |
" public static void main(String[] args) throws IOException {\n" + |
3629 |
" new X().foo(false, true);\n" + |
3630 |
" }\n" + |
3631 |
"}\n" |
3632 |
}, |
3633 |
"----------\n" + |
3634 |
"1. WARNING in X.java (at line 10)\n" + |
3635 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3636 |
" ^^^^^^^^^^^\n" + |
3637 |
"Potential resource leak: 'fileReader2' may not be closed\n" + |
3638 |
"----------\n" + |
3639 |
"2. ERROR in X.java (at line 14)\n" + |
3640 |
" return;\n" + |
3641 |
" ^^^^^^^\n" + |
3642 |
"Resource leak: \'fileReader1\' is not closed at this location\n" + |
3643 |
"----------\n", |
3644 |
null, |
3645 |
true, |
3646 |
options); |
3647 |
} |
3648 |
//Bug 349326 - [1.7] new warning for missing try-with-resources |
3649 |
//a method uses two AutoCloseables (testing independent analysis) |
3650 |
//- one closeable may be unclosed at a conditional return |
3651 |
//- the other is only conditionally closed |
3652 |
public void test056d_suppress() { |
3653 |
Map options = getCompilerOptions(); |
3654 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3655 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3656 |
options.put(CompilerOptions.OPTION_SuppressOptionalErrors, CompilerOptions.ENABLED); |
3657 |
this.runNegativeTest( |
3658 |
new String[] { |
3659 |
"X.java", |
3660 |
"import java.io.File;\n" + |
3661 |
"import java.io.FileReader;\n" + |
3662 |
"import java.io.IOException;\n" + |
3663 |
"public class X {\n" + |
3664 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
3665 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
3666 |
" char[] in = new char[50];\n" + |
3667 |
" FileReader fileReader1 = new FileReader(file);\n" + |
3668 |
" fileReader1.read(in);\n" + |
3669 |
" @SuppressWarnings(\"resource\") FileReader fileReader2 = new FileReader(file); // useful suppress\n" + |
3670 |
" fileReader2.read(in);\n" + |
3671 |
" if (flag1) {\n" + |
3672 |
" fileReader2.close();\n" + |
3673 |
" return; // not suppressed\n" + |
3674 |
" } else if (flag2) {\n" + |
3675 |
" fileReader2.close();\n" + |
3676 |
" }\n" + |
3677 |
" fileReader1.close();\n" + |
3678 |
" }\n" + |
3679 |
" @SuppressWarnings(\"resource\") // useful suppress\n" + |
3680 |
" void bar() throws IOException {\n" + |
3681 |
" File file = new File(\"somefile\");\n" + |
3682 |
" FileReader fileReader = new FileReader(file);\n" + |
3683 |
" char[] in = new char[50];\n" + |
3684 |
" fileReader.read(in);\n" + |
3685 |
" }\n" + |
3686 |
" public static void main(String[] args) throws IOException {\n" + |
3687 |
" new X().foo(false, true);\n" + |
3688 |
" }\n" + |
3689 |
"}\n" |
3690 |
}, |
3691 |
"----------\n" + |
3692 |
"1. WARNING in X.java (at line 6)\n" + |
3693 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
3694 |
" ^^^^^^^^^^\n" + |
3695 |
"Unnecessary @SuppressWarnings(\"resource\")\n" + |
3696 |
"----------\n" + |
3697 |
"2. ERROR in X.java (at line 14)\n" + |
3698 |
" return; // not suppressed\n" + |
3699 |
" ^^^^^^^\n" + |
3700 |
"Resource leak: \'fileReader1\' is not closed at this location\n" + |
3701 |
"----------\n", |
3702 |
null, |
3703 |
true, |
3704 |
options); |
3705 |
} |
3706 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3707 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3708 |
public void test056e() { |
3709 |
Map options = getCompilerOptions(); |
3710 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3711 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3712 |
this.runNegativeTest( |
3713 |
new String[] { |
3714 |
"X.java", |
3715 |
"import java.io.File;\n" + |
3716 |
"import java.io.FileReader;\n" + |
3717 |
"import java.io.IOException;\n" + |
3718 |
"public class X {\n" + |
3719 |
" FileReader getReader(String filename) throws IOException {\n" + |
3720 |
" File file = new File(\"somefile\");\n" + |
3721 |
" FileReader fileReader = new FileReader(file);\n" + |
3722 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
3723 |
" }\n" + |
3724 |
" void foo() throws IOException {\n" + |
3725 |
" FileReader reader = getReader(\"somefile\");\n" + |
3726 |
" char[] in = new char[50];\n" + |
3727 |
" reader.read(in);\n" + |
3728 |
" }\n" + |
3729 |
" public static void main(String[] args) throws IOException {\n" + |
3730 |
" new X().foo();\n" + |
3731 |
" }\n" + |
3732 |
"}\n" |
3733 |
}, |
3734 |
"----------\n" + |
3735 |
"1. ERROR in X.java (at line 11)\n" + |
3736 |
" FileReader reader = getReader(\"somefile\");\n" + |
3737 |
" ^^^^^^\n" + |
3738 |
"Resource leak: 'reader' is never closed\n" + |
3739 |
"----------\n", |
3740 |
null, |
3741 |
true, |
3742 |
options); |
3743 |
} |
3744 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3745 |
// a method explicitly closes its AutoCloseable rather than using t-w-r |
3746 |
public void test056f() { |
3747 |
Map options = getCompilerOptions(); |
3748 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3749 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3750 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3751 |
this.runNegativeTest( |
3752 |
new String[] { |
3753 |
"X.java", |
3754 |
"import java.io.File;\n" + |
3755 |
"import java.io.FileReader;\n" + |
3756 |
"import java.io.IOException;\n" + |
3757 |
"public class X {\n" + |
3758 |
" void foo() throws IOException {\n" + |
3759 |
" File file = new File(\"somefile\");\n" + |
3760 |
" FileReader fileReader = null;\n" + |
3761 |
" try {\n" + |
3762 |
" fileReader = new FileReader(file);\n" + |
3763 |
" char[] in = new char[50];\n" + |
3764 |
" fileReader.read(in);\n" + |
3765 |
" } finally {\n" + |
3766 |
" fileReader.close();\n" + |
3767 |
" }\n" + |
3768 |
" }\n" + |
3769 |
" public static void main(String[] args) throws IOException {\n" + |
3770 |
" new X().foo();\n" + |
3771 |
" }\n" + |
3772 |
"}\n" |
3773 |
}, |
3774 |
"----------\n" + |
3775 |
"1. ERROR in X.java (at line 7)\n" + |
3776 |
" FileReader fileReader = null;\n" + |
3777 |
" ^^^^^^^^^^\n" + |
3778 |
"Resource \'fileReader\' should be managed by try-with-resource\n" + |
3779 |
"----------\n", |
3780 |
null, |
3781 |
true, |
3782 |
options); |
3783 |
} |
3784 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3785 |
// an AutoCloseable local is re-assigned |
3786 |
public void test056g() { |
3787 |
Map options = getCompilerOptions(); |
3788 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3789 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3790 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3791 |
this.runNegativeTest( |
3792 |
new String[] { |
3793 |
"X.java", |
3794 |
"import java.io.File;\n" + |
3795 |
"import java.io.FileReader;\n" + |
3796 |
"import java.io.IOException;\n" + |
3797 |
"public class X {\n" + |
3798 |
" void foo() throws IOException {\n" + |
3799 |
" File file = new File(\"somefile\");\n" + |
3800 |
" FileReader fileReader = new FileReader(file);\n" + |
3801 |
" char[] in = new char[50];\n" + |
3802 |
" fileReader.read(in);\n" + |
3803 |
" fileReader = new FileReader(file);\n" + |
3804 |
" fileReader.read(in);\n" + |
3805 |
" fileReader.close();\n" + |
3806 |
" fileReader = null;\n" + |
3807 |
" }\n" + |
3808 |
" public static void main(String[] args) throws IOException {\n" + |
3809 |
" new X().foo();\n" + |
3810 |
" }\n" + |
3811 |
"}\n" |
3812 |
}, |
3813 |
"----------\n" + |
3814 |
"1. ERROR in X.java (at line 10)\n" + |
3815 |
" fileReader = new FileReader(file);\n" + |
3816 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3817 |
"Resource leak: \'fileReader\' is not closed at this location\n" + |
3818 |
"----------\n", |
3819 |
null, |
3820 |
true, |
3821 |
options); |
3822 |
} |
3823 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3824 |
// an AutoCloseable local is re-assigned after null-assigned |
3825 |
public void test056g2() { |
3826 |
Map options = getCompilerOptions(); |
3827 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3828 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3829 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3830 |
this.runNegativeTest( |
3831 |
new String[] { |
3832 |
"X.java", |
3833 |
"import java.io.File;\n" + |
3834 |
"import java.io.FileReader;\n" + |
3835 |
"import java.io.IOException;\n" + |
3836 |
"public class X {\n" + |
3837 |
" void foo() throws IOException {\n" + |
3838 |
" File file = new File(\"somefile\");\n" + |
3839 |
" FileReader fileReader = new FileReader(file);\n" + |
3840 |
" char[] in = new char[50];\n" + |
3841 |
" fileReader.read(in);\n" + |
3842 |
" fileReader = null;\n" + |
3843 |
" fileReader = new FileReader(file);\n" + // don't complain again, fileReader is null, so nothing can leak here |
3844 |
" fileReader.read(in);\n" + |
3845 |
" fileReader.close();\n" + |
3846 |
" }\n" + |
3847 |
" public static void main(String[] args) throws IOException {\n" + |
3848 |
" new X().foo();\n" + |
3849 |
" }\n" + |
3850 |
"}\n" |
3851 |
}, |
3852 |
"----------\n" + |
3853 |
"1. ERROR in X.java (at line 10)\n" + |
3854 |
" fileReader = null;\n" + |
3855 |
" ^^^^^^^^^^^^^^^^^\n" + |
3856 |
"Resource leak: \'fileReader\' is not closed at this location\n" + |
3857 |
"----------\n", |
3858 |
null, |
3859 |
true, |
3860 |
options); |
3861 |
} |
3862 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3863 |
// two AutoCloseables at different nesting levels (anonymous local type) |
3864 |
public void test056h() { |
3865 |
Map options = getCompilerOptions(); |
3866 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3867 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3868 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3869 |
this.runNegativeTest( |
3870 |
new String[] { |
3871 |
"X.java", |
3872 |
"import java.io.File;\n" + |
3873 |
"import java.io.FileReader;\n" + |
3874 |
"import java.io.IOException;\n" + |
3875 |
"public class X {\n" + |
3876 |
" void foo() throws IOException {\n" + |
3877 |
" final File file = new File(\"somefile\");\n" + |
3878 |
" final FileReader fileReader = new FileReader(file);\n" + |
3879 |
" char[] in = new char[50];\n" + |
3880 |
" fileReader.read(in);\n" + |
3881 |
" new Runnable() {\n public void run() {\n" + |
3882 |
" try {\n" + |
3883 |
" fileReader.close();\n" + |
3884 |
" FileReader localReader = new FileReader(file);\n" + |
3885 |
" } catch (IOException ex) { /* nop */ }\n" + |
3886 |
" }}.run();\n" + |
3887 |
" }\n" + |
3888 |
" public static void main(String[] args) throws IOException {\n" + |
3889 |
" new X().foo();\n" + |
3890 |
" }\n" + |
3891 |
"}\n" |
3892 |
}, |
3893 |
"----------\n" + |
3894 |
"1. WARNING in X.java (at line 7)\n" + |
3895 |
" final FileReader fileReader = new FileReader(file);\n" + |
3896 |
" ^^^^^^^^^^\n" + |
3897 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
3898 |
"----------\n" + |
3899 |
"2. ERROR in X.java (at line 14)\n" + |
3900 |
" FileReader localReader = new FileReader(file);\n" + |
3901 |
" ^^^^^^^^^^^\n" + |
3902 |
"Resource leak: 'localReader' is never closed\n" + |
3903 |
"----------\n", |
3904 |
null, |
3905 |
true, |
3906 |
options); |
3907 |
} |
3908 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3909 |
// three AutoCloseables in different blocks of the same method |
3910 |
public void test056i() { |
3911 |
Map options = getCompilerOptions(); |
3912 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3913 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3914 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3915 |
this.runNegativeTest( |
3916 |
new String[] { |
3917 |
"X.java", |
3918 |
"import java.io.File;\n" + |
3919 |
"import java.io.FileReader;\n" + |
3920 |
"import java.io.IOException;\n" + |
3921 |
"public class X {\n" + |
3922 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
3923 |
" File file = new File(\"somefile\");\n" + |
3924 |
" if (f1) {\n" + |
3925 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
3926 |
" char[] in = new char[50];\n" + |
3927 |
" fileReader.read(in);\n" + |
3928 |
" while (true) {\n" + |
3929 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
3930 |
" loopReader.close();" + |
3931 |
" break;\n" + |
3932 |
" }\n" + |
3933 |
" } else {\n" + |
3934 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3935 |
" if (f2)\n" + |
3936 |
" fileReader.close();\n" + |
3937 |
" }\n" + |
3938 |
" }\n" + |
3939 |
" public static void main(String[] args) throws IOException {\n" + |
3940 |
" new X().foo(true, true);\n" + |
3941 |
" }\n" + |
3942 |
"}\n" |
3943 |
}, |
3944 |
"----------\n" + |
3945 |
"1. ERROR in X.java (at line 8)\n" + |
3946 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
3947 |
" ^^^^^^^^^^\n" + |
3948 |
"Resource leak: 'fileReader' is never closed\n" + |
3949 |
"----------\n" + |
3950 |
"2. WARNING in X.java (at line 16)\n" + |
3951 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3952 |
" ^^^^^^^^^^\n" + |
3953 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
3954 |
"----------\n", |
3955 |
null, |
3956 |
true, |
3957 |
options); |
3958 |
} |
3959 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3960 |
// three AutoCloseables in different blocks of the same method |
3961 |
public void test056i2() { |
3962 |
Map options = getCompilerOptions(); |
3963 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3964 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3965 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3966 |
this.runNegativeTest( |
3967 |
new String[] { |
3968 |
"X.java", |
3969 |
"import java.io.File;\n" + |
3970 |
"import java.io.FileReader;\n" + |
3971 |
"import java.io.IOException;\n" + |
3972 |
"public class X {\n" + |
3973 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
3974 |
" File file = new File(\"somefile\");\n" + |
3975 |
" if (f1) {\n" + |
3976 |
" FileReader fileReader = new FileReader(file); // properly closed\n" + |
3977 |
" char[] in = new char[50];\n" + |
3978 |
" fileReader.read(in);\n" + |
3979 |
" while (true) {\n" + |
3980 |
" fileReader.close();\n" + |
3981 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
3982 |
" loopReader.close();\n" + |
3983 |
" break;\n" + |
3984 |
" }\n" + |
3985 |
" } else {\n" + |
3986 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3987 |
" if (f2)\n" + |
3988 |
" fileReader.close();\n" + |
3989 |
" }\n" + |
3990 |
" }\n" + |
3991 |
" public static void main(String[] args) throws IOException {\n" + |
3992 |
" new X().foo(true, true);\n" + |
3993 |
" }\n" + |
3994 |
"}\n" |
3995 |
}, |
3996 |
"----------\n" + |
3997 |
"1. ERROR in X.java (at line 18)\n" + |
3998 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3999 |
" ^^^^^^^^^^\n" + |
4000 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
4001 |
"----------\n", |
4002 |
null, |
4003 |
true, |
4004 |
options); |
4005 |
} |
4006 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4007 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
4008 |
public void test056j() { |
4009 |
Map options = getCompilerOptions(); |
4010 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4011 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4012 |
this.runNegativeTest( |
4013 |
new String[] { |
4014 |
"X.java", |
4015 |
"import java.io.File;\n" + |
4016 |
"import java.io.FileReader;\n" + |
4017 |
"import java.io.IOException;\n" + |
4018 |
"public class X {\n" + |
4019 |
" void foo() throws IOException {\n" + |
4020 |
" File file = new File(\"somefile\");\n" + |
4021 |
" FileReader fileReader = new FileReader(file);\n" + |
4022 |
" read(fileReader);\n" + |
4023 |
" }\n" + |
4024 |
" void read(FileReader reader) { }\n" + |
4025 |
" public static void main(String[] args) throws IOException {\n" + |
4026 |
" new X().foo();\n" + |
4027 |
" }\n" + |
4028 |
"}\n" |
4029 |
}, |
4030 |
"----------\n" + |
4031 |
"1. ERROR in X.java (at line 7)\n" + |
4032 |
" FileReader fileReader = new FileReader(file);\n" + |
4033 |
" ^^^^^^^^^^\n" + |
4034 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
4035 |
"----------\n", |
4036 |
null, |
4037 |
true, |
4038 |
options); |
4039 |
} |
4040 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4041 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
4042 |
public void test056jconditional() { |
4043 |
Map options = getCompilerOptions(); |
4044 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4045 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4046 |
this.runNegativeTest( |
4047 |
new String[] { |
4048 |
"X.java", |
4049 |
"import java.io.File;\n" + |
4050 |
"import java.io.FileReader;\n" + |
4051 |
"import java.io.IOException;\n" + |
4052 |
"public class X {\n" + |
4053 |
" void foo(boolean b) throws IOException {\n" + |
4054 |
" File file = new File(\"somefile\");\n" + |
4055 |
" FileReader fileReader = new FileReader(file);\n" + |
4056 |
" synchronized (b ? this : new X()) {\n" + |
4057 |
" new ReadDelegator(fileReader);\n" + |
4058 |
" }\n" + |
4059 |
" }\n" + |
4060 |
" class ReadDelegator { ReadDelegator(FileReader reader) { } }\n" + |
4061 |
" public static void main(String[] args) throws IOException {\n" + |
4062 |
" new X().foo(true);\n" + |
4063 |
" }\n" + |
4064 |
"}\n" |
4065 |
}, |
4066 |
"----------\n" + |
4067 |
"1. ERROR in X.java (at line 7)\n" + |
4068 |
" FileReader fileReader = new FileReader(file);\n" + |
4069 |
" ^^^^^^^^^^\n" + |
4070 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
4071 |
"----------\n", |
4072 |
null, |
4073 |
true, |
4074 |
options); |
4075 |
} |
4076 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4077 |
// many locals, some are AutoCloseable. |
4078 |
// Unfortunately analysis cannot respect how exception exits may affect ra3 and rb3, |
4079 |
// doing so would create false positives. |
4080 |
public void test056k() { |
4081 |
Map options = getCompilerOptions(); |
4082 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4083 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
4084 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
4085 |
this.runNegativeTest( |
4086 |
new String[] { |
4087 |
"X.java", |
4088 |
"import java.io.File;\n" + |
4089 |
"import java.io.FileReader;\n" + |
4090 |
"import java.io.IOException;\n" + |
4091 |
"public class X {\n" + |
4092 |
" void foo() throws IOException {\n" + |
4093 |
" int i01, i02, i03, i04, i05, i06, i07, i08, i09,\n" + |
4094 |
" i11, i12, i13, i14, i15, i16, i17, i18, i19,\n" + |
4095 |
" i21, i22, i23, i24, i25, i26, i27, i28, i29,\n" + |
4096 |
" i31, i32, i33, i34, i35, i36, i37, i38, i39,\n" + |
4097 |
" i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" + |
4098 |
" File file = new File(\"somefile\");\n" + |
4099 |
" FileReader ra1 = null, ra2 = null;\n" + |
4100 |
" try {\n" + |
4101 |
" ra1 = new FileReader(file);\n" + |
4102 |
" ra2 = new FileReader(file);\n" + |
4103 |
" FileReader ra3 = new FileReader(file);\n" + |
4104 |
" char[] in = new char[50];\n" + |
4105 |
" ra1.read(in);\n" + |
4106 |
" ra2.read(in);\n" + |
4107 |
" ra3.close();\n" + |
4108 |
" } finally {\n" + |
4109 |
" ra1.close();\n" + |
4110 |
" }\n" + |
4111 |
" int i51, i52, i53, i54, i55, i56, i57, i58, i59, i60;\n" + // beyond this point locals are analyzed using extraBits |
4112 |
" FileReader rb1 = null, rb2 = null;\n" + |
4113 |
" try {\n" + |
4114 |
" rb1 = new FileReader(file);\n" + |
4115 |
" rb2 = new FileReader(file);\n" + |
4116 |
" FileReader rb3 = new FileReader(file);\n" + |
4117 |
" char[] in = new char[50];\n" + |
4118 |
" rb1.read(in);\n" + |
4119 |
" rb2.read(in);\n" + |
4120 |
" rb3.close();\n" + |
4121 |
" } finally {\n" + |
4122 |
" rb1.close();\n" + |
4123 |
" }\n" + |
4124 |
" }\n" + |
4125 |
" public static void main(String[] args) throws IOException {\n" + |
4126 |
" new X().foo();\n" + |
4127 |
" }\n" + |
4128 |
"}\n" |
4129 |
}, |
4130 |
"----------\n" + |
4131 |
"1. ERROR in X.java (at line 12)\n" + |
4132 |
" FileReader ra1 = null, ra2 = null;\n" + |
4133 |
" ^^^\n" + |
4134 |
"Resource \'ra1\' should be managed by try-with-resource\n" + |
4135 |
"----------\n" + |
4136 |
"2. ERROR in X.java (at line 15)\n" + |
4137 |
" ra2 = new FileReader(file);\n" + |
4138 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4139 |
"Resource leak: 'ra2' is never closed\n" + |
4140 |
"----------\n" + |
4141 |
"3. ERROR in X.java (at line 16)\n" + |
4142 |
" FileReader ra3 = new FileReader(file);\n" + |
4143 |
" ^^^\n" + |
4144 |
"Resource \'ra3\' should be managed by try-with-resource\n" + |
4145 |
"----------\n" + |
4146 |
"4. ERROR in X.java (at line 25)\n" + |
4147 |
" FileReader rb1 = null, rb2 = null;\n" + |
4148 |
" ^^^\n" + |
4149 |
"Resource \'rb1\' should be managed by try-with-resource\n" + |
4150 |
"----------\n" + |
4151 |
"5. ERROR in X.java (at line 28)\n" + |
4152 |
" rb2 = new FileReader(file);\n" + |
4153 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4154 |
"Resource leak: 'rb2' is never closed\n" + |
4155 |
"----------\n" + |
4156 |
"6. ERROR in X.java (at line 29)\n" + |
4157 |
" FileReader rb3 = new FileReader(file);\n" + |
4158 |
" ^^^\n" + |
4159 |
"Resource \'rb3\' should be managed by try-with-resource\n" + |
4160 |
"----------\n", |
4161 |
null, |
4162 |
true, |
4163 |
options); |
4164 |
} |
4165 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4166 |
// various non-problems |
4167 |
public void test056l() { |
4168 |
Map options = getCompilerOptions(); |
4169 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4170 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4171 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
4172 |
this.runNegativeTest( |
4173 |
new String[] { |
4174 |
"X.java", |
4175 |
"import java.io.File;\n" + |
4176 |
"import java.io.FileReader;\n" + |
4177 |
"import java.io.IOException;\n" + |
4178 |
"public class X {\n" + |
4179 |
" X(FileReader r0) {}\n" + // don't complain against argument |
4180 |
" FileReader getReader() { return null; }\n" + |
4181 |
" void foo(FileReader r1) throws IOException {\n" + |
4182 |
" FileReader fileReader = getReader();\n" + |
4183 |
" if (fileReader == null)\n" + |
4184 |
" return;\n" + // don't complain, resource is actually null |
4185 |
" FileReader r3 = getReader();\n" + |
4186 |
" if (r3 == null)\n" + |
4187 |
" r3 = new FileReader(new File(\"absent\"));\n" + // don't complain, previous resource is actually null |
4188 |
" try {\n" + |
4189 |
" char[] in = new char[50];\n" + |
4190 |
" fileReader.read(in);\n" + |
4191 |
" r1.read(in);\n" + |
4192 |
" } finally {\n" + |
4193 |
" fileReader.close();\n" + |
4194 |
" 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 |
4195 |
" }\n" + |
4196 |
" }\n" + |
4197 |
" public static void main(String[] args) throws IOException {\n" + |
4198 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
4199 |
" new X(r2).foo(new FileReader(new File(\"notthere\")));\n" + |
4200 |
" }\n" + |
4201 |
"}\n" |
4202 |
}, |
4203 |
"----------\n" + |
4204 |
"1. ERROR in X.java (at line 8)\n" + |
4205 |
" FileReader fileReader = getReader();\n" + |
4206 |
" ^^^^^^^^^^\n" + |
4207 |
"Resource \'fileReader\' should be managed by try-with-resource\n" + |
4208 |
"----------\n" + |
4209 |
"2. ERROR in X.java (at line 11)\n" + |
4210 |
" FileReader r3 = getReader();\n" + |
4211 |
" ^^\n" + |
4212 |
"Resource 'r3' should be managed by try-with-resource\n" + |
4213 |
"----------\n" + |
4214 |
"3. ERROR in X.java (at line 24)\n" + |
4215 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
4216 |
" ^^\n" + |
4217 |
"Potential resource leak: 'r2' may not be closed\n" + |
4218 |
"----------\n", |
4219 |
null, |
4220 |
true, |
4221 |
options); |
4222 |
} |
4223 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4224 |
// nested try with early exit |
4225 |
public void test056m() { |
4226 |
Map options = getCompilerOptions(); |
4227 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4228 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4229 |
this.runConformTest( |
4230 |
new String[] { |
4231 |
"X.java", |
4232 |
"import java.io.File;\n" + |
4233 |
"import java.io.FileReader;\n" + |
4234 |
"import java.io.IOException;\n" + |
4235 |
"public class X {\n" + |
4236 |
" void foo() {\n" + |
4237 |
" File file = new File(\"somefile\");" + |
4238 |
" try {\n" + |
4239 |
" FileReader fileReader = new FileReader(file);\n" + |
4240 |
" try {\n" + |
4241 |
" char[] in = new char[50];\n" + |
4242 |
" if (fileReader.read(in)==0)\n" + |
4243 |
" return;\n" + |
4244 |
" } finally {\n" + |
4245 |
" fileReader.close();\n" + |
4246 |
" }\n" + |
4247 |
" } catch (IOException e) {\n" + |
4248 |
" System.out.println(\"caught\");\n" + |
4249 |
" }\n" + |
4250 |
" }\n" + |
4251 |
" public static void main(String[] args) {\n" + |
4252 |
" new X().foo();\n" + |
4253 |
" }\n" + |
4254 |
"}\n" |
4255 |
}, |
4256 |
"caught", /*output*/ |
4257 |
null/*classLibs*/, |
4258 |
true/*shouldFlush*/, |
4259 |
null/*vmargs*/, |
4260 |
options, |
4261 |
null/*requestor*/); |
4262 |
} |
4263 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4264 |
// nested try should not interfere with earlier analysis. |
4265 |
public void test056n() { |
4266 |
Map options = getCompilerOptions(); |
4267 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4268 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4269 |
this.runConformTest( |
4270 |
new String[] { |
4271 |
"X.java", |
4272 |
"import java.io.File;\n" + |
4273 |
"import java.io.FileReader;\n" + |
4274 |
"import java.io.IOException;\n" + |
4275 |
"import java.io.FileNotFoundException;\n" + |
4276 |
"public class X {\n" + |
4277 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
4278 |
" FileReader fr1 = new FileReader(someFile);\n" + |
4279 |
" try {\n" + |
4280 |
" fr1.read(buf);\n" + |
4281 |
" } finally {\n" + |
4282 |
" fr1.close();\n" + |
4283 |
" }\n" + |
4284 |
" try {\n" + |
4285 |
" FileReader fr3 = new FileReader(someFile);\n" + |
4286 |
" try {\n" + |
4287 |
" } finally {\n" + |
4288 |
" fr3.close();\n" + |
4289 |
" }\n" + |
4290 |
" } catch (IOException e) {\n" + |
4291 |
" }\n" + |
4292 |
" }\n" + |
4293 |
" public static void main(String[] args) throws IOException {\n" + |
4294 |
" try {\n" + |
4295 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
4296 |
" } catch (FileNotFoundException e) {\n" + |
4297 |
" System.out.println(\"caught\");\n" + |
4298 |
" }\n" + |
4299 |
" }\n" + |
4300 |
"}\n" |
4301 |
}, |
4302 |
"caught", /*output*/ |
4303 |
null/*classLibs*/, |
4304 |
true/*shouldFlush*/, |
4305 |
null/*vmargs*/, |
4306 |
options, |
4307 |
null/*requestor*/); |
4308 |
} |
4309 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4310 |
// if close is guarded by null check this should still be recognized as definitely closed |
4311 |
public void test056o() { |
4312 |
Map options = getCompilerOptions(); |
4313 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4314 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4315 |
this.runConformTest( |
4316 |
new String[] { |
4317 |
"X.java", |
4318 |
"import java.io.File;\n" + |
4319 |
"import java.io.FileReader;\n" + |
4320 |
"import java.io.IOException;\n" + |
4321 |
"import java.io.FileNotFoundException;\n" + |
4322 |
"public class X {\n" + |
4323 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
4324 |
" FileReader fr1 = null;\n" + |
4325 |
" try {\n" + |
4326 |
" fr1 = new FileReader(someFile);" + |
4327 |
" fr1.read(buf);\n" + |
4328 |
" } finally {\n" + |
4329 |
" if (fr1 != null)\n" + |
4330 |
" try {\n" + |
4331 |
" fr1.close();\n" + |
4332 |
" } catch (IOException e) { /*do nothing*/ }\n" + |
4333 |
" }\n" + |
4334 |
" }\n" + |
4335 |
" public static void main(String[] args) throws IOException {\n" + |
4336 |
" try {\n" + |
4337 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
4338 |
" } catch (FileNotFoundException e) {\n" + |
4339 |
" System.out.println(\"caught\");\n" + |
4340 |
" }\n" + |
4341 |
" }\n" + |
4342 |
"}\n" |
4343 |
}, |
4344 |
"caught", /*output*/ |
4345 |
null/*classLibs*/, |
4346 |
true/*shouldFlush*/, |
4347 |
null/*vmargs*/, |
4348 |
options, |
4349 |
null/*requestor*/); |
4350 |
} |
4351 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4352 |
// a method uses an AutoCloseable without ever closing it, type from a type variable |
4353 |
public void test056p() { |
4354 |
Map options = getCompilerOptions(); |
4355 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4356 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
4357 |
this.runNegativeTest( |
4358 |
new String[] { |
4359 |
"X.java", |
4360 |
"import java.io.File;\n" + |
4361 |
"import java.io.FileReader;\n" + |
4362 |
"import java.io.Reader;\n" + |
4363 |
"import java.io.IOException;\n" + |
4364 |
"public abstract class X <T extends Reader> {\n" + |
4365 |
" void foo() throws IOException {\n" + |
4366 |
" File file = new File(\"somefile\");\n" + |
4367 |
" T fileReader = newReader(file);\n" + |
4368 |
" char[] in = new char[50];\n" + |
4369 |
" fileReader.read(in);\n" + |
4370 |
" }\n" + |
4371 |
" abstract T newReader(File file) throws IOException;\n" + |
4372 |
" public static void main(String[] args) throws IOException {\n" + |
4373 |
" new X<FileReader>() {\n" + |
4374 |
" FileReader newReader(File f) throws IOException { return new FileReader(f); }\n" + |
4375 |
" }.foo();\n" + |
4376 |
" }\n" + |
4377 |
"}\n" |
4378 |
}, |
4379 |
"----------\n" + |
4380 |
"1. ERROR in X.java (at line 8)\n" + |
4381 |
" T fileReader = newReader(file);\n" + |
4382 |
" ^^^^^^^^^^\n" + |
4383 |
"Resource leak: 'fileReader' is never closed\n" + |
4384 |
"----------\n", |
4385 |
null, |
4386 |
true, |
4387 |
options); |
4388 |
} |
4389 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4390 |
// closed in dead code |
4391 |
public void test056q() { |
4392 |
Map options = getCompilerOptions(); |
4393 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4394 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4395 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
4396 |
this.runNegativeTest( |
4397 |
new String[] { |
4398 |
"X.java", |
4399 |
"import java.io.File;\n" + |
4400 |
"import java.io.FileReader;\n" + |
4401 |
"import java.io.IOException;\n" + |
4402 |
"public class X {\n" + |
4403 |
" void foo() throws IOException {\n" + |
4404 |
" File file = new File(\"somefile\");\n" + |
4405 |
" FileReader fileReader = new FileReader(file);\n" + |
4406 |
" char[] in = new char[50];\n" + |
4407 |
" fileReader.read(in);\n" + |
4408 |
" if (2*2 == 4)\n" + |
4409 |
" return;\n" + |
4410 |
" fileReader.close();\n" + |
4411 |
" }\n" + |
4412 |
" public static void main(String[] args) throws IOException {\n" + |
4413 |
" new X().foo();\n" + |
4414 |
" }\n" + |
4415 |
"}\n" |
4416 |
}, |
4417 |
"----------\n" + |
4418 |
"1. ERROR in X.java (at line 7)\n" + |
4419 |
" FileReader fileReader = new FileReader(file);\n" + |
4420 |
" ^^^^^^^^^^\n" + |
4421 |
"Resource leak: \'fileReader\' is never closed\n" + |
4422 |
"----------\n" + |
4423 |
"2. WARNING in X.java (at line 10)\n" + |
4424 |
" if (2*2 == 4)\n" + |
4425 |
" ^^^^^^^^\n" + |
4426 |
"Comparing identical expressions\n" + |
4427 |
"----------\n" + |
4428 |
"3. WARNING in X.java (at line 12)\n" + |
4429 |
" fileReader.close();\n" + |
4430 |
" ^^^^^^^^^^^^^^^^^^\n" + |
4431 |
"Dead code\n" + |
4432 |
"----------\n", |
4433 |
null, |
4434 |
true, |
4435 |
options); |
4436 |
} |
4437 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4438 |
// closed in dead code |
4439 |
public void test056r() { |
4440 |
Map options = getCompilerOptions(); |
4441 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4442 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4443 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
4444 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4445 |
this.runNegativeTest( |
4446 |
new String[] { |
4447 |
"X.java", |
4448 |
"import java.io.File;\n" + |
4449 |
"import java.io.FileReader;\n" + |
4450 |
"import java.io.IOException;\n" + |
4451 |
"public class X {\n" + |
4452 |
" void foo() throws IOException {\n" + |
4453 |
" File file = new File(\"somefile\");\n" + |
4454 |
" FileReader fr = new FileReader(file);\n" + |
4455 |
" Object b = null;\n" + |
4456 |
" fr.close();\n" + |
4457 |
" if (b != null) {\n" + |
4458 |
" fr = new FileReader(file);\n" + |
4459 |
" return;\n" + |
4460 |
" } else {\n" + |
4461 |
" System.out.print(42);\n" + |
4462 |
" }\n" + |
4463 |
" return; // Should not complain about fr\n" + |
4464 |
" }\n" + |
4465 |
" public static void main(String[] args) throws IOException {\n" + |
4466 |
" new X().foo();\n" + |
4467 |
" }\n" + |
4468 |
"}\n" |
4469 |
}, |
4470 |
"----------\n" + |
4471 |
"1. ERROR in X.java (at line 10)\n" + |
4472 |
" if (b != null) {\n" + |
4473 |
" fr = new FileReader(file);\n" + |
4474 |
" return;\n" + |
4475 |
" } else {\n" + |
4476 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4477 |
"Dead code\n" + |
4478 |
"----------\n" + |
4479 |
"2. WARNING in X.java (at line 13)\n" + |
4480 |
" } else {\n" + |
4481 |
" System.out.print(42);\n" + |
4482 |
" }\n" + |
4483 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4484 |
"Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally\n" + |
4485 |
"----------\n", |
4486 |
null, |
4487 |
true, |
4488 |
options); |
4489 |
} |
4490 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4491 |
// resource inside t-w-r is re-assigned, shouldn't even record an errorLocation |
4492 |
public void test056s() { |
4493 |
Map options = getCompilerOptions(); |
4494 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4495 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4496 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
4497 |
this.runNegativeTest( |
4498 |
new String[] { |
4499 |
"X.java", |
4500 |
"import java.io.File;\n" + |
4501 |
"import java.io.FileReader;\n" + |
4502 |
"import java.io.IOException;\n" + |
4503 |
"public class X {\n" + |
4504 |
" void foo() throws IOException {\n" + |
4505 |
" File file = new File(\"somefile\");\n" + |
4506 |
" try (FileReader fileReader = new FileReader(file);) {\n" + |
4507 |
" char[] in = new char[50];\n" + |
4508 |
" fileReader.read(in);\n" + |
4509 |
" fileReader = new FileReader(file); // debug here\n" + |
4510 |
" fileReader.read(in);\n" + |
4511 |
" }\n" + |
4512 |
" }\n" + |
4513 |
" public static void main(String[] args) throws IOException {\n" + |
4514 |
" new X().foo();\n" + |
4515 |
" }\n" + |
4516 |
"}\n" |
4517 |
}, |
4518 |
"----------\n" + |
4519 |
"1. ERROR in X.java (at line 10)\n" + |
4520 |
" fileReader = new FileReader(file); // debug here\n" + |
4521 |
" ^^^^^^^^^^\n" + |
4522 |
"The resource fileReader of a try-with-resources statement cannot be assigned\n" + |
4523 |
"----------\n", |
4524 |
null, |
4525 |
true, |
4526 |
options); |
4527 |
} |
4528 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4529 |
// resource is closed, dead code follows |
4530 |
public void test056t() { |
4531 |
Map options = getCompilerOptions(); |
4532 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4533 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4534 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4535 |
this.runNegativeTest( |
4536 |
new String[] { |
4537 |
"X.java", |
4538 |
"import java.io.FileReader;\n" + |
4539 |
"import java.io.IOException;\n" + |
4540 |
"public class X {\n" + |
4541 |
" void foo31() throws IOException {\n" + |
4542 |
" FileReader reader = new FileReader(\"file\"); //warning\n" + |
4543 |
" if (reader != null) {\n" + |
4544 |
" reader.close();\n" + |
4545 |
" } else {\n" + |
4546 |
" // nop\n" + |
4547 |
" }\n" + |
4548 |
" }\n" + |
4549 |
" public static void main(String[] args) throws IOException {\n" + |
4550 |
" new X().foo31();\n" + |
4551 |
" }\n" + |
4552 |
"}\n" |
4553 |
}, |
4554 |
"----------\n" + |
4555 |
"1. ERROR in X.java (at line 8)\n" + |
4556 |
" } else {\n" + |
4557 |
" // nop\n" + |
4558 |
" }\n" + |
4559 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4560 |
"Dead code\n" + |
4561 |
"----------\n", |
4562 |
null, |
4563 |
true, |
4564 |
options); |
4565 |
} |
4566 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4567 |
// resource is reassigned within t-w-r with different resource |
4568 |
// disabled due to Bug 358827 - [1.7] exception analysis for t-w-r spoils null analysis |
4569 |
public void _test056u() { |
4570 |
Map options = getCompilerOptions(); |
4571 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4572 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4573 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4574 |
this.runNegativeTest( |
4575 |
new String[] { |
4576 |
"X.java", |
4577 |
"import java.io.FileReader;\n" + |
4578 |
"public class X {\n" + |
4579 |
" void foo() throws Exception {\n" + |
4580 |
" FileReader reader1 = new FileReader(\"file1\");\n" + |
4581 |
" FileReader reader2 = new FileReader(\"file2\");\n" + |
4582 |
" reader2 = reader1;// warning 1\n" + |
4583 |
" try (FileReader reader3 = new FileReader(\"file3\")) {\n" + |
4584 |
" int ch;\n" + |
4585 |
" while ((ch = reader2.read()) != -1) {\n" + |
4586 |
" System.out.println(ch);\n" + |
4587 |
" reader1.read();\n" + |
4588 |
" }\n" + |
4589 |
" reader2 = reader1;// warning 2\n" + |
4590 |
" reader2 = reader1;// warning 3\n" + |
4591 |
" } finally {\n" + |
4592 |
" if (reader2 != null) {\n" + |
4593 |
" reader2.close();\n" + |
4594 |
" } else {\n" + |
4595 |
" System.out.println();\n" + |
4596 |
" }\n" + |
4597 |
" }\n" + |
4598 |
" }\n" + |
4599 |
"}\n" |
4600 |
}, |
4601 |
"----------\n" + |
4602 |
"1. ERROR in X.java (at line 4)\n" + |
4603 |
" FileReader reader1 = new FileReader(\"file1\");\n" + |
4604 |
" ^^^^^^^\n" + |
4605 |
"Resource leak: \'reader1\' is never closed\n" + |
4606 |
"----------\n" + |
4607 |
"2. ERROR in X.java (at line 4)\n" + |
4608 |
" reader2 = reader1;// warning 1\n" + |
4609 |
" ^^^^^^^^^^^^^^^^^\n" + |
4610 |
"Resource leak: \'reader2\' is not closed at this location\n" + |
4611 |
"----------\n" + |
4612 |
"3. ERROR in X.java (at line 13)\n" + |
4613 |
" reader2 = reader1;// warning 2\n" + |
4614 |
" ^^^^^^^^^^^^^^^^^\n" + |
4615 |
"Resource leak: \'reader2\' is not closed at this location\n" + |
4616 |
"----------\n" + |
4617 |
"4. ERROR in X.java (at line 14)\n" + |
4618 |
" reader2 = reader1;// warning 3\n" + |
4619 |
" ^^^^^^^^^^^^^^^^^\n" + |
4620 |
"Resource leak: \'reader2\' is not closed at this location\n" + |
4621 |
"----------\n", |
4622 |
null, |
4623 |
true, |
4624 |
options); |
4625 |
} |
4626 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4627 |
// pb reported in https://bugs.eclipse.org/bugs/show_bug.cgi?id=349326#c70 |
4628 |
public void test056v() { |
4629 |
Map options = getCompilerOptions(); |
4630 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4631 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4632 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4633 |
this.runNegativeTest( |
4634 |
new String[] { |
4635 |
"X.java", |
4636 |
"import java.io.FileReader;\n" + |
4637 |
"public class X {\n" + |
4638 |
" boolean foo1() throws Exception {\n" + |
4639 |
" FileReader reader = new FileReader(\"file\");\n" + |
4640 |
" try {\n" + |
4641 |
" int ch;\n" + |
4642 |
" while ((ch = reader.read()) != -1) {\n" + |
4643 |
" System.out.println(ch);\n" + |
4644 |
" reader.read();\n" + |
4645 |
" }\n" + |
4646 |
" if (ch > 10) {\n" + |
4647 |
" return true;\n" + |
4648 |
" }\n" + |
4649 |
" return false;\n" + // return while resource from enclosing scope remains unclosed |
4650 |
" } finally {\n" + |
4651 |
" }\n" + |
4652 |
" }\n" + |
4653 |
" void foo2() throws Exception {\n" + |
4654 |
" FileReader reader2 = new FileReader(\"file\");\n" + |
4655 |
" try {\n" + |
4656 |
" int ch;\n" + |
4657 |
" while ((ch = reader2.read()) != -1) {\n" + |
4658 |
" System.out.println(ch);\n" + |
4659 |
" reader2.read();\n" + |
4660 |
" }\n" + |
4661 |
" if (ch > 10) {\n" + |
4662 |
" return;\n" + // potential leak |
4663 |
" }\n" + |
4664 |
" } finally {\n" + |
4665 |
" }\n" + |
4666 |
" reader2.close();\n" + // due to this close we don't say "never closed" |
4667 |
" }\n" + |
4668 |
"}\n" |
4669 |
}, |
4670 |
"----------\n" + |
4671 |
"1. ERROR in X.java (at line 4)\n" + |
4672 |
" FileReader reader = new FileReader(\"file\");\n" + |
4673 |
" ^^^^^^\n" + |
4674 |
"Resource leak: \'reader\' is never closed\n" + |
4675 |
"----------\n" + |
4676 |
"2. ERROR in X.java (at line 27)\n" + |
4677 |
" return;\n" + |
4678 |
" ^^^^^^^\n" + |
4679 |
"Resource leak: \'reader2\' is not closed at this location\n" + |
4680 |
"----------\n", |
4681 |
null, |
4682 |
true, |
4683 |
options); |
4684 |
} |
4685 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4686 |
// end of method is dead end, but before we have both a close() and an early return |
4687 |
public void test056w() { |
4688 |
Map options = getCompilerOptions(); |
4689 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4690 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4691 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4692 |
this.runNegativeTest( |
4693 |
new String[] { |
4694 |
"X.java", |
4695 |
"import java.io.FileReader;\n" + |
4696 |
"public class X {\n" + |
4697 |
" boolean foo1() throws Exception {\n" + |
4698 |
" FileReader reader = new FileReader(\"file\");\n" + |
4699 |
" try {\n" + |
4700 |
" int ch;\n" + |
4701 |
" while ((ch = reader.read()) != -1) {\n" + |
4702 |
" System.out.println(ch);\n" + |
4703 |
" reader.read();\n" + |
4704 |
" }\n" + |
4705 |
" if (ch > 10) {\n" + |
4706 |
" reader.close();\n" + |
4707 |
" return true;\n" + |
4708 |
" }\n" + |
4709 |
" return false;\n" + |
4710 |
" } finally {\n" + |
4711 |
" }\n" + |
4712 |
" }\n" + |
4713 |
"}\n" |
4714 |
}, |
4715 |
"----------\n" + |
4716 |
"1. ERROR in X.java (at line 15)\n" + |
4717 |
" return false;\n" + |
4718 |
" ^^^^^^^^^^^^^\n" + |
4719 |
"Resource leak: \'reader\' is not closed at this location\n" + |
4720 |
"----------\n", |
4721 |
null, |
4722 |
true, |
4723 |
options); |
4724 |
} |
4725 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4726 |
// different early exits, if no close seen report as definitely unclosed |
4727 |
public void test056x() { |
4728 |
Map options = getCompilerOptions(); |
4729 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4730 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4731 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4732 |
this.runNegativeTest( |
4733 |
new String[] { |
4734 |
"X.java", |
4735 |
"import java.io.FileReader;\n" + |
4736 |
"public class X {\n" + |
4737 |
" void foo31(boolean b) throws Exception {\n" + |
4738 |
" FileReader reader = new FileReader(\"file\");\n" + |
4739 |
" if (b) {\n" + |
4740 |
" reader.close();\n" + |
4741 |
" } else {\n" + |
4742 |
" return; // warning\n" + |
4743 |
" }\n" + |
4744 |
" }\n" + |
4745 |
" void foo32(boolean b) throws Exception {\n" + |
4746 |
" FileReader reader = new FileReader(\"file\"); // warn here\n" + |
4747 |
" return;\n" + |
4748 |
" }\n" + |
4749 |
"}\n" |
4750 |
}, |
4751 |
"----------\n" + |
4752 |
"1. ERROR in X.java (at line 8)\n" + |
4753 |
" return; // warning\n" + |
4754 |
" ^^^^^^^\n" + |
4755 |
"Resource leak: \'reader\' is not closed at this location\n" + |
4756 |
"----------\n" + |
4757 |
"2. ERROR in X.java (at line 12)\n" + |
4758 |
" FileReader reader = new FileReader(\"file\"); // warn here\n" + |
4759 |
" ^^^^^^\n" + |
4760 |
"Resource leak: \'reader\' is never closed\n" + |
4761 |
"----------\n", |
4762 |
null, |
4763 |
true, |
4764 |
options); |
4765 |
} |
4766 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4767 |
// nested method passes the resource to outside code |
4768 |
public void test056y() { |
4769 |
Map options = getCompilerOptions(); |
4770 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4771 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4772 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4773 |
this.runNegativeTest( |
4774 |
new String[] { |
4775 |
"X.java", |
4776 |
"import java.io.FileReader;\n" + |
4777 |
"public class X {\n" + |
4778 |
" void foo31(boolean b) throws Exception {\n" + |
4779 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
4780 |
" new Runnable() {\n" + |
4781 |
" public void run() {\n" + |
4782 |
" foo18(reader31);\n" + |
4783 |
" }\n" + |
4784 |
" }.run();\n" + |
4785 |
" }\n" + |
4786 |
" void foo18(FileReader r18) {\n" + |
4787 |
" // could theoretically close r18;\n" + |
4788 |
" }\n" + |
4789 |
" abstract class ResourceProvider {\n" + |
4790 |
" abstract FileReader provide();" + |
4791 |
" }\n" + |
4792 |
" ResourceProvider provider;" + |
4793 |
" void foo23() throws Exception {\n" + |
4794 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
4795 |
" provider = new ResourceProvider() {\n" + |
4796 |
" public FileReader provide() {\n" + |
4797 |
" return reader23;\n" + |
4798 |
" }\n" + |
4799 |
" };\n" + |
4800 |
" }\n" + |
4801 |
"}\n" |
4802 |
}, |
4803 |
"----------\n" + |
4804 |
"1. WARNING in X.java (at line 4)\n" + |
4805 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
4806 |
" ^^^^^^^^\n" + |
4807 |
"Potential resource leak: \'reader31\' may not be closed\n" + |
4808 |
"----------\n" + |
4809 |
"2. WARNING in X.java (at line 17)\n" + |
4810 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
4811 |
" ^^^^^^^^\n" + |
4812 |
"Potential resource leak: \'reader23\' may not be closed\n" + |
4813 |
"----------\n", |
4814 |
null, |
4815 |
true, |
4816 |
options); |
4817 |
} |
4818 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4819 |
// resource assigned to second local and is (potentially) closed on the latter |
4820 |
public void test056z() { |
4821 |
Map options = getCompilerOptions(); |
4822 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4823 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4824 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4825 |
this.runNegativeTest( |
4826 |
new String[] { |
4827 |
"X.java", |
4828 |
"import java.io.FileReader;\n" + |
4829 |
"public class X {\n" + |
4830 |
" void foo17() throws Exception {\n" + |
4831 |
" FileReader reader17 = new FileReader(\"file\");\n" + |
4832 |
" final FileReader readerCopy = reader17;\n" + |
4833 |
" readerCopy.close();\n" + |
4834 |
" }\n" + |
4835 |
" void foo17a() throws Exception {\n" + |
4836 |
" FileReader reader17a = new FileReader(\"file\");\n" + |
4837 |
" FileReader readerCopya;" + |
4838 |
" readerCopya = reader17a;\n" + |
4839 |
" bar(readerCopya);\n" + // potentially closes |
4840 |
" }\n" + |
4841 |
" void bar(FileReader r) {}\n" + |
4842 |
"}\n" |
4843 |
}, |
4844 |
"----------\n" + |
4845 |
"1. ERROR in X.java (at line 9)\n" + |
4846 |
" FileReader reader17a = new FileReader(\"file\");\n" + |
4847 |
" ^^^^^^^^^\n" + |
4848 |
"Potential resource leak: \'reader17a\' may not be closed\n" + |
4849 |
"----------\n", |
4850 |
null, |
4851 |
true, |
4852 |
options); |
4853 |
} |
4854 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4855 |
// multiple early exists from nested scopes (always closed) |
4856 |
public void test056zz() { |
4857 |
Map options = getCompilerOptions(); |
4858 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4859 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4860 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
4861 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4862 |
this.runNegativeTest( |
4863 |
new String[] { |
4864 |
"X.java", |
4865 |
"import java.io.FileReader;\n" + |
4866 |
"public class X {\n" + |
4867 |
" void foo16() throws Exception {\n" + |
4868 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
4869 |
" try {\n" + |
4870 |
" reader16.close();\n " + |
4871 |
" return;\n" + |
4872 |
" } catch (RuntimeException re) {\n" + |
4873 |
" return;\n" + |
4874 |
" } catch (Error e) {\n" + |
4875 |
" return;\n" + |
4876 |
" } finally {\n" + |
4877 |
" reader16.close();\n " + |
4878 |
" }\n" + |
4879 |
" }\n" + |
4880 |
"}\n" |
4881 |
}, |
4882 |
"----------\n" + |
4883 |
"1. ERROR in X.java (at line 4)\n" + |
4884 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
4885 |
" ^^^^^^^^\n" + |
4886 |
"Resource \'reader16\' should be managed by try-with-resource\n" + |
4887 |
"----------\n", |
4888 |
null, |
4889 |
true, |
4890 |
options); |
4891 |
} |
4892 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4893 |
// multiple early exists from nested scopes (never closed) |
4894 |
public void test056zzz() { |
4895 |
Map options = getCompilerOptions(); |
4896 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4897 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4898 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
4899 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4900 |
this.runNegativeTest( |
4901 |
new String[] { |
4902 |
"X.java", |
4903 |
"import java.io.FileReader;\n" + |
4904 |
"public class X {\n" + |
4905 |
" void foo16() throws Exception {\n" + |
4906 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
4907 |
" try {\n" + |
4908 |
" return;\n" + |
4909 |
" } catch (RuntimeException re) {\n" + |
4910 |
" return;\n" + |
4911 |
" } catch (Error e) {\n" + |
4912 |
" return;\n" + |
4913 |
" } finally {\n" + |
4914 |
" System.out.println();\n " + |
4915 |
" }\n" + |
4916 |
" }\n" + |
4917 |
"}\n" |
4918 |
}, |
4919 |
"----------\n" + |
4920 |
"1. ERROR in X.java (at line 4)\n" + |
4921 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
4922 |
" ^^^^^^^^\n" + |
4923 |
"Resource leak: \'reader16\' is never closed\n" + |
4924 |
"----------\n", |
4925 |
null, |
4926 |
true, |
4927 |
options); |
4928 |
} |
3435 |
public static Class testClass() { |
4929 |
public static Class testClass() { |
3436 |
return TryWithResourcesStatementTest.class; |
4930 |
return TryWithResourcesStatementTest.class; |
3437 |
} |
4931 |
} |