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