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[] { "test014" }; |
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 properly. |
3392 |
public void test055a() { |
3393 |
Map options = getCompilerOptions(); |
3394 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3395 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3396 |
this.runConformTest( |
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 |
"caught", /*output*/ |
3420 |
null/*classLibs*/, |
3421 |
true/*shouldFlush*/, |
3422 |
null/*vmargs*/, |
3423 |
options, |
3424 |
null/*requestor*/); |
3425 |
} |
3426 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3427 |
// a method uses an AutoCloseable properly within try-with-resources. |
3428 |
public void test055b() { |
3429 |
Map options = getCompilerOptions(); |
3430 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3431 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
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 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
3442 |
" char[] in = new char[50];\n" + |
3443 |
" fileReader.read(in);\n" + |
3444 |
" }\n" + |
3445 |
" }\n" + |
3446 |
" public static void main(String[] args) {\n" + |
3447 |
" try {\n" + |
3448 |
" new X().foo();\n" + |
3449 |
" } catch (IOException ioex) {\n" + |
3450 |
" System.out.println(\"caught\");\n" + |
3451 |
" }\n" + |
3452 |
" }\n" + |
3453 |
"}\n" |
3454 |
}, |
3455 |
"caught", /*output*/ |
3456 |
null/*classLibs*/, |
3457 |
true/*shouldFlush*/, |
3458 |
null/*vmargs*/, |
3459 |
options, |
3460 |
null/*requestor*/); |
3461 |
} |
3462 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3463 |
// a method uses two AutoCloseables (testing independent analysis) |
3464 |
// - one closeable may be unclosed at a conditional return |
3465 |
// - the other is only conditionally closed |
3466 |
public void test055c() { |
3467 |
Map options = getCompilerOptions(); |
3468 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3469 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3470 |
this.runNegativeTest( |
3471 |
new String[] { |
3472 |
"X.java", |
3473 |
"import java.io.File;\n" + |
3474 |
"import java.io.FileReader;\n" + |
3475 |
"import java.io.IOException;\n" + |
3476 |
"public class X {\n" + |
3477 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
3478 |
" File file = new File(\"somefile\");\n" + |
3479 |
" char[] in = new char[50];\n" + |
3480 |
" FileReader fileReader1 = new FileReader(file);\n" + |
3481 |
" fileReader1.read(in);\n" + |
3482 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3483 |
" fileReader2.read(in);\n" + |
3484 |
" if (flag1) {\n" + |
3485 |
" fileReader2.close();\n" + |
3486 |
" return;\n" + |
3487 |
" } else if (flag2) {\n" + |
3488 |
" fileReader2.close();\n" + |
3489 |
" }\n" + |
3490 |
" fileReader1.close();\n" + |
3491 |
" }\n" + |
3492 |
" public static void main(String[] args) throws IOException {\n" + |
3493 |
" new X().foo(false, true);\n" + |
3494 |
" }\n" + |
3495 |
"}\n" |
3496 |
}, |
3497 |
"----------\n" + |
3498 |
"1. WARNING in X.java (at line 10)\n" + |
3499 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3500 |
" ^^^^^^^^^^^\n" + |
3501 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3502 |
"----------\n" + |
3503 |
"2. ERROR in X.java (at line 14)\n" + |
3504 |
" return;\n" + |
3505 |
" ^^^^^^^\n" + |
3506 |
"Instance \'fileReader1\' of type AutoCloseable is not closed at this point.\n" + |
3507 |
"----------\n", |
3508 |
null, |
3509 |
true, |
3510 |
options); |
3511 |
} |
3512 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3513 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3514 |
public void test055d() { |
3515 |
Map options = getCompilerOptions(); |
3516 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3517 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3518 |
this.runNegativeTest( |
3519 |
new String[] { |
3520 |
"X.java", |
3521 |
"import java.io.File;\n" + |
3522 |
"import java.io.FileReader;\n" + |
3523 |
"import java.io.IOException;\n" + |
3524 |
"public class X {\n" + |
3525 |
" FileReader getReader(String filename) throws IOException {\n" + |
3526 |
" File file = new File(\"somefile\");\n" + |
3527 |
" FileReader fileReader = new FileReader(file);\n" + |
3528 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
3529 |
" }\n" + |
3530 |
" void foo() throws IOException {\n" + |
3531 |
" FileReader reader = getReader(\"somefile\");\n" + |
3532 |
" char[] in = new char[50];\n" + |
3533 |
" reader.read(in);\n" + |
3534 |
" }\n" + |
3535 |
" public static void main(String[] args) throws IOException {\n" + |
3536 |
" new X().foo();\n" + |
3537 |
" }\n" + |
3538 |
"}\n" |
3539 |
}, |
3540 |
"----------\n" + |
3541 |
"1. ERROR in X.java (at line 11)\n" + |
3542 |
" FileReader reader = getReader(\"somefile\");\n" + |
3543 |
" ^^^^^^\n" + |
3544 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3545 |
"----------\n", |
3546 |
null, |
3547 |
true, |
3548 |
options); |
3549 |
} |
3550 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3551 |
// a method explicitly closes its AutoCloseable rather than using t-w-r |
3552 |
public void test055e() { |
3553 |
Map options = getCompilerOptions(); |
3554 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3555 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3556 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3557 |
this.runNegativeTest( |
3558 |
new String[] { |
3559 |
"X.java", |
3560 |
"import java.io.File;\n" + |
3561 |
"import java.io.FileReader;\n" + |
3562 |
"import java.io.IOException;\n" + |
3563 |
"public class X {\n" + |
3564 |
" void foo() throws IOException {\n" + |
3565 |
" File file = new File(\"somefile\");\n" + |
3566 |
" FileReader fileReader;\n" + |
3567 |
" fileReader = new FileReader(file);\n" + |
3568 |
" char[] in = new char[50];\n" + |
3569 |
" fileReader.read(in);\n" + |
3570 |
" fileReader.close();\n" + |
3571 |
" }\n" + |
3572 |
" public static void main(String[] args) throws IOException {\n" + |
3573 |
" new X().foo();\n" + |
3574 |
" }\n" + |
3575 |
"}\n" |
3576 |
}, |
3577 |
"----------\n" + |
3578 |
"1. ERROR in X.java (at line 8)\n" + |
3579 |
" fileReader = new FileReader(file);\n" + |
3580 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3581 |
"Instance \'fileReader\' should be managed by try-with-resource.\n" + |
3582 |
"----------\n", |
3583 |
null, |
3584 |
true, |
3585 |
options); |
3586 |
} |
3587 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3588 |
// an AutoCloseable local is re-assigned |
3589 |
public void test055f() { |
3590 |
Map options = getCompilerOptions(); |
3591 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3592 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3593 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3594 |
this.runNegativeTest( |
3595 |
new String[] { |
3596 |
"X.java", |
3597 |
"import java.io.File;\n" + |
3598 |
"import java.io.FileReader;\n" + |
3599 |
"import java.io.IOException;\n" + |
3600 |
"public class X {\n" + |
3601 |
" void foo() throws IOException {\n" + |
3602 |
" File file = new File(\"somefile\");\n" + |
3603 |
" FileReader fileReader = new FileReader(file);\n" + |
3604 |
" char[] in = new char[50];\n" + |
3605 |
" fileReader.read(in);\n" + |
3606 |
" fileReader = new FileReader(file);\n" + |
3607 |
" fileReader.read(in);\n" + |
3608 |
" fileReader.close();\n" + |
3609 |
" }\n" + |
3610 |
" public static void main(String[] args) throws IOException {\n" + |
3611 |
" new X().foo();\n" + |
3612 |
" }\n" + |
3613 |
"}\n" |
3614 |
}, |
3615 |
"----------\n" + |
3616 |
"1. ERROR in X.java (at line 10)\n" + |
3617 |
" fileReader = new FileReader(file);\n" + |
3618 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3619 |
"Instance \'fileReader\' of type AutoCloseable is not closed at this point.\n" + |
3620 |
"----------\n", |
3621 |
null, |
3622 |
true, |
3623 |
options); |
3624 |
} |
3344 |
public static Class testClass() { |
3625 |
public static Class testClass() { |
3345 |
return TryWithResourcesStatementTest.class; |
3626 |
return TryWithResourcesStatementTest.class; |
3346 |
} |
3627 |
} |