Lines 16-28
Link Here
|
16 |
|
16 |
|
17 |
import java.util.Map; |
17 |
import java.util.Map; |
18 |
|
18 |
|
|
|
19 |
import org.eclipse.jdt.core.JavaCore; |
19 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
20 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
20 |
|
21 |
|
21 |
import junit.framework.Test; |
22 |
import junit.framework.Test; |
22 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
23 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
23 |
|
24 |
|
24 |
static { |
25 |
static { |
25 |
// TESTS_NAMES = new String[] { "test053" }; |
26 |
// TESTS_NAMES = new String[] { "test055" }; |
26 |
// TESTS_NUMBERS = new int[] { 50 }; |
27 |
// TESTS_NUMBERS = new int[] { 50 }; |
27 |
// TESTS_RANGE = new int[] { 11, -1 }; |
28 |
// TESTS_RANGE = new int[] { 11, -1 }; |
28 |
} |
29 |
} |
Lines 489-500
Link Here
|
489 |
" ^^\n" + |
490 |
" ^^\n" + |
490 |
"Dead code\n" + |
491 |
"Dead code\n" + |
491 |
"----------\n" + |
492 |
"----------\n" + |
492 |
"3. ERROR in X.java (at line 5)\n" + |
493 |
"3. WARNING in X.java (at line 5)\n" + |
|
|
494 |
" Y why = new Y();\n" + |
495 |
" ^^^\n" + |
496 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
497 |
"----------\n" + |
498 |
"4. ERROR in X.java (at line 5)\n" + |
493 |
" Y why = new Y();\n" + |
499 |
" Y why = new Y();\n" + |
494 |
" ^^^^^^^\n" + |
500 |
" ^^^^^^^\n" + |
495 |
"Unhandled exception type WeirdException\n" + |
501 |
"Unhandled exception type WeirdException\n" + |
496 |
"----------\n" + |
502 |
"----------\n" + |
497 |
"4. WARNING in X.java (at line 22)\n" + |
503 |
"5. WARNING in X.java (at line 22)\n" + |
498 |
" class WeirdException extends Throwable {}\n" + |
504 |
" class WeirdException extends Throwable {}\n" + |
499 |
" ^^^^^^^^^^^^^^\n" + |
505 |
" ^^^^^^^^^^^^^^\n" + |
500 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
506 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
Lines 562-573
Link Here
|
562 |
" ^^\n" + |
568 |
" ^^\n" + |
563 |
"Dead code\n" + |
569 |
"Dead code\n" + |
564 |
"----------\n" + |
570 |
"----------\n" + |
565 |
"3. ERROR in X.java (at line 5)\n" + |
571 |
"3. WARNING in X.java (at line 5)\n" + |
|
|
572 |
" Y why = new Y();\n" + |
573 |
" ^^^\n" + |
574 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
575 |
"----------\n" + |
576 |
"4. ERROR in X.java (at line 5)\n" + |
566 |
" Y why = new Y();\n" + |
577 |
" Y why = new Y();\n" + |
567 |
" ^^^^^^^\n" + |
578 |
" ^^^^^^^\n" + |
568 |
"Unhandled exception type WeirdException\n" + |
579 |
"Unhandled exception type WeirdException\n" + |
569 |
"----------\n" + |
580 |
"----------\n" + |
570 |
"4. WARNING in X.java (at line 20)\n" + |
581 |
"5. WARNING in X.java (at line 20)\n" + |
571 |
" class WeirdException extends Throwable {}\n" + |
582 |
" class WeirdException extends Throwable {}\n" + |
572 |
" ^^^^^^^^^^^^^^\n" + |
583 |
" ^^^^^^^^^^^^^^\n" + |
573 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
584 |
"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + |
Lines 3341-3346
Link Here
|
3341 |
"Object.Integer cannot be resolved to a type\n" + |
3352 |
"Object.Integer cannot be resolved to a type\n" + |
3342 |
"----------\n"); |
3353 |
"----------\n"); |
3343 |
} |
3354 |
} |
|
|
3355 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3356 |
// a method uses an AutoCloseable without ever closing it. |
3357 |
public void test055() { |
3358 |
Map options = getCompilerOptions(); |
3359 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3360 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3361 |
this.runNegativeTest( |
3362 |
new String[] { |
3363 |
"X.java", |
3364 |
"import java.io.File;\n" + |
3365 |
"import java.io.FileReader;\n" + |
3366 |
"import java.io.IOException;\n" + |
3367 |
"public class X {\n" + |
3368 |
" void foo() throws IOException {\n" + |
3369 |
" File file = new File(\"somefile\");\n" + |
3370 |
" FileReader fileReader = new FileReader(file);\n" + |
3371 |
// not invoking any methods on FileReader, try to avoid necessary call to superclass() |
3372 |
// " char[] in = new char[50];\n" + |
3373 |
// " fileReader.read(in);\n" + |
3374 |
" }\n" + |
3375 |
" public static void main(String[] args) throws IOException {\n" + |
3376 |
" new X().foo();\n" + |
3377 |
" }\n" + |
3378 |
"}\n" |
3379 |
}, |
3380 |
"----------\n" + |
3381 |
"1. ERROR in X.java (at line 7)\n" + |
3382 |
" FileReader fileReader = new FileReader(file);\n" + |
3383 |
" ^^^^^^^^^^\n" + |
3384 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3385 |
"----------\n", |
3386 |
null, |
3387 |
true, |
3388 |
options); |
3389 |
} |
3390 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3391 |
// a method uses an AutoCloseable and closes it but exception may occur earlier. |
3392 |
public void test055a() { |
3393 |
Map options = getCompilerOptions(); |
3394 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3395 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3396 |
this.runNegativeTest( |
3397 |
new String[] { |
3398 |
"X.java", |
3399 |
"import java.io.File;\n" + |
3400 |
"import java.io.FileReader;\n" + |
3401 |
"import java.io.IOException;\n" + |
3402 |
"public class X {\n" + |
3403 |
" void foo() throws IOException {\n" + |
3404 |
" File file = new File(\"somefile\");\n" + |
3405 |
" FileReader fileReader = new FileReader(file);\n" + |
3406 |
" char[] in = new char[50];\n" + |
3407 |
" fileReader.read(in);\n" + |
3408 |
" fileReader.close();\n" + |
3409 |
" }\n" + |
3410 |
" public static void main(String[] args) {\n" + |
3411 |
" try {\n" + |
3412 |
" new X().foo();\n" + |
3413 |
" } catch (IOException ioex) {\n" + |
3414 |
" System.out.println(\"caught\");\n" + |
3415 |
" }\n" + |
3416 |
" }\n" + |
3417 |
"}\n" |
3418 |
}, |
3419 |
"----------\n" + |
3420 |
"1. ERROR in X.java (at line 7)\n" + |
3421 |
" FileReader fileReader = new FileReader(file);\n" + |
3422 |
" ^^^^^^^^^^\n" + |
3423 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3424 |
"----------\n", |
3425 |
null, |
3426 |
true, |
3427 |
options); |
3428 |
} |
3429 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3430 |
// a method uses an AutoCloseable and closes it properly in a finally block |
3431 |
public void test055b() { |
3432 |
Map options = getCompilerOptions(); |
3433 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3434 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3435 |
this.runConformTest( |
3436 |
new String[] { |
3437 |
"X.java", |
3438 |
"import java.io.File;\n" + |
3439 |
"import java.io.FileReader;\n" + |
3440 |
"import java.io.IOException;\n" + |
3441 |
"public class X {\n" + |
3442 |
" void foo() throws IOException {\n" + |
3443 |
" File file = new File(\"somefile\");\n" + |
3444 |
" FileReader fileReader = new FileReader(file);\n" + |
3445 |
" try {\n" + |
3446 |
" char[] in = new char[50];\n" + |
3447 |
" fileReader.read(in);\n" + |
3448 |
" } finally {\n" + |
3449 |
" fileReader.close();\n" + |
3450 |
" }\n" + |
3451 |
" }\n" + |
3452 |
" public static void main(String[] args) {\n" + |
3453 |
" try {\n" + |
3454 |
" new X().foo();\n" + |
3455 |
" } catch (IOException ioex) {\n" + |
3456 |
" System.out.println(\"caught\");\n" + |
3457 |
" }\n" + |
3458 |
" }\n" + |
3459 |
"}\n" |
3460 |
}, |
3461 |
"caught", /*output*/ |
3462 |
null/*classLibs*/, |
3463 |
true/*shouldFlush*/, |
3464 |
null/*vmargs*/, |
3465 |
options, |
3466 |
null/*requestor*/); |
3467 |
} |
3468 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3469 |
// a method uses an AutoCloseable properly within try-with-resources. |
3470 |
public void test055c() { |
3471 |
Map options = getCompilerOptions(); |
3472 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3473 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3474 |
this.runConformTest( |
3475 |
new String[] { |
3476 |
"X.java", |
3477 |
"import java.io.File;\n" + |
3478 |
"import java.io.FileReader;\n" + |
3479 |
"import java.io.IOException;\n" + |
3480 |
"public class X {\n" + |
3481 |
" void foo() throws IOException {\n" + |
3482 |
" File file = new File(\"somefile\");\n" + |
3483 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
3484 |
" char[] in = new char[50];\n" + |
3485 |
" fileReader.read(in);\n" + |
3486 |
" }\n" + |
3487 |
" }\n" + |
3488 |
" public static void main(String[] args) {\n" + |
3489 |
" try {\n" + |
3490 |
" new X().foo();\n" + |
3491 |
" } catch (IOException ioex) {\n" + |
3492 |
" System.out.println(\"caught\");\n" + |
3493 |
" }\n" + |
3494 |
" }\n" + |
3495 |
"}\n" |
3496 |
}, |
3497 |
"caught", /*output*/ |
3498 |
null/*classLibs*/, |
3499 |
true/*shouldFlush*/, |
3500 |
null/*vmargs*/, |
3501 |
options, |
3502 |
null/*requestor*/); |
3503 |
} |
3504 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3505 |
// a method uses two AutoCloseables (testing independent analysis) |
3506 |
// - one closeable may be unclosed at a conditional return |
3507 |
// - the other is only conditionally closed |
3508 |
public void test055d() { |
3509 |
Map options = getCompilerOptions(); |
3510 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3511 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3512 |
this.runNegativeTest( |
3513 |
new String[] { |
3514 |
"X.java", |
3515 |
"import java.io.File;\n" + |
3516 |
"import java.io.FileReader;\n" + |
3517 |
"import java.io.IOException;\n" + |
3518 |
"public class X {\n" + |
3519 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
3520 |
" File file = new File(\"somefile\");\n" + |
3521 |
" char[] in = new char[50];\n" + |
3522 |
" FileReader fileReader1 = new FileReader(file);\n" + |
3523 |
" fileReader1.read(in);\n" + |
3524 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3525 |
" fileReader2.read(in);\n" + |
3526 |
" if (flag1) {\n" + |
3527 |
" fileReader2.close();\n" + |
3528 |
" return;\n" + |
3529 |
" } else if (flag2) {\n" + |
3530 |
" fileReader2.close();\n" + |
3531 |
" }\n" + |
3532 |
" fileReader1.close();\n" + |
3533 |
" }\n" + |
3534 |
" public static void main(String[] args) throws IOException {\n" + |
3535 |
" new X().foo(false, true);\n" + |
3536 |
" }\n" + |
3537 |
"}\n" |
3538 |
}, |
3539 |
"----------\n" + |
3540 |
"1. WARNING in X.java (at line 10)\n" + |
3541 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3542 |
" ^^^^^^^^^^^\n" + |
3543 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3544 |
"----------\n" + |
3545 |
"2. ERROR in X.java (at line 14)\n" + |
3546 |
" return;\n" + |
3547 |
" ^^^^^^^\n" + |
3548 |
"Instance \'fileReader1\' of type AutoCloseable is not closed at this point.\n" + |
3549 |
"----------\n", |
3550 |
null, |
3551 |
true, |
3552 |
options); |
3553 |
} |
3554 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3555 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3556 |
public void test055e() { |
3557 |
Map options = getCompilerOptions(); |
3558 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3559 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3560 |
this.runNegativeTest( |
3561 |
new String[] { |
3562 |
"X.java", |
3563 |
"import java.io.File;\n" + |
3564 |
"import java.io.FileReader;\n" + |
3565 |
"import java.io.IOException;\n" + |
3566 |
"public class X {\n" + |
3567 |
" FileReader getReader(String filename) throws IOException {\n" + |
3568 |
" File file = new File(\"somefile\");\n" + |
3569 |
" FileReader fileReader = new FileReader(file);\n" + |
3570 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
3571 |
" }\n" + |
3572 |
" void foo() throws IOException {\n" + |
3573 |
" FileReader reader = getReader(\"somefile\");\n" + |
3574 |
" char[] in = new char[50];\n" + |
3575 |
" reader.read(in);\n" + |
3576 |
" }\n" + |
3577 |
" public static void main(String[] args) throws IOException {\n" + |
3578 |
" new X().foo();\n" + |
3579 |
" }\n" + |
3580 |
"}\n" |
3581 |
}, |
3582 |
"----------\n" + |
3583 |
"1. ERROR in X.java (at line 11)\n" + |
3584 |
" FileReader reader = getReader(\"somefile\");\n" + |
3585 |
" ^^^^^^\n" + |
3586 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3587 |
"----------\n", |
3588 |
null, |
3589 |
true, |
3590 |
options); |
3591 |
} |
3592 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3593 |
// a method explicitly closes its AutoCloseable rather than using t-w-r |
3594 |
public void test055f() { |
3595 |
Map options = getCompilerOptions(); |
3596 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3597 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3598 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3599 |
this.runNegativeTest( |
3600 |
new String[] { |
3601 |
"X.java", |
3602 |
"import java.io.File;\n" + |
3603 |
"import java.io.FileReader;\n" + |
3604 |
"import java.io.IOException;\n" + |
3605 |
"public class X {\n" + |
3606 |
" void foo() throws IOException {\n" + |
3607 |
" File file = new File(\"somefile\");\n" + |
3608 |
" FileReader fileReader = null;\n" + |
3609 |
" try {\n" + |
3610 |
" fileReader = new FileReader(file);\n" + |
3611 |
" char[] in = new char[50];\n" + |
3612 |
" fileReader.read(in);\n" + |
3613 |
" } finally {\n" + |
3614 |
" fileReader.close();\n" + |
3615 |
" }\n" + |
3616 |
" }\n" + |
3617 |
" public static void main(String[] args) throws IOException {\n" + |
3618 |
" new X().foo();\n" + |
3619 |
" }\n" + |
3620 |
"}\n" |
3621 |
}, |
3622 |
"----------\n" + |
3623 |
"1. ERROR in X.java (at line 7)\n" + |
3624 |
" FileReader fileReader = null;\n" + |
3625 |
" ^^^^^^^^^^\n" + |
3626 |
"Instance \'fileReader\' should be managed by try-with-resource.\n" + |
3627 |
"----------\n", |
3628 |
null, |
3629 |
true, |
3630 |
options); |
3631 |
} |
3632 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3633 |
// an AutoCloseable local is re-assigned |
3634 |
public void test055g() { |
3635 |
Map options = getCompilerOptions(); |
3636 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3637 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3638 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3639 |
this.runNegativeTest( |
3640 |
new String[] { |
3641 |
"X.java", |
3642 |
"import java.io.File;\n" + |
3643 |
"import java.io.FileReader;\n" + |
3644 |
"import java.io.IOException;\n" + |
3645 |
"public class X {\n" + |
3646 |
" void foo() throws IOException {\n" + |
3647 |
" File file = new File(\"somefile\");\n" + |
3648 |
" FileReader fileReader = new FileReader(file);\n" + |
3649 |
" char[] in = new char[50];\n" + |
3650 |
" fileReader.read(in);\n" + |
3651 |
" fileReader = new FileReader(file);\n" + |
3652 |
" fileReader.read(in);\n" + |
3653 |
" fileReader.close();\n" + |
3654 |
" }\n" + |
3655 |
" public static void main(String[] args) throws IOException {\n" + |
3656 |
" new X().foo();\n" + |
3657 |
" }\n" + |
3658 |
"}\n" |
3659 |
}, |
3660 |
"----------\n" + |
3661 |
"1. ERROR in X.java (at line 10)\n" + |
3662 |
" fileReader = new FileReader(file);\n" + |
3663 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3664 |
"Instance \'fileReader\' of type AutoCloseable is not closed at this point.\n" + |
3665 |
"----------\n", |
3666 |
null, |
3667 |
true, |
3668 |
options); |
3669 |
} |
3670 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3671 |
// two AutoCloseables at different nesting levels (anonymous local type) |
3672 |
public void test055h() { |
3673 |
Map options = getCompilerOptions(); |
3674 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3675 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3676 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3677 |
this.runNegativeTest( |
3678 |
new String[] { |
3679 |
"X.java", |
3680 |
"import java.io.File;\n" + |
3681 |
"import java.io.FileReader;\n" + |
3682 |
"import java.io.IOException;\n" + |
3683 |
"public class X {\n" + |
3684 |
" void foo() throws IOException {\n" + |
3685 |
" final File file = new File(\"somefile\");\n" + |
3686 |
" final FileReader fileReader = new FileReader(file);\n" + |
3687 |
" char[] in = new char[50];\n" + |
3688 |
" fileReader.read(in);\n" + |
3689 |
" new Runnable() {\n public void run() {\n" + |
3690 |
" try {\n" + |
3691 |
" fileReader.close();\n" + |
3692 |
" FileReader localReader = new FileReader(file);\n" + |
3693 |
" } catch (IOException ex) { /* nop */ }\n" + |
3694 |
" }}.run();\n" + |
3695 |
" }\n" + |
3696 |
" public static void main(String[] args) throws IOException {\n" + |
3697 |
" new X().foo();\n" + |
3698 |
" }\n" + |
3699 |
"}\n" |
3700 |
}, |
3701 |
"----------\n" + |
3702 |
"1. WARNING in X.java (at line 7)\n" + |
3703 |
" final FileReader fileReader = new FileReader(file);\n" + |
3704 |
" ^^^^^^^^^^\n" + |
3705 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3706 |
"----------\n" + |
3707 |
"2. ERROR in X.java (at line 14)\n" + |
3708 |
" FileReader localReader = new FileReader(file);\n" + |
3709 |
" ^^^^^^^^^^^\n" + |
3710 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3711 |
"----------\n", |
3712 |
null, |
3713 |
true, |
3714 |
options); |
3715 |
} |
3716 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3717 |
// three AutoCloseables in different blocks of the same method |
3718 |
public void test055i() { |
3719 |
Map options = getCompilerOptions(); |
3720 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3721 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3722 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3723 |
this.runNegativeTest( |
3724 |
new String[] { |
3725 |
"X.java", |
3726 |
"import java.io.File;\n" + |
3727 |
"import java.io.FileReader;\n" + |
3728 |
"import java.io.IOException;\n" + |
3729 |
"public class X {\n" + |
3730 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
3731 |
" File file = new File(\"somefile\");\n" + |
3732 |
" if (f1) {\n" + |
3733 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
3734 |
" char[] in = new char[50];\n" + |
3735 |
" fileReader.read(in);\n" + |
3736 |
" while (true) {\n" + |
3737 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
3738 |
" loopReader.close();" + |
3739 |
" break;\n" + |
3740 |
" }\n" + |
3741 |
" } else {\n" + |
3742 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3743 |
" if (f2)\n" + |
3744 |
" fileReader.close();\n" + |
3745 |
" }\n" + |
3746 |
" }\n" + |
3747 |
" public static void main(String[] args) throws IOException {\n" + |
3748 |
" new X().foo(true, true);\n" + |
3749 |
" }\n" + |
3750 |
"}\n" |
3751 |
}, |
3752 |
"----------\n" + |
3753 |
"1. ERROR in X.java (at line 8)\n" + |
3754 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
3755 |
" ^^^^^^^^^^\n" + |
3756 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3757 |
"----------\n" + |
3758 |
"2. WARNING in X.java (at line 16)\n" + |
3759 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
3760 |
" ^^^^^^^^^^\n" + |
3761 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3762 |
"----------\n", |
3763 |
null, |
3764 |
true, |
3765 |
options); |
3766 |
} |
3767 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3768 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
3769 |
public void test055j() { |
3770 |
Map options = getCompilerOptions(); |
3771 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3772 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3773 |
this.runNegativeTest( |
3774 |
new String[] { |
3775 |
"X.java", |
3776 |
"import java.io.File;\n" + |
3777 |
"import java.io.FileReader;\n" + |
3778 |
"import java.io.IOException;\n" + |
3779 |
"public class X {\n" + |
3780 |
" void foo() throws IOException {\n" + |
3781 |
" File file = new File(\"somefile\");\n" + |
3782 |
" FileReader fileReader = new FileReader(file);\n" + |
3783 |
" read(fileReader);\n" + |
3784 |
" }\n" + |
3785 |
" void read(FileReader reader) { }\n" + |
3786 |
" public static void main(String[] args) throws IOException {\n" + |
3787 |
" new X().foo();\n" + |
3788 |
" }\n" + |
3789 |
"}\n" |
3790 |
}, |
3791 |
"----------\n" + |
3792 |
"1. ERROR in X.java (at line 7)\n" + |
3793 |
" FileReader fileReader = new FileReader(file);\n" + |
3794 |
" ^^^^^^^^^^\n" + |
3795 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3796 |
"----------\n", |
3797 |
null, |
3798 |
true, |
3799 |
options); |
3800 |
} |
3801 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3802 |
// many locals, some are AutoCloseable. |
3803 |
// Unfortunately analysis cannot respect how exception exits may affect ra3 and rb3, |
3804 |
// doing so would create false positives. |
3805 |
public void test055k() { |
3806 |
Map options = getCompilerOptions(); |
3807 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3808 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3809 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3810 |
this.runNegativeTest( |
3811 |
new String[] { |
3812 |
"X.java", |
3813 |
"import java.io.File;\n" + |
3814 |
"import java.io.FileReader;\n" + |
3815 |
"import java.io.IOException;\n" + |
3816 |
"public class X {\n" + |
3817 |
" void foo() throws IOException {\n" + |
3818 |
" int i01, i02, i03, i04, i05, i06, i07, i08, i09,\n" + |
3819 |
" i11, i12, i13, i14, i15, i16, i17, i18, i19,\n" + |
3820 |
" i21, i22, i23, i24, i25, i26, i27, i28, i29,\n" + |
3821 |
" i31, i32, i33, i34, i35, i36, i37, i38, i39,\n" + |
3822 |
" i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" + |
3823 |
" File file = new File(\"somefile\");\n" + |
3824 |
" FileReader ra1 = null, ra2 = null;\n" + |
3825 |
" try {\n" + |
3826 |
" ra1 = new FileReader(file);\n" + |
3827 |
" ra2 = new FileReader(file);\n" + |
3828 |
" FileReader ra3 = new FileReader(file);\n" + |
3829 |
" char[] in = new char[50];\n" + |
3830 |
" ra1.read(in);\n" + |
3831 |
" ra2.read(in);\n" + |
3832 |
" ra3.close();\n" + |
3833 |
" } finally {\n" + |
3834 |
" ra1.close();\n" + |
3835 |
" }\n" + |
3836 |
" int i51, i52, i53, i54, i55, i56, i57, i58, i59, i60;\n" + // beyond this point locals are analyzed using extraBits |
3837 |
" FileReader rb1 = null, rb2 = null;\n" + |
3838 |
" try {\n" + |
3839 |
" rb1 = new FileReader(file);\n" + |
3840 |
" rb2 = new FileReader(file);\n" + |
3841 |
" FileReader rb3 = new FileReader(file);\n" + |
3842 |
" char[] in = new char[50];\n" + |
3843 |
" rb1.read(in);\n" + |
3844 |
" rb2.read(in);\n" + |
3845 |
" rb3.close();\n" + |
3846 |
" } finally {\n" + |
3847 |
" rb1.close();\n" + |
3848 |
" }\n" + |
3849 |
" }\n" + |
3850 |
" public static void main(String[] args) throws IOException {\n" + |
3851 |
" new X().foo();\n" + |
3852 |
" }\n" + |
3853 |
"}\n" |
3854 |
}, |
3855 |
"----------\n" + |
3856 |
"1. ERROR in X.java (at line 12)\n" + |
3857 |
" FileReader ra1 = null, ra2 = null;\n" + |
3858 |
" ^^^\n" + |
3859 |
"Instance \'ra1\' should be managed by try-with-resource.\n" + |
3860 |
"----------\n" + |
3861 |
"2. ERROR in X.java (at line 15)\n" + |
3862 |
" ra2 = new FileReader(file);\n" + |
3863 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3864 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3865 |
"----------\n" + |
3866 |
"3. ERROR in X.java (at line 16)\n" + |
3867 |
" FileReader ra3 = new FileReader(file);\n" + |
3868 |
" ^^^\n" + |
3869 |
"Instance \'ra3\' should be managed by try-with-resource.\n" + |
3870 |
"----------\n" + |
3871 |
"4. ERROR in X.java (at line 25)\n" + |
3872 |
" FileReader rb1 = null, rb2 = null;\n" + |
3873 |
" ^^^\n" + |
3874 |
"Instance \'rb1\' should be managed by try-with-resource.\n" + |
3875 |
"----------\n" + |
3876 |
"5. ERROR in X.java (at line 28)\n" + |
3877 |
" rb2 = new FileReader(file);\n" + |
3878 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3879 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3880 |
"----------\n" + |
3881 |
"6. ERROR in X.java (at line 29)\n" + |
3882 |
" FileReader rb3 = new FileReader(file);\n" + |
3883 |
" ^^^\n" + |
3884 |
"Instance \'rb3\' should be managed by try-with-resource.\n" + |
3885 |
"----------\n", |
3886 |
null, |
3887 |
true, |
3888 |
options); |
3889 |
} |
3890 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3891 |
// various non-problems |
3892 |
public void test055l() { |
3893 |
Map options = getCompilerOptions(); |
3894 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3895 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3896 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3897 |
this.runNegativeTest( |
3898 |
new String[] { |
3899 |
"X.java", |
3900 |
"import java.io.File;\n" + |
3901 |
"import java.io.FileReader;\n" + |
3902 |
"import java.io.IOException;\n" + |
3903 |
"public class X {\n" + |
3904 |
" X(FileReader r0) {}\n" + // don't complain against argument |
3905 |
" FileReader getReader() { return null; }\n" + |
3906 |
" void foo(FileReader r1) throws IOException {\n" + |
3907 |
" FileReader fileReader = getReader();\n" + |
3908 |
" if (fileReader == null)\n" + |
3909 |
" return;\n" + // don't complain, resource is actually null |
3910 |
" FileReader r3 = getReader();\n" + |
3911 |
" if (r3 == null)\n" + |
3912 |
" r3 = new FileReader(new File(\"absent\"));\n" + // don't complain, previous resource is actually null |
3913 |
" try {\n" + |
3914 |
" char[] in = new char[50];\n" + |
3915 |
" fileReader.read(in);\n" + |
3916 |
" r1.read(in);\n" + |
3917 |
" } finally {\n" + |
3918 |
" fileReader.close();\n" + |
3919 |
" r3.close();\n" + |
3920 |
" }\n" + |
3921 |
" }\n" + |
3922 |
" public static void main(String[] args) throws IOException {\n" + |
3923 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
3924 |
" new X(r2).foo(new FileReader(new File(\"notthere\")));\n" + |
3925 |
" }\n" + |
3926 |
"}\n" |
3927 |
}, |
3928 |
"----------\n" + |
3929 |
"1. ERROR in X.java (at line 8)\n" + |
3930 |
" FileReader fileReader = getReader();\n" + |
3931 |
" ^^^^^^^^^^\n" + |
3932 |
"Instance \'fileReader\' should be managed by try-with-resource.\n" + |
3933 |
"----------\n" + |
3934 |
"2. ERROR in X.java (at line 11)\n" + |
3935 |
" FileReader r3 = getReader();\n" + |
3936 |
" ^^\n" + |
3937 |
"Instance \'r3\' should be managed by try-with-resource.\n" + |
3938 |
"----------\n" + |
3939 |
"3. ERROR in X.java (at line 24)\n" + |
3940 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
3941 |
" ^^\n" + |
3942 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3943 |
"----------\n", |
3944 |
null, |
3945 |
true, |
3946 |
options); |
3947 |
} |
3948 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3949 |
// nested try with early exit |
3950 |
public void test055m() { |
3951 |
Map options = getCompilerOptions(); |
3952 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3953 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3954 |
this.runConformTest( |
3955 |
new String[] { |
3956 |
"X.java", |
3957 |
"import java.io.File;\n" + |
3958 |
"import java.io.FileReader;\n" + |
3959 |
"import java.io.IOException;\n" + |
3960 |
"public class X {\n" + |
3961 |
" void foo() {\n" + |
3962 |
" File file = new File(\"somefile\");" + |
3963 |
" try {\n" + |
3964 |
" FileReader fileReader = new FileReader(file);\n" + |
3965 |
" try {\n" + |
3966 |
" char[] in = new char[50];\n" + |
3967 |
" if (fileReader.read(in)==0)\n" + |
3968 |
" return;\n" + |
3969 |
" } finally {\n" + |
3970 |
" fileReader.close();\n" + |
3971 |
" }\n" + |
3972 |
" } catch (IOException e) {\n" + |
3973 |
" System.out.println(\"caught\");\n" + |
3974 |
" }\n" + |
3975 |
" }\n" + |
3976 |
" public static void main(String[] args) {\n" + |
3977 |
" new X().foo();\n" + |
3978 |
" }\n" + |
3979 |
"}\n" |
3980 |
}, |
3981 |
"caught", /*output*/ |
3982 |
null/*classLibs*/, |
3983 |
true/*shouldFlush*/, |
3984 |
null/*vmargs*/, |
3985 |
options, |
3986 |
null/*requestor*/); |
3987 |
} |
3988 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3989 |
// nested try should not interfere with earlier analysis. |
3990 |
public void test055n() { |
3991 |
Map options = getCompilerOptions(); |
3992 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3993 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3994 |
this.runConformTest( |
3995 |
new String[] { |
3996 |
"X.java", |
3997 |
"import java.io.File;\n" + |
3998 |
"import java.io.FileReader;\n" + |
3999 |
"import java.io.IOException;\n" + |
4000 |
"import java.io.FileNotFoundException;\n" + |
4001 |
"public class X {\n" + |
4002 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
4003 |
" FileReader fr1 = new FileReader(someFile);\n" + |
4004 |
" try {\n" + |
4005 |
" fr1.read(buf);\n" + |
4006 |
" } finally {\n" + |
4007 |
" fr1.close();\n" + |
4008 |
" }\n" + |
4009 |
" try {\n" + |
4010 |
" FileReader fr3 = new FileReader(someFile);\n" + |
4011 |
" try {\n" + |
4012 |
" } finally {\n" + |
4013 |
" fr3.close();\n" + |
4014 |
" }\n" + |
4015 |
" } catch (IOException e) {\n" + |
4016 |
" }\n" + |
4017 |
" }\n" + |
4018 |
" public static void main(String[] args) throws IOException {\n" + |
4019 |
" try {\n" + |
4020 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
4021 |
" } catch (FileNotFoundException e) {\n" + |
4022 |
" System.out.println(\"caught\");\n" + |
4023 |
" }\n" + |
4024 |
" }\n" + |
4025 |
"}\n" |
4026 |
}, |
4027 |
"caught", /*output*/ |
4028 |
null/*classLibs*/, |
4029 |
true/*shouldFlush*/, |
4030 |
null/*vmargs*/, |
4031 |
options, |
4032 |
null/*requestor*/); |
4033 |
} |
4034 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
4035 |
// if close is guarded by null check this should still be recognized as definitely closed |
4036 |
public void test055o() { |
4037 |
Map options = getCompilerOptions(); |
4038 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
4039 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
4040 |
this.runConformTest( |
4041 |
new String[] { |
4042 |
"X.java", |
4043 |
"import java.io.File;\n" + |
4044 |
"import java.io.FileReader;\n" + |
4045 |
"import java.io.IOException;\n" + |
4046 |
"import java.io.FileNotFoundException;\n" + |
4047 |
"public class X {\n" + |
4048 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
4049 |
" FileReader fr1 = null;\n" + |
4050 |
" try {\n" + |
4051 |
" fr1 = new FileReader(someFile);" + |
4052 |
" fr1.read(buf);\n" + |
4053 |
" } finally {\n" + |
4054 |
" if (fr1 != null)\n" + |
4055 |
" try {\n" + |
4056 |
" fr1.close();\n" + |
4057 |
" } catch (IOException e) { /*do nothing*/ }\n" + |
4058 |
" }\n" + |
4059 |
" }\n" + |
4060 |
" public static void main(String[] args) throws IOException {\n" + |
4061 |
" try {\n" + |
4062 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
4063 |
" } catch (FileNotFoundException e) {\n" + |
4064 |
" System.out.println(\"caught\");\n" + |
4065 |
" }\n" + |
4066 |
" }\n" + |
4067 |
"}\n" |
4068 |
}, |
4069 |
"caught", /*output*/ |
4070 |
null/*classLibs*/, |
4071 |
true/*shouldFlush*/, |
4072 |
null/*vmargs*/, |
4073 |
options, |
4074 |
null/*requestor*/); |
4075 |
} |
3344 |
public static Class testClass() { |
4076 |
public static Class testClass() { |
3345 |
return TryWithResourcesStatementTest.class; |
4077 |
return TryWithResourcesStatementTest.class; |
3346 |
} |
4078 |
} |