InlinedFinallyConfuseDebuggersTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2015, 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 7008643
27 * @summary inlined finally clauses confuse debuggers
28 * @library /tools/lib
29 * @modules jdk.jdeps/com.sun.tools.classfile
30 *          jdk.compiler/com.sun.tools.javac.api
31 *          jdk.compiler/com.sun.tools.javac.file
32 *          jdk.compiler/com.sun.tools.javac.main
33 *          jdk.compiler/com.sun.tools.javac.util
34 *          jdk.jdeps/com.sun.tools.javap
35 * @build ToolBox
36 * @run main InlinedFinallyConfuseDebuggersTest
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.LineNumberTable_attribute;
45import com.sun.tools.classfile.Method;
46import com.sun.tools.javac.util.Assert;
47
48public class InlinedFinallyConfuseDebuggersTest {
49
50    static final String testSource =
51    /* 01 */        "public class InlinedFinallyTest {\n" +
52    /* 02 */        "    void lookForThisMethod(int value) {\n" +
53    /* 03 */        "        try {\n" +
54    /* 04 */        "            if (value > 0) {\n" +
55    /* 05 */        "                System.out.println(\"if\");\n" +
56    /* 06 */        "                return;\n" +
57    /* 07 */        "            }\n" +
58    /* 08 */        "        } finally {\n" +
59    /* 09 */        "            System.out.println(\"finally\");\n" +
60    /* 10 */        "        }\n" +
61    /* 11 */        "    }\n" +
62    /* 12 */        "}";
63
64    static final int[][] expectedLNT = {
65    //  {line-number, start-pc},
66        {4,           0},       //if (value > 0) {
67        {5,           4},       //    System.out.println("if");
68        {9,           12},      //System.out.println("finally");
69        {6,           20},      //    return;
70        {9,           21},      //System.out.println("finally");
71        {10,          29},
72        {9,           32},      //System.out.println("finally");
73        {11,          43},
74    };
75
76    static final String methodToLookFor = "lookForThisMethod";
77
78    public static void main(String[] args) throws Exception {
79        new InlinedFinallyConfuseDebuggersTest().run();
80    }
81
82    ToolBox tb = new ToolBox();
83
84    void run() throws Exception {
85        compileTestClass();
86        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
87                "InlinedFinallyTest.class").toUri()), methodToLookFor);
88    }
89
90    void compileTestClass() throws Exception {
91        tb.new JavacTask()
92                .sources(testSource)
93                .run();
94    }
95
96    void checkClassFile(final File cfile, String methodToFind) throws Exception {
97        ClassFile classFile = ClassFile.read(cfile);
98        boolean methodFound = false;
99        for (Method method : classFile.methods) {
100            if (method.getName(classFile.constant_pool).equals(methodToFind)) {
101                methodFound = true;
102                Code_attribute code = (Code_attribute) method.attributes.get("Code");
103                LineNumberTable_attribute lnt =
104                        (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
105                Assert.check(lnt.line_number_table_length == expectedLNT.length,
106                        "The LineNumberTable found has a length different to the expected one");
107                int i = 0;
108                for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
109                    Assert.check(entry.line_number == expectedLNT[i][0] &&
110                            entry.start_pc == expectedLNT[i][1],
111                            "LNT entry at pos " + i + " differ from expected." +
112                            "Found " + entry.line_number + ":" + entry.start_pc +
113                            ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
114                    i++;
115                }
116            }
117        }
118        Assert.check(methodFound, "The seek method was not found");
119    }
120
121    void error(String msg) {
122        throw new AssertionError(msg);
123    }
124
125}
126