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