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[] { "test056i2" }; |
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 |
"Leaking resource '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 |
"Leaking resource '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 |
"Leaking resource '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 exception may occur earlier. |
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 |
this.runNegativeTest( |
3437 |
new String[] { |
3438 |
"X.java", |
3439 |
"import java.io.File;\n" + |
3440 |
"import java.io.FileReader;\n" + |
3441 |
"import java.io.IOException;\n" + |
3442 |
"public class X {\n" + |
3443 |
" void foo() throws IOException {\n" + |
3444 |
" File file = new File(\"somefile\");\n" + |
3445 |
" FileReader fileReader = new FileReader(file);\n" + |
3446 |
" char[] in = new char[50];\n" + |
3447 |
" fileReader.read(in);\n" + |
3448 |
" fileReader.close();\n" + |
3449 |
" }\n" + |
3450 |
" public static void main(String[] args) {\n" + |
3451 |
" try {\n" + |
3452 |
" new X().foo();\n" + |
3453 |
" } catch (IOException ioex) {\n" + |
3454 |
" System.out.println(\"caught\");\n" + |
3455 |
" }\n" + |
3456 |
" }\n" + |
3457 |
"}\n" |
3458 |
}, |
3459 |
"----------\n" + |
3460 |
"1. ERROR in X.java (at line 7)\n" + |
3461 |
" FileReader fileReader = new FileReader(file);\n" + |
3462 |
" ^^^^^^^^^^\n" + |
3463 |
"Potentially leaking resource 'fileReader': is not closed on all paths\n" + |
3464 |
"----------\n", |
3465 |
null, |
3466 |
true, |
3467 |
options); |
3468 |
} |
3469 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3470 |
// a method uses an AutoCloseable and closes it properly in a finally block |
3471 |
public void test056b() { |
3472 |
Map options = getCompilerOptions(); |
3473 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3474 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3475 |
this.runConformTest( |
3476 |
new String[] { |
3477 |
"X.java", |
3478 |
"import java.io.File;\n" + |
3479 |
"import java.io.FileReader;\n" + |
3480 |
"import java.io.IOException;\n" + |
3481 |
"public class X {\n" + |
3482 |
" void foo() throws IOException {\n" + |
3483 |
" File file = new File(\"somefile\");\n" + |
3484 |
" FileReader fileReader = new FileReader(file);\n" + |
3485 |
" try {\n" + |
3486 |
" char[] in = new char[50];\n" + |
3487 |
" fileReader.read(in);\n" + |
3488 |
" } finally {\n" + |
3489 |
" fileReader.close();\n" + |
3490 |
" }\n" + |
3491 |
" }\n" + |
3492 |
" public static void main(String[] args) {\n" + |
3493 |
" try {\n" + |
3494 |
" new X().foo();\n" + |
3495 |
" } catch (IOException ioex) {\n" + |
3496 |
" System.out.println(\"caught\");\n" + |
3497 |
" }\n" + |
3498 |
" }\n" + |
3499 |
"}\n" |
3500 |
}, |
3501 |
"caught", /*output*/ |
3502 |
null/*classLibs*/, |
3503 |
true/*shouldFlush*/, |
3504 |
null/*vmargs*/, |
3505 |
options, |
3506 |
null/*requestor*/); |
3507 |
} |
3508 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3509 |
// a method uses an AutoCloseable properly within try-with-resources. |
3510 |
public void test056c() { |
3511 |
Map options = getCompilerOptions(); |
3512 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3513 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3514 |
this.runConformTest( |
3515 |
new String[] { |
3516 |
"X.java", |
3517 |
"import java.io.File;\n" + |
3518 |
"import java.io.FileReader;\n" + |
3519 |
"import java.io.IOException;\n" + |
3520 |
"public class X {\n" + |
3521 |
" void foo() throws IOException {\n" + |
3522 |
" File file = new File(\"somefile\");\n" + |
3523 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
3524 |
" char[] in = new char[50];\n" + |
3525 |
" fileReader.read(in);\n" + |
3526 |
" }\n" + |
3527 |
" }\n" + |
3528 |
" public static void main(String[] args) {\n" + |
3529 |
" try {\n" + |
3530 |
" new X().foo();\n" + |
3531 |
" } catch (IOException ioex) {\n" + |
3532 |
" System.out.println(\"caught\");\n" + |
3533 |
" }\n" + |
3534 |
" }\n" + |
3535 |
"}\n" |
3536 |
}, |
3537 |
"caught", /*output*/ |
3538 |
null/*classLibs*/, |
3539 |
true/*shouldFlush*/, |
3540 |
null/*vmargs*/, |
3541 |
options, |
3542 |
null/*requestor*/); |
3543 |
} |
3544 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3545 |
// a method uses two AutoCloseables (testing independent analysis) |
3546 |
// - one closeable may be unclosed at a conditional return |
3547 |
// - the other is only conditionally closed |
3548 |
public void test056d() { |
3549 |
Map options = getCompilerOptions(); |
3550 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3551 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3552 |
this.runNegativeTest( |
3553 |
new String[] { |
3554 |
"X.java", |
3555 |
"import java.io.File;\n" + |
3556 |
"import java.io.FileReader;\n" + |
3557 |
"import java.io.IOException;\n" + |
3558 |
"public class X {\n" + |
3559 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
3560 |
" File file = new File(\"somefile\");\n" + |
3561 |
" char[] in = new char[50];\n" + |
3562 |
" FileReader fileReader1 = new FileReader(file);\n" + |
3563 |
" fileReader1.read(in);\n" + |
3564 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3565 |
" fileReader2.read(in);\n" + |
3566 |
" if (flag1) {\n" + |
3567 |
" fileReader2.close();\n" + |
3568 |
" return;\n" + |
3569 |
" } else if (flag2) {\n" + |
3570 |
" fileReader2.close();\n" + |
3571 |
" }\n" + |
3572 |
" fileReader1.close();\n" + |
3573 |
" }\n" + |
3574 |
" public static void main(String[] args) throws IOException {\n" + |
3575 |
" new X().foo(false, true);\n" + |
3576 |
" }\n" + |
3577 |
"}\n" |
3578 |
}, |
3579 |
"----------\n" + |
3580 |
"1. WARNING in X.java (at line 10)\n" + |
3581 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3582 |
" ^^^^^^^^^^^\n" + |
3583 |
"Potentially leaking resource 'fileReader2': is not closed on all paths\n" + |
3584 |
"----------\n" + |
3585 |
"2. ERROR in X.java (at line 14)\n" + |
3586 |
" return;\n" + |
3587 |
" ^^^^^^^\n" + |
3588 |
"Leaking resource \'fileReader1\': is not closed at this location\n" + |
3589 |
"----------\n", |
3590 |
null, |
3591 |
true, |
3592 |
options); |
3593 |
} |
3594 |
//Bug 349326 - [1.7] new warning for missing try-with-resources |
3595 |
//a method uses two AutoCloseables (testing independent analysis) |
3596 |
//- one closeable may be unclosed at a conditional return |
3597 |
//- the other is only conditionally closed |
3598 |
public void test056d_suppress() { |
3599 |
Map options = getCompilerOptions(); |
3600 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3601 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3602 |
options.put(CompilerOptions.OPTION_SuppressOptionalErrors, CompilerOptions.ENABLED); |
3603 |
this.runNegativeTest( |
3604 |
new String[] { |
3605 |
"X.java", |
3606 |
"import java.io.File;\n" + |
3607 |
"import java.io.FileReader;\n" + |
3608 |
"import java.io.IOException;\n" + |
3609 |
"public class X {\n" + |
3610 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
3611 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
3612 |
" char[] in = new char[50];\n" + |
3613 |
" FileReader fileReader1 = new FileReader(file);\n" + |
3614 |
" fileReader1.read(in);\n" + |
3615 |
" @SuppressWarnings(\"resource\") FileReader fileReader2 = new FileReader(file); // useful suppress\n" + |
3616 |
" fileReader2.read(in);\n" + |
3617 |
" if (flag1) {\n" + |
3618 |
" fileReader2.close();\n" + |
3619 |
" return; // not suppressed\n" + |
3620 |
" } else if (flag2) {\n" + |
3621 |
" fileReader2.close();\n" + |
3622 |
" }\n" + |
3623 |
" fileReader1.close();\n" + |
3624 |
" }\n" + |
3625 |
" @SuppressWarnings(\"resource\") // useful suppress\n" + |
3626 |
" void bar() throws IOException {\n" + |
3627 |
" File file = new File(\"somefile\");\n" + |
3628 |
" FileReader fileReader = new FileReader(file);\n" + |
3629 |
" char[] in = new char[50];\n" + |
3630 |
" fileReader.read(in);\n" + |
3631 |
" }\n" + |
3632 |
" public static void main(String[] args) throws IOException {\n" + |
3633 |
" new X().foo(false, true);\n" + |
3634 |
" }\n" + |
3635 |
"}\n" |
3636 |
}, |
3637 |
"----------\n" + |
3638 |
"1. WARNING in X.java (at line 6)\n" + |
3639 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
3640 |
" ^^^^^^^^^^\n" + |
3641 |
"Unnecessary @SuppressWarnings(\"resource\")\n" + |
3642 |
"----------\n" + |
3643 |
"2. ERROR in X.java (at line 14)\n" + |
3644 |
" return; // not suppressed\n" + |
3645 |
" ^^^^^^^\n" + |
3646 |
"Leaking resource \'fileReader1\': is not closed at this location\n" + |
3647 |
"----------\n", |
3648 |
null, |
3649 |
true, |
3650 |
options); |
3651 |
} |
3652 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3653 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3654 |
public void test056e() { |
3655 |
Map options = getCompilerOptions(); |
3656 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3657 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3658 |
this.runNegativeTest( |
3659 |
new String[] { |
3660 |
"X.java", |
3661 |
"import java.io.File;\n" + |
3662 |
"import java.io.FileReader;\n" + |
3663 |
"import java.io.IOException;\n" + |
3664 |
"public class X {\n" + |
3665 |
" FileReader getReader(String filename) throws IOException {\n" + |
3666 |
" File file = new File(\"somefile\");\n" + |
3667 |
" FileReader fileReader = new FileReader(file);\n" + |
3668 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
3669 |
" }\n" + |
3670 |
" void foo() throws IOException {\n" + |
3671 |
" FileReader reader = getReader(\"somefile\");\n" + |
3672 |
" char[] in = new char[50];\n" + |
3673 |
" reader.read(in);\n" + |
3674 |
" }\n" + |
3675 |
" public static void main(String[] args) throws IOException {\n" + |
3676 |
" new X().foo();\n" + |
3677 |
" }\n" + |
3678 |
"}\n" |
3679 |
}, |
3680 |
"----------\n" + |
3681 |
"1. ERROR in X.java (at line 11)\n" + |
3682 |
" FileReader reader = getReader(\"somefile\");\n" + |
3683 |
" ^^^^^^\n" + |
3684 |
"Leaking resource 'reader': is never closed\n" + |
3685 |
"----------\n", |
3686 |
null, |
3687 |
true, |
3688 |
options); |
3689 |
} |
3690 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3691 |
// a method explicitly closes its AutoCloseable rather than using t-w-r |
3692 |
public void test056f() { |
3693 |
Map options = getCompilerOptions(); |
3694 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3695 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3696 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3697 |
this.runNegativeTest( |
3698 |
new String[] { |
3699 |
"X.java", |
3700 |
"import java.io.File;\n" + |
3701 |
"import java.io.FileReader;\n" + |
3702 |
"import java.io.IOException;\n" + |
3703 |
"public class X {\n" + |
3704 |
" void foo() throws IOException {\n" + |
3705 |
" File file = new File(\"somefile\");\n" + |
3706 |
" FileReader fileReader = null;\n" + |
3707 |
" try {\n" + |
3708 |
" fileReader = new FileReader(file);\n" + |
3709 |
" char[] in = new char[50];\n" + |
3710 |
" fileReader.read(in);\n" + |
3711 |
" } finally {\n" + |
3712 |
" fileReader.close();\n" + |
3713 |
" }\n" + |
3714 |
" }\n" + |
3715 |
" public static void main(String[] args) throws IOException {\n" + |
3716 |
" new X().foo();\n" + |
3717 |
" }\n" + |
3718 |
"}\n" |
3719 |
}, |
3720 |
"----------\n" + |
3721 |
"1. ERROR in X.java (at line 7)\n" + |
3722 |
" FileReader fileReader = null;\n" + |
3723 |
" ^^^^^^^^^^\n" + |
3724 |
"Resource \'fileReader\' should be managed by try-with-resource\n" + |
3725 |
"----------\n", |
3726 |
null, |
3727 |
true, |
3728 |
options); |
3729 |
} |
3730 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3731 |
// an AutoCloseable local is re-assigned |
3732 |
public void test056g() { |
3733 |
Map options = getCompilerOptions(); |
3734 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3735 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3736 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3737 |
this.runNegativeTest( |
3738 |
new String[] { |
3739 |
"X.java", |
3740 |
"import java.io.File;\n" + |
3741 |
"import java.io.FileReader;\n" + |
3742 |
"import java.io.IOException;\n" + |
3743 |
"public class X {\n" + |
3744 |
" void foo() throws IOException {\n" + |
3745 |
" File file = new File(\"somefile\");\n" + |
3746 |
" FileReader fileReader = new FileReader(file);\n" + |
3747 |
" char[] in = new char[50];\n" + |
3748 |
" fileReader.read(in);\n" + |
3749 |
" fileReader = new FileReader(file);\n" + |
3750 |
" fileReader.read(in);\n" + |
3751 |
" fileReader.close();\n" + |
3752 |
" fileReader = null;\n" + |
3753 |
" }\n" + |
3754 |
" public static void main(String[] args) throws IOException {\n" + |
3755 |
" new X().foo();\n" + |
3756 |
" }\n" + |
3757 |
"}\n" |
3758 |
}, |
3759 |
"----------\n" + |
3760 |
"1. ERROR in X.java (at line 10)\n" + |
3761 |
" fileReader = new FileReader(file);\n" + |
3762 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3763 |
"Leaking resource \'fileReader\': is not closed at this location\n" + |
3764 |
"----------\n", |
3765 |
null, |
3766 |
true, |
3767 |
options); |
3768 |
} |
3769 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3770 |
// an AutoCloseable local is re-assigned after null-assigned |
3771 |
public void test056g2() { |
3772 |
Map options = getCompilerOptions(); |
3773 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3774 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3775 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3776 |
this.runNegativeTest( |
3777 |
new String[] { |
3778 |
"X.java", |
3779 |
"import java.io.File;\n" + |
3780 |
"import java.io.FileReader;\n" + |
3781 |
"import java.io.IOException;\n" + |
3782 |
"public class X {\n" + |
3783 |
" void foo() throws IOException {\n" + |
3784 |
" File file = new File(\"somefile\");\n" + |
3785 |
" FileReader fileReader = new FileReader(file);\n" + |
3786 |
" char[] in = new char[50];\n" + |
3787 |
" fileReader.read(in);\n" + |
3788 |
" fileReader = null;\n" + |
3789 |
" fileReader = new FileReader(file);\n" + // don't complain again, fileReader is null, so nothing can leak here |
3790 |
" fileReader.read(in);\n" + |
3791 |
" fileReader.close();\n" + |
3792 |
" }\n" + |
3793 |
" public static void main(String[] args) throws IOException {\n" + |
3794 |
" new X().foo();\n" + |
3795 |
" }\n" + |
3796 |
"}\n" |
3797 |
}, |
3798 |
"----------\n" + |
3799 |
"1. ERROR in X.java (at line 10)\n" + |
3800 |
" fileReader = null;\n" + |
3801 |
" ^^^^^^^^^^^^^^^^^\n" + |
3802 |
"Leaking resource \'fileReader\': is not closed at this location\n" + |
3803 |
"----------\n", |
3804 |
null, |
3805 |
true, |
3806 |
options); |
3807 |
} |
3808 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3809 |
// two AutoCloseables at different nesting levels (anonymous local type) |
3810 |
public void test056h() { |
3811 |
Map options = getCompilerOptions(); |
3812 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3813 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3814 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3815 |
this.runNegativeTest( |
3816 |
new String[] { |
3817 |
"X.java", |
3818 |
"import java.io.File;\n" + |
3819 |
"import java.io.FileReader;\n" + |
3820 |
"import java.io.IOException;\n" + |
3821 |
"public class X {\n" + |
3822 |
" void foo() throws IOException {\n" + |
3823 |
" final File file = new File(\"somefile\");\n" + |
3824 |
" final FileReader fileReader = new FileReader(file);\n" + |
3825 |
" char[] in = new char[50];\n" + |
3826 |
" fileReader.read(in);\n" + |
3827 |
" new Runnable() {\n public void run() {\n" + |
3828 |
" try {\n" + |
3829 |
" fileReader.close();\n" + |
3830 |
" FileReader localReader = new FileReader(file);\n" + |
3831 |
" } catch (IOException ex) { /* nop */ }\n" + |
3832 |
" }}.run();\n" + |
3833 |
" }\n" + |
3834 |
" public static void main(String[] args) throws IOException {\n" + |
3835 |
" new X().foo();\n" + |
3836 |
" }\n" + |
3837 |
"}\n" |
3838 |
}, |
3839 |
"----------\n" + |
3840 |
"1. WARNING in X.java (at line 7)\n" + |
3841 |
" final FileReader fileReader = new FileReader(file);\n" + |
3842 |
" ^^^^^^^^^^\n" + |
3843 |
"Potentially leaking resource 'fileReader': is not closed on all paths\n" + |
3844 |
"----------\n" + |
3845 |
"2. ERROR in X.java (at line 14)\n" + |
3846 |
" FileReader localReader = new FileReader(file);\n" + |
3847 |
" ^^^^^^^^^^^\n" + |
3848 |
"Leaking resource 'localReader': is never closed\n" + |
3849 |
"----------\n", |
3850 |
null, |
3851 |
true, |
3852 |
options); |
3853 |
} |
3854 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3855 |
// three AutoCloseables in different blocks of the same method |
3856 |
public void test056i() { |
3857 |
Map options = getCompilerOptions(); |
3858 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3859 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3860 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3861 |
this.runNegativeTest( |
3862 |
new String[] { |
3863 |
"X.java", |
3864 |
"import java.io.File;\n" + |
3865 |
"import java.io.FileReader;\n" + |
3866 |
"import java.io.IOException;\n" + |
3867 |
"public class X {\n" + |
3868 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
3869 |
" File file = new File(\"somefile\");\n" + |
3870 |
" if (f1) {\n" + |
3871 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
3872 |
" char[] in = new char[50];\n" + |
3873 |
" fileReader.read(in);\n" + |
3874 |
" while (true) {\n" + |
3875 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
3876 |
" loopReader.close();" + |
3877 |
" break;\n" + |
3878 |
" }\n" + |
3879 |
" } else {\n" + |
3880 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3881 |
" if (f2)\n" + |
3882 |
" fileReader.close();\n" + |
3883 |
" }\n" + |
3884 |
" }\n" + |
3885 |
" public static void main(String[] args) throws IOException {\n" + |
3886 |
" new X().foo(true, true);\n" + |
3887 |
" }\n" + |
3888 |
"}\n" |
3889 |
}, |
3890 |
"----------\n" + |
3891 |
"1. ERROR in X.java (at line 8)\n" + |
3892 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
3893 |
" ^^^^^^^^^^\n" + |
3894 |
"Leaking resource 'fileReader': is never closed\n" + |
3895 |
"----------\n" + |
3896 |
"2. WARNING in X.java (at line 16)\n" + |
3897 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3898 |
" ^^^^^^^^^^\n" + |
3899 |
"Potentially leaking resource 'fileReader': is not closed on all paths\n" + |
3900 |
"----------\n", |
3901 |
null, |
3902 |
true, |
3903 |
options); |
3904 |
} |
3905 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3906 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
3907 |
public void test056j() { |
3908 |
Map options = getCompilerOptions(); |
3909 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3910 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3911 |
this.runNegativeTest( |
3912 |
new String[] { |
3913 |
"X.java", |
3914 |
"import java.io.File;\n" + |
3915 |
"import java.io.FileReader;\n" + |
3916 |
"import java.io.IOException;\n" + |
3917 |
"public class X {\n" + |
3918 |
" void foo() throws IOException {\n" + |
3919 |
" File file = new File(\"somefile\");\n" + |
3920 |
" FileReader fileReader = new FileReader(file);\n" + |
3921 |
" read(fileReader);\n" + |
3922 |
" }\n" + |
3923 |
" void read(FileReader reader) { }\n" + |
3924 |
" public static void main(String[] args) throws IOException {\n" + |
3925 |
" new X().foo();\n" + |
3926 |
" }\n" + |
3927 |
"}\n" |
3928 |
}, |
3929 |
"----------\n" + |
3930 |
"1. ERROR in X.java (at line 7)\n" + |
3931 |
" FileReader fileReader = new FileReader(file);\n" + |
3932 |
" ^^^^^^^^^^\n" + |
3933 |
"Potentially leaking resource 'fileReader': is not closed on all paths\n" + |
3934 |
"----------\n", |
3935 |
null, |
3936 |
true, |
3937 |
options); |
3938 |
} |
3939 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3940 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
3941 |
public void test056jconditional() { |
3942 |
Map options = getCompilerOptions(); |
3943 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3944 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3945 |
this.runNegativeTest( |
3946 |
new String[] { |
3947 |
"X.java", |
3948 |
"import java.io.File;\n" + |
3949 |
"import java.io.FileReader;\n" + |
3950 |
"import java.io.IOException;\n" + |
3951 |
"public class X {\n" + |
3952 |
" void foo(boolean b) throws IOException {\n" + |
3953 |
" File file = new File(\"somefile\");\n" + |
3954 |
" FileReader fileReader = new FileReader(file);\n" + |
3955 |
" synchronized (b ? this : new X()) {\n" + |
3956 |
" new ReadDelegator(fileReader);\n" + |
3957 |
" }\n" + |
3958 |
" }\n" + |
3959 |
" class ReadDelegator { ReadDelegator(FileReader reader) { } }\n" + |
3960 |
" public static void main(String[] args) throws IOException {\n" + |
3961 |
" new X().foo(true);\n" + |
3962 |
" }\n" + |
3963 |
"}\n" |
3964 |
}, |
3965 |
"----------\n" + |
3966 |
"1. ERROR in X.java (at line 7)\n" + |
3967 |
" FileReader fileReader = new FileReader(file);\n" + |
3968 |
" ^^^^^^^^^^\n" + |
3969 |
"Potentially leaking resource 'fileReader': is not closed on all paths\n" + |
3970 |
"----------\n", |
3971 |
null, |
3972 |
true, |
3973 |
options); |
3974 |
} |
3975 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3976 |
// many locals, some are AutoCloseable. |
3977 |
// Unfortunately analysis cannot respect how exception exits may affect ra3 and rb3, |
3978 |
// doing so would create false positives. |
3979 |
public void test056k() { |
3980 |
Map options = getCompilerOptions(); |
3981 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3982 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3983 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3984 |
this.runNegativeTest( |
3985 |
new String[] { |
3986 |
"X.java", |
3987 |
"import java.io.File;\n" + |
3988 |
"import java.io.FileReader;\n" + |
3989 |
"import java.io.IOException;\n" + |
3990 |
"public class X {\n" + |
3991 |
" void foo() throws IOException {\n" + |
3992 |
" int i01, i02, i03, i04, i05, i06, i07, i08, i09,\n" + |
3993 |
" i11, i12, i13, i14, i15, i16, i17, i18, i19,\n" + |
3994 |
" i21, i22, i23, i24, i25, i26, i27, i28, i29,\n" + |
3995 |
" i31, i32, i33, i34, i35, i36, i37, i38, i39,\n" + |
3996 |
" i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" + |
3997 |
" File file = new File(\"somefile\");\n" + |
3998 |
" FileReader ra1 = null, ra2 = null;\n" + |
3999 |
" try {\n" + |
4000 |
" ra1 = new FileReader(file);\n" + |
4001 |
" ra2 = new FileReader(file);\n" + |
4002 |
" FileReader ra3 = new FileReader(file);\n" + |
4003 |
" char[] in = new char[50];\n" + |
4004 |
" ra1.read(in);\n" + |
4005 |
" ra2.read(in);\n" + |
4006 |
" ra3.close();\n" + |
4007 |
" } finally {\n" + |
4008 |
" ra1.close();\n" + |
4009 |
" }\n" + |
4010 |
" int i51, i52, i53, i54, i55, i56, i57, i58, i59, i60;\n" + // beyond this point locals are analyzed using extraBits |
4011 |
" FileReader rb1 = null, rb2 = null;\n" + |
4012 |
" try {\n" + |
4013 |
" rb1 = new FileReader(file);\n" + |
4014 |
" rb2 = new FileReader(file);\n" + |
4015 |
" FileReader rb3 = new FileReader(file);\n" + |
4016 |
" char[] in = new char[50];\n" + |
4017 |
" rb1.read(in);\n" + |
4018 |
" rb2.read(in);\n" + |
4019 |
" rb3.close();\n" + |
4020 |
" } finally {\n" + |
4021 |
" rb1.close();\n" + |
4022 |
" }\n" + |
4023 |
" }\n" + |
4024 |
" public static void main(String[] args) throws IOException {\n" + |
4025 |
" new X().foo();\n" + |
4026 |
" }\n" + |
4027 |
"}\n" |
4028 |
}, |
4029 |
"----------\n" + |
4030 |
"1. ERROR in X.java (at line 12)\n" + |
4031 |
" FileReader ra1 = null, ra2 = null;\n" + |
4032 |
" ^^^\n" + |
4033 |
"Resource \'ra1\' should be managed by try-with-resource\n" + |
4034 |
"----------\n" + |
4035 |
"2. ERROR in X.java (at line 15)\n" + |
4036 |
" ra2 = new FileReader(file);\n" + |
4037 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4038 |
"Leaking resource 'ra2': is never closed\n" + |
4039 |
"----------\n" + |
4040 |
"3. ERROR in X.java (at line 16)\n" + |
4041 |
" FileReader ra3 = new FileReader(file);\n" + |
4042 |
" ^^^\n" + |
4043 |
"Resource \'ra3\' should be managed by try-with-resource\n" + |
4044 |
"----------\n" + |
4045 |
"4. ERROR in X.java (at line 25)\n" + |
4046 |
" FileReader rb1 = null, rb2 = null;\n" + |
4047 |
" ^^^\n" + |
4048 |
"Resource \'rb1\' should be managed by try-with-resource\n" + |
4049 |
"----------\n" + |
4050 |
"5. ERROR in X.java (at line 28)\n" + |
4051 |
" rb2 = new FileReader(file);\n" + |
4052 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4053 |
"Leaking resource 'rb2': is never closed\n" + |
4054 |
"----------\n" + |
4055 |
"6. ERROR in X.java (at line 29)\n" + |
4056 |
" FileReader rb3 = new FileReader(file);\n" + |
4057 |
" ^^^\n" + |
4058 |
"Resource \'rb3\' should be managed by try-with-resource\n" + |
4059 |
"----------\n", |
4060 |
null, |
4061 |
true, |
4062 |
options); |
4063 |
} |
4064 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4065 |
// various non-problems |
4066 |
public void test056l() { |
4067 |
Map options = getCompilerOptions(); |
4068 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4069 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4070 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
4071 |
this.runNegativeTest( |
4072 |
new String[] { |
4073 |
"X.java", |
4074 |
"import java.io.File;\n" + |
4075 |
"import java.io.FileReader;\n" + |
4076 |
"import java.io.IOException;\n" + |
4077 |
"public class X {\n" + |
4078 |
" X(FileReader r0) {}\n" + // don't complain against argument |
4079 |
" FileReader getReader() { return null; }\n" + |
4080 |
" void foo(FileReader r1) throws IOException {\n" + |
4081 |
" FileReader fileReader = getReader();\n" + |
4082 |
" if (fileReader == null)\n" + |
4083 |
" return;\n" + // don't complain, resource is actually null |
4084 |
" FileReader r3 = getReader();\n" + |
4085 |
" if (r3 == null)\n" + |
4086 |
" r3 = new FileReader(new File(\"absent\"));\n" + // don't complain, previous resource is actually null |
4087 |
" try {\n" + |
4088 |
" char[] in = new char[50];\n" + |
4089 |
" fileReader.read(in);\n" + |
4090 |
" r1.read(in);\n" + |
4091 |
" } finally {\n" + |
4092 |
" fileReader.close();\n" + |
4093 |
" r3.close();\n" + // the effect of this close() call might be spoiled by exception in fileReader.close() above |
4094 |
" }\n" + |
4095 |
" }\n" + |
4096 |
" public static void main(String[] args) throws IOException {\n" + |
4097 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
4098 |
" new X(r2).foo(new FileReader(new File(\"notthere\")));\n" + |
4099 |
" }\n" + |
4100 |
"}\n" |
4101 |
}, |
4102 |
"----------\n" + |
4103 |
"1. ERROR in X.java (at line 8)\n" + |
4104 |
" FileReader fileReader = getReader();\n" + |
4105 |
" ^^^^^^^^^^\n" + |
4106 |
"Resource \'fileReader\' should be managed by try-with-resource\n" + |
4107 |
"----------\n" + |
4108 |
"2. ERROR in X.java (at line 11)\n" + |
4109 |
" FileReader r3 = getReader();\n" + |
4110 |
" ^^\n" + |
4111 |
"Potentially leaking resource 'r3': is not closed on all paths\n" + |
4112 |
"----------\n" + |
4113 |
"3. ERROR in X.java (at line 24)\n" + |
4114 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
4115 |
" ^^\n" + |
4116 |
"Potentially leaking resource 'r2': is not closed on all paths\n" + |
4117 |
"----------\n", |
4118 |
null, |
4119 |
true, |
4120 |
options); |
4121 |
} |
4122 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4123 |
// nested try with early exit |
4124 |
public void test056m() { |
4125 |
Map options = getCompilerOptions(); |
4126 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4127 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4128 |
this.runConformTest( |
4129 |
new String[] { |
4130 |
"X.java", |
4131 |
"import java.io.File;\n" + |
4132 |
"import java.io.FileReader;\n" + |
4133 |
"import java.io.IOException;\n" + |
4134 |
"public class X {\n" + |
4135 |
" void foo() {\n" + |
4136 |
" File file = new File(\"somefile\");" + |
4137 |
" try {\n" + |
4138 |
" FileReader fileReader = new FileReader(file);\n" + |
4139 |
" try {\n" + |
4140 |
" char[] in = new char[50];\n" + |
4141 |
" if (fileReader.read(in)==0)\n" + |
4142 |
" return;\n" + |
4143 |
" } finally {\n" + |
4144 |
" fileReader.close();\n" + |
4145 |
" }\n" + |
4146 |
" } catch (IOException e) {\n" + |
4147 |
" System.out.println(\"caught\");\n" + |
4148 |
" }\n" + |
4149 |
" }\n" + |
4150 |
" public static void main(String[] args) {\n" + |
4151 |
" new X().foo();\n" + |
4152 |
" }\n" + |
4153 |
"}\n" |
4154 |
}, |
4155 |
"caught", /*output*/ |
4156 |
null/*classLibs*/, |
4157 |
true/*shouldFlush*/, |
4158 |
null/*vmargs*/, |
4159 |
options, |
4160 |
null/*requestor*/); |
4161 |
} |
4162 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4163 |
// nested try should not interfere with earlier analysis. |
4164 |
public void test056n() { |
4165 |
Map options = getCompilerOptions(); |
4166 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4167 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4168 |
this.runConformTest( |
4169 |
new String[] { |
4170 |
"X.java", |
4171 |
"import java.io.File;\n" + |
4172 |
"import java.io.FileReader;\n" + |
4173 |
"import java.io.IOException;\n" + |
4174 |
"import java.io.FileNotFoundException;\n" + |
4175 |
"public class X {\n" + |
4176 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
4177 |
" FileReader fr1 = new FileReader(someFile);\n" + |
4178 |
" try {\n" + |
4179 |
" fr1.read(buf);\n" + |
4180 |
" } finally {\n" + |
4181 |
" fr1.close();\n" + |
4182 |
" }\n" + |
4183 |
" try {\n" + |
4184 |
" FileReader fr3 = new FileReader(someFile);\n" + |
4185 |
" try {\n" + |
4186 |
" } finally {\n" + |
4187 |
" fr3.close();\n" + |
4188 |
" }\n" + |
4189 |
" } catch (IOException e) {\n" + |
4190 |
" }\n" + |
4191 |
" }\n" + |
4192 |
" public static void main(String[] args) throws IOException {\n" + |
4193 |
" try {\n" + |
4194 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
4195 |
" } catch (FileNotFoundException e) {\n" + |
4196 |
" System.out.println(\"caught\");\n" + |
4197 |
" }\n" + |
4198 |
" }\n" + |
4199 |
"}\n" |
4200 |
}, |
4201 |
"caught", /*output*/ |
4202 |
null/*classLibs*/, |
4203 |
true/*shouldFlush*/, |
4204 |
null/*vmargs*/, |
4205 |
options, |
4206 |
null/*requestor*/); |
4207 |
} |
4208 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4209 |
// if close is guarded by null check this should still be recognized as definitely closed |
4210 |
public void test056o() { |
4211 |
Map options = getCompilerOptions(); |
4212 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4213 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4214 |
this.runConformTest( |
4215 |
new String[] { |
4216 |
"X.java", |
4217 |
"import java.io.File;\n" + |
4218 |
"import java.io.FileReader;\n" + |
4219 |
"import java.io.IOException;\n" + |
4220 |
"import java.io.FileNotFoundException;\n" + |
4221 |
"public class X {\n" + |
4222 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
4223 |
" FileReader fr1 = null;\n" + |
4224 |
" try {\n" + |
4225 |
" fr1 = new FileReader(someFile);" + |
4226 |
" fr1.read(buf);\n" + |
4227 |
" } finally {\n" + |
4228 |
" if (fr1 != null)\n" + |
4229 |
" try {\n" + |
4230 |
" fr1.close();\n" + |
4231 |
" } catch (IOException e) { /*do nothing*/ }\n" + |
4232 |
" }\n" + |
4233 |
" }\n" + |
4234 |
" public static void main(String[] args) throws IOException {\n" + |
4235 |
" try {\n" + |
4236 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
4237 |
" } catch (FileNotFoundException e) {\n" + |
4238 |
" System.out.println(\"caught\");\n" + |
4239 |
" }\n" + |
4240 |
" }\n" + |
4241 |
"}\n" |
4242 |
}, |
4243 |
"caught", /*output*/ |
4244 |
null/*classLibs*/, |
4245 |
true/*shouldFlush*/, |
4246 |
null/*vmargs*/, |
4247 |
options, |
4248 |
null/*requestor*/); |
4249 |
} |
4250 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4251 |
// a method uses an AutoCloseable without ever closing it, type from a type variable |
4252 |
public void test056p() { |
4253 |
Map options = getCompilerOptions(); |
4254 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4255 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
4256 |
this.runNegativeTest( |
4257 |
new String[] { |
4258 |
"X.java", |
4259 |
"import java.io.File;\n" + |
4260 |
"import java.io.FileReader;\n" + |
4261 |
"import java.io.Reader;\n" + |
4262 |
"import java.io.IOException;\n" + |
4263 |
"public abstract class X <T extends Reader> {\n" + |
4264 |
" void foo() throws IOException {\n" + |
4265 |
" File file = new File(\"somefile\");\n" + |
4266 |
" T fileReader = newReader(file);\n" + |
4267 |
" char[] in = new char[50];\n" + |
4268 |
" fileReader.read(in);\n" + |
4269 |
" }\n" + |
4270 |
" abstract T newReader(File file) throws IOException;\n" + |
4271 |
" public static void main(String[] args) throws IOException {\n" + |
4272 |
" new X<FileReader>() {\n" + |
4273 |
" FileReader newReader(File f) throws IOException { return new FileReader(f); }\n" + |
4274 |
" }.foo();\n" + |
4275 |
" }\n" + |
4276 |
"}\n" |
4277 |
}, |
4278 |
"----------\n" + |
4279 |
"1. ERROR in X.java (at line 8)\n" + |
4280 |
" T fileReader = newReader(file);\n" + |
4281 |
" ^^^^^^^^^^\n" + |
4282 |
"Leaking resource 'fileReader': is never closed\n" + |
4283 |
"----------\n", |
4284 |
null, |
4285 |
true, |
4286 |
options); |
4287 |
} |
4288 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4289 |
// closed in dead code |
4290 |
public void test056q() { |
4291 |
Map options = getCompilerOptions(); |
4292 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
4293 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
4294 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
4295 |
this.runNegativeTest( |
4296 |
new String[] { |
4297 |
"X.java", |
4298 |
"import java.io.File;\n" + |
4299 |
"import java.io.FileReader;\n" + |
4300 |
"import java.io.IOException;\n" + |
4301 |
"public class X {\n" + |
4302 |
" void foo() throws IOException {\n" + |
4303 |
" File file = new File(\"somefile\");\n" + |
4304 |
" FileReader fileReader = new FileReader(file);\n" + |
4305 |
" char[] in = new char[50];\n" + |
4306 |
" fileReader.read(in);\n" + |
4307 |
" if (2*2 == 4)\n" + |
4308 |
" return;\n" + |
4309 |
" fileReader.close();\n" + |
4310 |
" }\n" + |
4311 |
" public static void main(String[] args) throws IOException {\n" + |
4312 |
" new X().foo();\n" + |
4313 |
" }\n" + |
4314 |
"}\n" |
4315 |
}, |
4316 |
"----------\n" + |
4317 |
"1. ERROR in X.java (at line 7)\n" + |
4318 |
" FileReader fileReader = new FileReader(file);\n" + |
4319 |
" ^^^^^^^^^^\n" + |
4320 |
"Leaking resource \'fileReader\': is never closed\n" + |
4321 |
"----------\n" + |
4322 |
"2. WARNING in X.java (at line 10)\n" + |
4323 |
" if (2*2 == 4)\n" + |
4324 |
" ^^^^^^^^\n" + |
4325 |
"Comparing identical expressions\n" + |
4326 |
"----------\n" + |
4327 |
"3. WARNING in X.java (at line 12)\n" + |
4328 |
" fileReader.close();\n" + |
4329 |
" ^^^^^^^^^^^^^^^^^^\n" + |
4330 |
"Dead code\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 test056r() { |
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 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
4344 |
this.runNegativeTest( |
4345 |
new String[] { |
4346 |
"X.java", |
4347 |
"import java.io.File;\n" + |
4348 |
"import java.io.FileReader;\n" + |
4349 |
"import java.io.IOException;\n" + |
4350 |
"public class X {\n" + |
4351 |
" void foo() throws IOException {\n" + |
4352 |
" File file = new File(\"somefile\");\n" + |
4353 |
" FileReader fr = new FileReader(file);\n" + |
4354 |
" Object b = null;\n" + |
4355 |
" fr.close();\n" + |
4356 |
" if (b != null) {\n" + |
4357 |
" fr = new FileReader(file);\n" + |
4358 |
" return;\n" + |
4359 |
" } else {\n" + |
4360 |
" System.out.print(42);\n" + |
4361 |
" }\n" + |
4362 |
" return; // Should not complain about fr\n" + |
4363 |
" }\n" + |
4364 |
" public static void main(String[] args) throws IOException {\n" + |
4365 |
" new X().foo();\n" + |
4366 |
" }\n" + |
4367 |
"}\n" |
4368 |
}, |
4369 |
"----------\n" + |
4370 |
"1. ERROR in X.java (at line 10)\n" + |
4371 |
" if (b != null) {\n" + |
4372 |
" fr = new FileReader(file);\n" + |
4373 |
" return;\n" + |
4374 |
" } else {\n" + |
4375 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4376 |
"Dead code\n" + |
4377 |
"----------\n" + |
4378 |
"2. WARNING in X.java (at line 13)\n" + |
4379 |
" } else {\n" + |
4380 |
" System.out.print(42);\n" + |
4381 |
" }\n" + |
4382 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
4383 |
"Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally\n" + |
4384 |
"----------\n", |
4385 |
null, |
4386 |
true, |
4387 |
options); |
4388 |
} |
4389 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4390 |
// resource inside t-w-r is re-assigned, shouldn't even record an errorLocation |
4391 |
public void test056s() { |
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 |
" try (FileReader fileReader = new FileReader(file);) {\n" + |
4406 |
" char[] in = new char[50];\n" + |
4407 |
" fileReader.read(in);\n" + |
4408 |
" fileReader = new FileReader(file); // debug here\n" + |
4409 |
" fileReader.read(in);\n" + |
4410 |
" }\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 |
" fileReader = new FileReader(file); // debug here\n" + |
4420 |
" ^^^^^^^^^^\n" + |
4421 |
"The resource fileReader of a try-with-resources statement cannot be assigned\n" + |
4422 |
"----------\n", |
4423 |
null, |
4424 |
true, |
4425 |
options); |
4426 |
} |
3383 |
public static Class testClass() { |
4427 |
public static Class testClass() { |
3384 |
return TryWithResourcesStatementTest.class; |
4428 |
return TryWithResourcesStatementTest.class; |
3385 |
} |
4429 |
} |