NoDeadCodeGenerationOnTrySmtTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8024039
27 * @summary javac, previous solution for JDK-8022186 was incorrect
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.file
31 *          jdk.compiler/com.sun.tools.javac.main
32 *          jdk.compiler/com.sun.tools.javac.util
33 *          jdk.jdeps/com.sun.tools.classfile
34 *          jdk.jdeps/com.sun.tools.javap
35 * @build ToolBox
36 * @run main NoDeadCodeGenerationOnTrySmtTest
37 */
38
39import java.io.File;
40import java.nio.file.Paths;
41
42import com.sun.tools.classfile.ClassFile;
43import com.sun.tools.classfile.Code_attribute;
44import com.sun.tools.classfile.Code_attribute.Exception_data;
45import com.sun.tools.classfile.Method;
46import com.sun.tools.javac.util.Assert;
47
48public class NoDeadCodeGenerationOnTrySmtTest {
49
50    static final String testSource =
51        "public class Test {\n" +
52        "    void m1(int arg) {\n" +
53        "        synchronized (new Integer(arg)) {\n" +
54        "            {\n" +
55        "                label0:\n" +
56        "                do {\n" +
57        "                    break label0;\n" +
58        "                } while (arg != 0);\n" +
59        "            }\n" +
60        "        }\n" +
61        "    }\n" +
62
63        "    void m2(int arg) {\n" +
64        "        synchronized (new Integer(arg)) {\n" +
65        "            {\n" +
66        "                label0:\n" +
67        "                {\n" +
68        "                    break label0;\n" +
69        "                }\n" +
70        "            }\n" +
71        "        }\n" +
72        "    }\n" +
73        "}";
74
75    static final int[][] expectedExceptionTable = {
76    //  {from,         to,         target,      type},
77        {11,           13,         16,          0},
78        {16,           19,         16,          0}
79    };
80
81    static final String[] methodsToLookFor = {"m1", "m2"};
82
83    ToolBox tb = new ToolBox();
84
85    public static void main(String[] args) throws Exception {
86        new NoDeadCodeGenerationOnTrySmtTest().run();
87    }
88
89    void run() throws Exception {
90        compileTestClass();
91        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
92                "Test.class").toUri()), methodsToLookFor);
93    }
94
95    void compileTestClass() throws Exception {
96        tb.new JavacTask()
97                .sources(testSource)
98                .run();
99    }
100
101    void checkClassFile(final File cfile, String[] methodsToFind) throws Exception {
102        ClassFile classFile = ClassFile.read(cfile);
103        int numberOfmethodsFound = 0;
104        for (String methodToFind : methodsToFind) {
105            for (Method method : classFile.methods) {
106                if (method.getName(classFile.constant_pool).equals(methodToFind)) {
107                    numberOfmethodsFound++;
108                    Code_attribute code = (Code_attribute) method.attributes.get("Code");
109                    Assert.check(code.exception_table_length == expectedExceptionTable.length,
110                            "The ExceptionTable found has a length different to the expected one");
111                    int i = 0;
112                    for (Exception_data entry: code.exception_table) {
113                        Assert.check(entry.start_pc == expectedExceptionTable[i][0] &&
114                                entry.end_pc == expectedExceptionTable[i][1] &&
115                                entry.handler_pc == expectedExceptionTable[i][2] &&
116                                entry.catch_type == expectedExceptionTable[i][3],
117                                "Exception table entry at pos " + i + " differ from expected.");
118                        i++;
119                    }
120                }
121            }
122        }
123        Assert.check(numberOfmethodsFound == 2, "Some seek methods were not found");
124    }
125
126    void error(String msg) {
127        throw new AssertionError(msg);
128    }
129
130}
131