langtools/test/tools/javac/TryWithResources/TwrForVariable1.java
author jlahoda
Wed, 09 Dec 2015 14:26:56 +0100
changeset 34565 627464b87753
parent 27844 8b5d79870a2f
permissions -rw-r--r--
8080641: JEP-JDK-8042880 : Implement new tests on Project Coin Summary: A set of tests using t-w-r as variable in different positive and negative constructions Reviewed-by: abuckley, darcy, jlahoda, sadayapalam Contributed-by: sergei.pikalev@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     1
/* @test /nodynamiccopyright/
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     2
 * @bug 7196163
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     3
 * @summary Verify that variables can be used as operands to try-with-resources
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     4
 * @compile/fail/ref=TwrForVariable1.out -source 8 -XDrawDiagnostics -Xlint:-options TwrForVariable1.java
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     5
 * @compile TwrForVariable1.java
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     6
 * @run main TwrForVariable1
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     7
 */
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     8
public class TwrForVariable1 implements AutoCloseable {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
     9
    private static int closeCount = 0;
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    10
    public static void main(String... args) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    11
        TwrForVariable1 v = new TwrForVariable1();
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    12
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    13
        try (v) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    14
            assertCloseCount(0);
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    15
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    16
        try (/**@deprecated*/v) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    17
            assertCloseCount(1);
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    18
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    19
        try (v.finalWrapper.finalField) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    20
            assertCloseCount(2);
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    21
        } catch (Exception ex) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    22
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    23
        try (new TwrForVariable1() { }.finalWrapper.finalField) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    24
            assertCloseCount(3);
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    25
        } catch (Exception ex) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    26
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    27
        try ((args.length > 0 ? v : new TwrForVariable1()).finalWrapper.finalField) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    28
            assertCloseCount(4);
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    29
        } catch (Exception ex) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    30
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    31
        try {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    32
            throw new CloseableException();
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    33
        } catch (CloseableException ex) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    34
            try (ex) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    35
                assertCloseCount(5);
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    36
            }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    37
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    38
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    39
        assertCloseCount(6);
34565
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    40
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    41
        // null test cases
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    42
        TwrForVariable1 n = null;
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    43
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    44
        try (n) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    45
        }
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    46
        try (n) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    47
            throw new Exception();
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    48
        } catch (Exception e) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    49
        }
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    50
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    51
        assertCloseCount(6);
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    52
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    53
        // initialization exception
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    54
        TwrForVariable1 i1 = new TwrForVariable1();
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    55
        try (i1; TwrForVariable1 i2 = new TwrForVariable1(true)) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    56
        } catch (Exception e) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    57
        }
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    58
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    59
        assertCloseCount(7);
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    60
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    61
        // multiple closures
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    62
        TwrForVariable1 m1 = new TwrForVariable1();
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    63
        try (m1; TwrForVariable1 m2 = m1; TwrForVariable1 m3 = m2;) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    64
        }
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    65
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    66
        assertCloseCount(10);
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    67
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    68
        // aliasing of variables keeps equality (bugs 6911256 6964740)
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    69
        TwrForVariable1 a1 = new TwrForVariable1();
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    70
        try (a1; TwrForVariable1 a2 = a1;) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    71
            if (a2 != a2)
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    72
                throw new RuntimeException("Unexpected inequality.");
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    73
        }
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    74
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    75
        assertCloseCount(12);
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    76
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    77
        // anonymous class implementing AutoCloseable as variable in twr
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    78
        AutoCloseable a = new AutoCloseable() {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    79
            public void close() { };
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    80
        };
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    81
        try (a) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
    82
        } catch (Exception e) {}
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    83
    }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    84
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    85
    static void assertCloseCount(int expectedCloseCount) {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    86
        if (closeCount != expectedCloseCount)
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    87
            throw new RuntimeException("bad closeCount: " + closeCount +
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    88
                                       "; expected: " + expectedCloseCount);
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    89
    }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    90
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    91
    public void close() {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    92
        closeCount++;
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    93
    }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    94
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    95
    final FinalWrapper finalWrapper = new FinalWrapper();
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    96
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    97
    static class FinalWrapper {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    98
        public final AutoCloseable finalField = new AutoCloseable() {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
    99
            @Override
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   100
            public void close() throws Exception {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   101
                closeCount++;
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   102
            }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   103
        };
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   104
    }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   105
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   106
    static class CloseableException extends Exception implements AutoCloseable {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   107
        @Override
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   108
        public void close() {
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   109
            closeCount++;
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   110
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   111
    }
34565
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   112
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   113
    public TwrForVariable1(boolean throwException) {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   114
        if (throwException)
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   115
            throw new RuntimeException("Initialization exception");
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   116
    }
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   117
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   118
    public TwrForVariable1() {
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   119
        this(false);
627464b87753 8080641: JEP-JDK-8042880 : Implement new tests on Project Coin
jlahoda
parents: 27844
diff changeset
   120
    }
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents:
diff changeset
   121
}