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.main
31 *          jdk.compiler/com.sun.tools.javac.util
32 *          jdk.jdeps/com.sun.tools.classfile
33 * @build toolbox.ToolBox toolbox.JavacTask
34 * @run main NoDeadCodeGenerationOnTrySmtTest
35 */
36
37import java.io.File;
38import java.nio.file.Paths;
39
40import com.sun.tools.classfile.ClassFile;
41import com.sun.tools.classfile.Code_attribute;
42import com.sun.tools.classfile.Code_attribute.Exception_data;
43import com.sun.tools.classfile.Method;
44import com.sun.tools.javac.util.Assert;
45
46import toolbox.JavacTask;
47import toolbox.ToolBox;
48
49public class NoDeadCodeGenerationOnTrySmtTest {
50
51    static final String testSource =
52        "public class Test {\n" +
53        "    void m1(int arg) {\n" +
54        "        synchronized (new Integer(arg)) {\n" +
55        "            {\n" +
56        "                label0:\n" +
57        "                do {\n" +
58        "                    break label0;\n" +
59        "                } while (arg != 0);\n" +
60        "            }\n" +
61        "        }\n" +
62        "    }\n" +
63
64        "    void m2(int arg) {\n" +
65        "        synchronized (new Integer(arg)) {\n" +
66        "            {\n" +
67        "                label0:\n" +
68        "                {\n" +
69        "                    break label0;\n" +
70        "                }\n" +
71        "            }\n" +
72        "        }\n" +
73        "    }\n" +
74        "}";
75
76    static final int[][] expectedExceptionTable = {
77    //  {from,         to,         target,      type},
78        {11,           13,         16,          0},
79        {16,           19,         16,          0}
80    };
81
82    static final String[] methodsToLookFor = {"m1", "m2"};
83
84    ToolBox tb = new ToolBox();
85
86    public static void main(String[] args) throws Exception {
87        new NoDeadCodeGenerationOnTrySmtTest().run();
88    }
89
90    void run() throws Exception {
91        compileTestClass();
92        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
93                "Test.class").toUri()), methodsToLookFor);
94    }
95
96    void compileTestClass() throws Exception {
97        new JavacTask(tb)
98                .sources(testSource)
99                .run();
100    }
101
102    void checkClassFile(final File cfile, String[] methodsToFind) throws Exception {
103        ClassFile classFile = ClassFile.read(cfile);
104        int numberOfmethodsFound = 0;
105        for (String methodToFind : methodsToFind) {
106            for (Method method : classFile.methods) {
107                if (method.getName(classFile.constant_pool).equals(methodToFind)) {
108                    numberOfmethodsFound++;
109                    Code_attribute code = (Code_attribute) method.attributes.get("Code");
110                    Assert.check(code.exception_table_length == expectedExceptionTable.length,
111                            "The ExceptionTable found has a length different to the expected one");
112                    int i = 0;
113                    for (Exception_data entry: code.exception_table) {
114                        Assert.check(entry.start_pc == expectedExceptionTable[i][0] &&
115                                entry.end_pc == expectedExceptionTable[i][1] &&
116                                entry.handler_pc == expectedExceptionTable[i][2] &&
117                                entry.catch_type == expectedExceptionTable[i][3],
118                                "Exception table entry at pos " + i + " differ from expected.");
119                        i++;
120                    }
121                }
122            }
123        }
124        Assert.check(numberOfmethodsFound == 2, "Some seek methods were not found");
125    }
126
127    void error(String msg) {
128        throw new AssertionError(msg);
129    }
130
131}
132