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