Fri, 24 Apr 2020 03:58:51 +0100
Merge
1 /*
2 * Copyright (c) 2013, 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 6970173
27 * @summary Debug pointer at bad position
28 * @library /tools/javac/lib
29 * @build ToolBox
30 * @run main DebugPointerAtBadPositionTest
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 DebugPointerAtBadPositionTest {
44 static final String testSource =
45 "public class AssertionTest {\n" +
46 " void lookForThisMethod() {\n" +
47 " int i;\n" +
48 " i = 33;\n" +
49 " assert // line 5\n" +
50 " i < 89:\n" +
51 " i < 100; // line 7\n" +
52 " }\n" +
53 "}";
55 static final int[][] expectedLNT = {
56 {4, 0},
57 {5, 3},
58 {8, 34}
59 };
61 static final String methodToLookFor = "lookForThisMethod";
62 static final String seekMethodNotFoundMsg =
63 "The seek method was not found";
64 static final String foundLNTLengthDifferentThanExpMsg =
65 "The LineNumberTable found has a length different to the expected one";
67 public static void main(String[] args) throws Exception {
68 new DebugPointerAtBadPositionTest().run();
69 }
71 void run() throws Exception {
72 compileTestClass();
73 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
74 "AssertionTest.class").toUri()), methodToLookFor);
75 }
77 void compileTestClass() throws Exception {
78 ToolBox.JavaToolArgs javacSuccessArgs =
79 new ToolBox.JavaToolArgs().setSources(testSource);
80 ToolBox.javac(javacSuccessArgs);
81 }
83 void checkClassFile(final File cfile, String methodToFind) throws Exception {
84 ClassFile classFile = ClassFile.read(cfile);
85 boolean methodFound = false;
86 for (Method method : classFile.methods) {
87 if (method.getName(classFile.constant_pool).equals(methodToFind)) {
88 methodFound = true;
89 Code_attribute code = (Code_attribute) method.attributes.get("Code");
90 LineNumberTable_attribute lnt =
91 (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
92 Assert.check(lnt.line_number_table_length == expectedLNT.length,
93 foundLNTLengthDifferentThanExpMsg);
94 int i = 0;
95 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
96 Assert.check(entry.line_number == expectedLNT[i][0] &&
97 entry.start_pc == expectedLNT[i][1],
98 "LNT entry at pos " + i + " differ from expected." +
99 "Found " + entry.line_number + ":" + entry.start_pc +
100 ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
101 i++;
102 }
103 }
104 }
105 Assert.check(methodFound, seekMethodNotFoundMsg);
106 }
108 void error(String msg) {
109 throw new AssertionError(msg);
110 }
112 }