langtools/test/tools/javac/TryWithResources/TwrClose.java
author duke
Wed, 05 Jul 2017 23:01:50 +0200
changeset 44228 e46434c65a2b
parent 37638 aac00923d48e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37638
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     1
/*
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     4
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     8
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    14
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    18
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    21
 * questions.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    22
 */
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    23
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    24
/*
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    25
 * @test
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    26
 * @bug 7020499
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    27
 * @summary Verify that the close resource code works properly in all cases
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    28
 * @library /tools/lib
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.comp
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.main
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    32
 * @build toolbox.ToolBox TwrClose
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    33
 * @run main TwrClose
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    34
 */
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    35
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    36
import javax.tools.JavaFileManager;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    37
import javax.tools.StandardLocation;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    38
import javax.tools.ToolProvider;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    39
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    40
import com.sun.tools.javac.comp.Lower;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    41
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    42
import toolbox.JavacTask;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    43
import toolbox.ToolBox;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    44
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    45
public class TwrClose {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    46
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    47
    public static void main(String... args) throws Exception {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    48
        for (int i = 1; i < Lower.USE_CLOSE_RESOURCE_METHOD_THRESHOLD * 2; i++) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    49
            new TwrClose().compile(i);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    50
        }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    51
    }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    52
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    53
    ToolBox tb = new ToolBox();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    54
    JavaFileManager fm = ToolProvider.getSystemJavaCompiler()
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    55
                                     .getStandardFileManager(null, null, null);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    56
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    57
    void compile(int trysCount) throws Exception {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    58
        StringBuilder testInvocations = new StringBuilder();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    59
        StringBuilder testMethods = new StringBuilder();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    60
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    61
        for (int i = 0; i < trysCount; i++) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    62
            testInvocations.append(TEST_INVOCATIONS_TEMPLATE.replace("#N", Integer.toString(i)));
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    63
            testMethods.append(TEST_METHOD_TEMPLATE.replace("#N", Integer.toString(i)));
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    64
        }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    65
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    66
        String sourceCode = FILE_TEMPLATE.replace("#TEST_INVOCATIONS", testInvocations.toString())
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    67
                                         .replace("#TEST_METHODS", testMethods.toString());
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    68
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    69
        System.err.println("analyzing:");
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    70
        System.err.println(sourceCode);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    71
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    72
        try (ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager()) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    73
            new JavacTask(tb).fileManager(mfm)
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    74
                             .sources(sourceCode)
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    75
                             .run()
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    76
                             .writeAll();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    77
            ClassLoader cl = new ClassLoader(TwrClose.class.getClassLoader()) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    78
                @Override
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    79
                protected Class<?> findClass(String name) throws ClassNotFoundException {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    80
                    byte[] data = mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, name);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    81
                    if (data != null) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    82
                        return defineClass(name, data, 0, data.length);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    83
                    }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    84
                    return super.findClass(name);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    85
                }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    86
            };
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    87
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    88
            ((Runnable) cl.loadClass("Test").newInstance()).run();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    89
        }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    90
    }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    91
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    92
    final String TEST_INVOCATIONS_TEMPLATE =
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    93
        "        test#N(false, false, Arrays.asList(\"close\"));\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    94
        "        test#N(false, true, Arrays.asList(\"close\", \"close-exception\"));\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    95
        "        test#N(true, false, Arrays.asList(\"close\", \"inTwr\"));\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    96
        "        test#N(true, true, Arrays.asList(\"close\", \"inTwr\", \"close-exception\"));\n";
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    97
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    98
    final String TEST_METHOD_TEMPLATE =
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    99
        "    private void test#N(boolean failInTwr, boolean failOnClose,\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   100
        "                        List<String> expectedMessages) {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   101
        "        List<String> messages = new ArrayList<>();\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   102
        "        try {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   103
        "            try (CloseableImpl c = new CloseableImpl(messages, failOnClose)) {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   104
        "                if (failInTwr)\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   105
        "                    throw new IllegalStateException(\"inTwr\");\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   106
        "            }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   107
        "        } catch (IllegalStateException ex) {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   108
        "            messages.add(ex.getMessage());\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   109
        "            for (Throwable t : ex.getSuppressed()) {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   110
        "                messages.add(t.getMessage());\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   111
        "            }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   112
        "        }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   113
        "        if (!expectedMessages.equals(messages))\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   114
        "            throw new AssertionError(\"Expected and actual messages differ; expectedMessages=\" +\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   115
        "                                     expectedMessages + \"; actual=\" + messages);\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   116
        "    }\n";
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   117
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   118
    final String FILE_TEMPLATE =
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   119
        "import java.util.*;\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   120
        "public class Test implements Runnable {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   121
        "    public void run() {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   122
        "#TEST_INVOCATIONS" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   123
        "    }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   124
        "#TEST_METHODS" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   125
        "    static class CloseableImpl implements AutoCloseable {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   126
        "        private final List<String> messages;\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   127
        "        private final boolean failOnClose;\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   128
        "        public CloseableImpl(List<String> messages, boolean failOnClose) {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   129
        "            this.messages = messages;\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   130
        "            this.failOnClose = failOnClose;\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   131
        "        }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   132
        "        @Override\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   133
        "        public void close() {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   134
        "            messages.add(\"close\");\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   135
        "            if (failOnClose)\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   136
        "                throw new IllegalStateException(\"close-exception\");\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   137
        "        }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   138
        "    }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   139
        "}\n";
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   140
}