Lines 2112-2128
public void test061f2() throws IOException {
Link Here
|
2112 |
"import java.io.IOException;\n" + |
2112 |
"import java.io.IOException;\n" + |
2113 |
"public class X {\n" + |
2113 |
"public class X {\n" + |
2114 |
" void zork() throws IOException {\n" + |
2114 |
" void zork() throws IOException {\n" + |
2115 |
" try {\n" + |
2115 |
" try {\n" + |
2116 |
" OutputStream os = null;\n" + |
2116 |
" OutputStream os = null;\n" + |
2117 |
" try {\n" + |
2117 |
" try {\n" + |
2118 |
" os = new BufferedOutputStream(new FileOutputStream(\"somefile\"));\n" + |
2118 |
" os = new BufferedOutputStream(new FileOutputStream(\"somefile\"));\n" + |
2119 |
" String externalForm = \"externalPath\";\n" + |
2119 |
" String externalForm = \"externalPath\";\n" + |
2120 |
" } finally {\n" + |
2120 |
" } finally {\n" + |
2121 |
" if (os != null)\n" + |
2121 |
" if (os != null)\n" + |
2122 |
" os.close();\n" + |
2122 |
" os.close();\n" + |
2123 |
" }\n" + |
2123 |
" }\n" + |
2124 |
" } catch (IOException e) {\n" + |
2124 |
" } catch (IOException e) {\n" + |
2125 |
" e.printStackTrace();\n" + |
2125 |
" e.printStackTrace();\n" + |
2126 |
" }\n" + |
2126 |
" }\n" + |
2127 |
" }\n" + |
2127 |
" }\n" + |
2128 |
"}\n" |
2128 |
"}\n" |
Lines 2150-2174
public void test061f3() throws IOException {
Link Here
|
2150 |
"import java.io.InputStream;\n" + |
2150 |
"import java.io.InputStream;\n" + |
2151 |
"import java.io.BufferedInputStream;\n" + |
2151 |
"import java.io.BufferedInputStream;\n" + |
2152 |
"public class X {\n" + |
2152 |
"public class X {\n" + |
2153 |
" String loadProfile(File profileFile) {\n" + |
2153 |
" String loadProfile(File profileFile) {\n" + |
2154 |
" try {\n" + |
2154 |
" try {\n" + |
2155 |
" InputStream stream = new BufferedInputStream(new FileInputStream(profileFile));\n" + |
2155 |
" InputStream stream = new BufferedInputStream(new FileInputStream(profileFile));\n" + |
2156 |
" return loadProfile(stream);\n" + |
2156 |
" return loadProfile(stream);\n" + |
2157 |
" } catch (FileNotFoundException e) {\n" + |
2157 |
" } catch (FileNotFoundException e) {\n" + |
2158 |
" //null\n" + |
2158 |
" //null\n" + |
2159 |
" }\n" + |
2159 |
" }\n" + |
2160 |
" return null;\n" + |
2160 |
" return null;\n" + |
2161 |
" }\n" + |
2161 |
" }\n" + |
2162 |
" private String loadProfile(InputStream stream) {\n" + |
2162 |
" private String loadProfile(InputStream stream) {\n" + |
2163 |
" return null;\n" + |
2163 |
" return null;\n" + |
2164 |
" }\n" + |
2164 |
" }\n" + |
2165 |
"}\n" |
2165 |
"}\n" |
2166 |
}, |
2166 |
}, |
2167 |
"----------\n" + |
2167 |
"----------\n" + |
2168 |
"1. ERROR in X.java (at line 10)\n" + |
2168 |
"1. ERROR in X.java (at line 10)\n" + |
2169 |
" return loadProfile(stream);\n" + |
2169 |
" return loadProfile(stream);\n" + |
2170 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2170 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2171 |
"Potential resource leak: \'stream\' may not be closed at this location\n" + |
2171 |
"Potential resource leak: \'stream\' may not be closed at this location\n" + |
2172 |
"----------\n", |
2172 |
"----------\n", |
2173 |
null, |
2173 |
null, |
2174 |
true, |
2174 |
true, |
Lines 2487-2497
public void test061n() {
Link Here
|
2487 |
" }\n" + |
2487 |
" }\n" + |
2488 |
"}\n" |
2488 |
"}\n" |
2489 |
}, |
2489 |
}, |
2490 |
"----------\n" + |
2490 |
"----------\n" + |
2491 |
"1. ERROR in X.java (at line 5)\n" + |
2491 |
"1. ERROR in X.java (at line 5)\n" + |
2492 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2492 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2493 |
" ^^^^^^\n" + |
2493 |
" ^^^^^^\n" + |
2494 |
"Resource leak: \'writer\' is never closed\n" + |
2494 |
"Resource leak: \'writer\' is never closed\n" + |
2495 |
"----------\n", |
2495 |
"----------\n", |
2496 |
null, |
2496 |
null, |
2497 |
true, |
2497 |
true, |
Lines 2511-2525
public void test061o() {
Link Here
|
2511 |
"import java.io.BufferedInputStream;\n" + |
2511 |
"import java.io.BufferedInputStream;\n" + |
2512 |
"import java.io.IOException;\n" + |
2512 |
"import java.io.IOException;\n" + |
2513 |
"public class X {\n" + |
2513 |
"public class X {\n" + |
2514 |
" void foo(boolean bar) throws IOException {\n" + |
2514 |
" void foo(boolean bar) throws IOException {\n" + |
2515 |
" File file = new File(\"somefil\");\n" + |
2515 |
" File file = new File(\"somefil\");\n" + |
2516 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2516 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2517 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
2517 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
2518 |
" if (bar) {\n" + |
2518 |
" if (bar) {\n" + |
2519 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
2519 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
2520 |
" doubleWrap.close();\n" + |
2520 |
" doubleWrap.close();\n" + |
2521 |
" }\n" + |
2521 |
" }\n" + |
2522 |
" }\n" + |
2522 |
" }\n" + |
2523 |
"}\n" |
2523 |
"}\n" |
2524 |
}, |
2524 |
}, |
2525 |
"----------\n" + |
2525 |
"----------\n" + |
Lines 2547-2559
public void test061f4() throws IOException {
Link Here
|
2547 |
"import java.io.InputStream;\n" + |
2547 |
"import java.io.InputStream;\n" + |
2548 |
"import java.io.BufferedInputStream;\n" + |
2548 |
"import java.io.BufferedInputStream;\n" + |
2549 |
"public class X {\n" + |
2549 |
"public class X {\n" + |
2550 |
" void foo(File location, String adviceFilePath) throws FileNotFoundException {\n" + |
2550 |
" void foo(File location, String adviceFilePath) throws FileNotFoundException {\n" + |
2551 |
" InputStream stream = null;\n" + |
2551 |
" InputStream stream = null;\n" + |
2552 |
" if (location.isDirectory()) {\n" + |
2552 |
" if (location.isDirectory()) {\n" + |
2553 |
" File adviceFile = new File(location, adviceFilePath);\n" + |
2553 |
" File adviceFile = new File(location, adviceFilePath);\n" + |
2554 |
" stream = new BufferedInputStream(new FileInputStream(adviceFile));\n" + |
2554 |
" stream = new BufferedInputStream(new FileInputStream(adviceFile));\n" + |
2555 |
" }\n" + |
2555 |
" }\n" + |
2556 |
" }\n" + |
2556 |
" }\n" + |
2557 |
"}\n" |
2557 |
"}\n" |
2558 |
}, |
2558 |
}, |
2559 |
"----------\n" + |
2559 |
"----------\n" + |
Lines 2566-2571
public void test061f4() throws IOException {
Link Here
|
2566 |
true, |
2566 |
true, |
2567 |
options); |
2567 |
options); |
2568 |
} |
2568 |
} |
|
|
2569 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2570 |
// a t-w-r wraps an existing resource |
2571 |
public void test061p() { |
2572 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; |
2573 |
Map options = getCompilerOptions(); |
2574 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2575 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2576 |
this.runConformTest( |
2577 |
new String[] { |
2578 |
"X.java", |
2579 |
"import java.io.PrintWriter;\n" + |
2580 |
"import java.io.BufferedWriter;\n" + |
2581 |
"import java.io.IOException;\n" + |
2582 |
"public class X {\n" + |
2583 |
" void foo() throws IOException {\n" + |
2584 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2585 |
" try (BufferedWriter bw = new BufferedWriter(writer)) {\n" + |
2586 |
" bw.write(1);\n" + |
2587 |
" }\n" + |
2588 |
" }\n" + |
2589 |
"}\n" |
2590 |
}, |
2591 |
"", |
2592 |
null, |
2593 |
true, |
2594 |
null, |
2595 |
options, |
2596 |
null); |
2597 |
} |
2598 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2599 |
// a t-w-r potentially wraps an existing resource |
2600 |
// DISABLED, fails because we currently don't include t-w-r managed resources in the analysis |
2601 |
public void _test061q() { |
2602 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; |
2603 |
Map options = getCompilerOptions(); |
2604 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2605 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2606 |
this.runNegativeTest( |
2607 |
new String[] { |
2608 |
"X.java", |
2609 |
"import java.io.PrintWriter;\n" + |
2610 |
"import java.io.BufferedWriter;\n" + |
2611 |
"import java.io.IOException;\n" + |
2612 |
"public class X {\n" + |
2613 |
" void foo(boolean b) throws IOException {\n" + |
2614 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
2615 |
" if (b)\n" + |
2616 |
" try (BufferedWriter bw = new BufferedWriter(writer)) {\n" + |
2617 |
" bw.write(1);\n" + |
2618 |
" }\n" + |
2619 |
" }\n" + |
2620 |
"}\n" |
2621 |
}, |
2622 |
"----------\n" + |
2623 |
"1. ERROR in X.java (at line 6)\n" + |
2624 |
" PrintWriter writer = new PrintWriter(\\\"filename\\\");\n" + |
2625 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2626 |
"Potential resource leak: \'writer\' may not be closed\n" + |
2627 |
"----------\n", |
2628 |
null, |
2629 |
true, |
2630 |
options); |
2631 |
} |
2632 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2633 |
// the inner from a wrapper is returned |
2634 |
public void test061r() { |
2635 |
Map options = getCompilerOptions(); |
2636 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2637 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2638 |
this.runConformTest( |
2639 |
new String[] { |
2640 |
"X.java", |
2641 |
"import java.io.FileInputStream;\n" + |
2642 |
"import java.io.File;\n" + |
2643 |
"import java.io.BufferedInputStream;\n" + |
2644 |
"import java.io.IOException;\n" + |
2645 |
"public class X {\n" + |
2646 |
" FileInputStream foo() throws IOException {\n" + |
2647 |
" File file = new File(\"somefil\");\n" + |
2648 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2649 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
2650 |
" return fileStream;\n" + |
2651 |
" }\n" + |
2652 |
"}\n" |
2653 |
}, |
2654 |
"", |
2655 |
null, |
2656 |
true, |
2657 |
null, |
2658 |
options, |
2659 |
null); |
2660 |
} |
2661 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
2662 |
// a wrapper is forgotten, the inner is closed afterwards |
2663 |
public void test061s() { |
2664 |
Map options = getCompilerOptions(); |
2665 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
2666 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
2667 |
this.runConformTest( |
2668 |
new String[] { |
2669 |
"X.java", |
2670 |
"import java.io.FileInputStream;\n" + |
2671 |
"import java.io.File;\n" + |
2672 |
"import java.io.BufferedInputStream;\n" + |
2673 |
"import java.io.IOException;\n" + |
2674 |
"public class X {\n" + |
2675 |
" void foo() throws IOException {\n" + |
2676 |
" File file = new File(\"somefil\");\n" + |
2677 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
2678 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
2679 |
" bis = null;\n" + |
2680 |
" fileStream.close();\n" + |
2681 |
" }\n" + |
2682 |
"}\n" |
2683 |
}, |
2684 |
"", |
2685 |
null, |
2686 |
true, |
2687 |
null, |
2688 |
options, |
2689 |
null); |
2690 |
} |
2569 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
2691 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
2570 |
// a resource is never assigned |
2692 |
// a resource is never assigned |
2571 |
public void test062a() throws IOException { |
2693 |
public void test062a() throws IOException { |
Lines 2702-2722
public void test063a() throws IOException {
Link Here
|
2702 |
" }\n" + |
2824 |
" }\n" + |
2703 |
"}\n" |
2825 |
"}\n" |
2704 |
}, |
2826 |
}, |
2705 |
"----------\n" + |
2827 |
"----------\n" + |
2706 |
"1. ERROR in X.java (at line 7)\n" + |
2828 |
"1. ERROR in X.java (at line 7)\n" + |
2707 |
" FileInputStream stream = new FileInputStream(file);\n" + |
2829 |
" FileInputStream stream = new FileInputStream(file);\n" + |
2708 |
" ^^^^^^\n" + |
2830 |
" ^^^^^^\n" + |
2709 |
"Resource leak: \'stream\' is never closed\n" + |
2831 |
"Resource leak: \'stream\' is never closed\n" + |
2710 |
"----------\n" + |
2832 |
"----------\n" + |
2711 |
"2. ERROR in X.java (at line 9)\n" + |
2833 |
"2. ERROR in X.java (at line 9)\n" + |
2712 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
2834 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
2713 |
" ^^^^^^^\n" + |
2835 |
" ^^^^^^^\n" + |
2714 |
"Potential resource leak: \'stream2\' may not be closed\n" + |
2836 |
"Potential resource leak: \'stream2\' may not be closed\n" + |
2715 |
"----------\n" + |
2837 |
"----------\n" + |
2716 |
"3. ERROR in X.java (at line 10)\n" + |
2838 |
"3. ERROR in X.java (at line 10)\n" + |
2717 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
2839 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
2718 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2840 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2719 |
"Potential resource leak: \'bis\' may not be closed\n" + |
2841 |
"Potential resource leak: \'bis\' may not be closed\n" + |
2720 |
"----------\n", |
2842 |
"----------\n", |
2721 |
null, |
2843 |
null, |
2722 |
true, |
2844 |
true, |