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 6970173
27 * @summary Debug pointer at bad position
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.main
32 *          jdk.compiler/com.sun.tools.javac.util
33 *          jdk.jdeps/com.sun.tools.javap
34 * @build toolbox.ToolBox toolbox.JavacTask
35 * @run main DebugPointerAtBadPositionTest
36 */
37
38import java.io.File;
39import java.nio.file.Paths;
40
41import com.sun.tools.classfile.ClassFile;
42import com.sun.tools.classfile.Code_attribute;
43import com.sun.tools.classfile.LineNumberTable_attribute;
44import com.sun.tools.classfile.Method;
45import com.sun.tools.javac.util.Assert;
46
47import toolbox.JavacTask;
48import toolbox.ToolBox;
49
50public class DebugPointerAtBadPositionTest {
51
52    static final String testSource =
53            "public class AssertionTest {\n" +
54            "    void lookForThisMethod() {\n" +
55            "        int i;\n" +
56            "        i = 33;\n" +
57            "        assert // line 5\n" +
58            "            i < 89:\n" +
59            "        i < 100; // line 7\n" +
60            "    }\n" +
61            "}";
62
63    static final int[][] expectedLNT = {
64        {4, 0},
65        {5, 3},
66        {8, 34}
67    };
68
69    static final String methodToLookFor = "lookForThisMethod";
70    static final String seekMethodNotFoundMsg =
71        "The seek method was not found";
72    static final String foundLNTLengthDifferentThanExpMsg =
73        "The LineNumberTable found has a length different to the expected one";
74
75    public static void main(String[] args) throws Exception {
76        new DebugPointerAtBadPositionTest().run();
77    }
78
79    void run() throws Exception {
80        compileTestClass();
81        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
82                "AssertionTest.class").toUri()), methodToLookFor);
83    }
84
85    void compileTestClass() throws Exception {
86        ToolBox tb = new ToolBox();
87        new JavacTask(tb)
88                .sources(testSource)
89                .run();
90    }
91
92    void checkClassFile(final File cfile, String methodToFind) throws Exception {
93        ClassFile classFile = ClassFile.read(cfile);
94        boolean methodFound = false;
95        for (Method method : classFile.methods) {
96            if (method.getName(classFile.constant_pool).equals(methodToFind)) {
97                methodFound = true;
98                Code_attribute code = (Code_attribute) method.attributes.get("Code");
99                LineNumberTable_attribute lnt =
100                        (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
101                Assert.check(lnt.line_number_table_length == expectedLNT.length,
102                        foundLNTLengthDifferentThanExpMsg);
103                int i = 0;
104                for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
105                    Assert.check(entry.line_number == expectedLNT[i][0] &&
106                            entry.start_pc == expectedLNT[i][1],
107                            "LNT entry at pos " + i + " differ from expected." +
108                            "Found " + entry.line_number + ":" + entry.start_pc +
109                            ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
110                    i++;
111                }
112            }
113        }
114        Assert.check(methodFound, seekMethodNotFoundMsg);
115    }
116
117    void error(String msg) {
118        throw new AssertionError(msg);
119    }
120
121}
122