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