TwrForVariable4.java revision 2726:f62d01419621
1/* @test /nodynamiccopyright/
2 * @bug 7196163
3 * @summary Verify that variable used as an operand to try-with-resources is rejected if it is not
4 *          definitelly assigned before use and or not final or effectivelly final.
5 * @compile/fail/ref=TwrForVariable4.out -XDrawDiagnostics -Xlint:-options TwrForVariable4.java
6 */
7public class TwrForVariable4 implements AutoCloseable {
8    public static void main(String... args) {
9        TwrForVariable4 uninitialized;
10
11        try (uninitialized) {
12            fail("must be initialized before use");
13        }
14        uninitialized = new TwrForVariable4();
15
16        TwrForVariable4 notEffectivellyFinal1 = new TwrForVariable4();
17
18        notEffectivellyFinal1 = new TwrForVariable4();
19
20        try (notEffectivellyFinal1) {
21            fail("not effectivelly final");
22        }
23
24        TwrForVariable4 notEffectivellyFinal2 = new TwrForVariable4();
25
26        try (notEffectivellyFinal2) {
27            notEffectivellyFinal2 = new TwrForVariable4();
28            fail("not effectivelly final");
29        }
30
31        try (notFinal) {
32            fail("not final");
33        }
34    }
35
36    static TwrForVariable4 notFinal = new TwrForVariable4();
37
38    static void fail(String reason) {
39        throw new RuntimeException(reason);
40    }
41
42    public void close() {
43    }
44
45}
46