1/* @test /nodynamiccopyright/
2 * @bug 7196163
3 * @summary Verify that variables can be used as operands to try-with-resources
4 * @compile/fail/ref=TwrForVariable1.out -source 8 -XDrawDiagnostics -Xlint:-options TwrForVariable1.java
5 * @compile TwrForVariable1.java
6 * @run main TwrForVariable1
7 */
8public class TwrForVariable1 implements AutoCloseable {
9    private static int closeCount = 0;
10    public static void main(String... args) {
11        TwrForVariable1 v = new TwrForVariable1();
12
13        try (v) {
14            assertCloseCount(0);
15        }
16        try (/**@deprecated*/v) {
17            assertCloseCount(1);
18        }
19        try (v.finalWrapper.finalField) {
20            assertCloseCount(2);
21        } catch (Exception ex) {
22        }
23        try (new TwrForVariable1() { }.finalWrapper.finalField) {
24            assertCloseCount(3);
25        } catch (Exception ex) {
26        }
27        try ((args.length > 0 ? v : new TwrForVariable1()).finalWrapper.finalField) {
28            assertCloseCount(4);
29        } catch (Exception ex) {
30        }
31        try {
32            throw new CloseableException();
33        } catch (CloseableException ex) {
34            try (ex) {
35                assertCloseCount(5);
36            }
37        }
38
39        assertCloseCount(6);
40
41        // null test cases
42        TwrForVariable1 n = null;
43
44        try (n) {
45        }
46        try (n) {
47            throw new Exception();
48        } catch (Exception e) {
49        }
50
51        assertCloseCount(6);
52
53        // initialization exception
54        TwrForVariable1 i1 = new TwrForVariable1();
55        try (i1; TwrForVariable1 i2 = new TwrForVariable1(true)) {
56        } catch (Exception e) {
57        }
58
59        assertCloseCount(7);
60
61        // multiple closures
62        TwrForVariable1 m1 = new TwrForVariable1();
63        try (m1; TwrForVariable1 m2 = m1; TwrForVariable1 m3 = m2;) {
64        }
65
66        assertCloseCount(10);
67
68        // aliasing of variables keeps equality (bugs 6911256 6964740)
69        TwrForVariable1 a1 = new TwrForVariable1();
70        try (a1; TwrForVariable1 a2 = a1;) {
71            if (a2 != a2)
72                throw new RuntimeException("Unexpected inequality.");
73        }
74
75        assertCloseCount(12);
76
77        // anonymous class implementing AutoCloseable as variable in twr
78        AutoCloseable a = new AutoCloseable() {
79            public void close() { };
80        };
81        try (a) {
82        } catch (Exception e) {}
83    }
84
85    static void assertCloseCount(int expectedCloseCount) {
86        if (closeCount != expectedCloseCount)
87            throw new RuntimeException("bad closeCount: " + closeCount +
88                                       "; expected: " + expectedCloseCount);
89    }
90
91    public void close() {
92        closeCount++;
93    }
94
95    final FinalWrapper finalWrapper = new FinalWrapper();
96
97    static class FinalWrapper {
98        public final AutoCloseable finalField = new AutoCloseable() {
99            @Override
100            public void close() throws Exception {
101                closeCount++;
102            }
103        };
104    }
105
106    static class CloseableException extends Exception implements AutoCloseable {
107        @Override
108        public void close() {
109            closeCount++;
110        }
111    }
112
113    public TwrForVariable1(boolean throwException) {
114        if (throwException)
115            throw new RuntimeException("Initialization exception");
116    }
117
118    public TwrForVariable1() {
119        this(false);
120    }
121}
122