langtools/test/tools/javac/T6970173/DebugPointerAtBadPositionTest.java
author mchung
Wed, 27 May 2015 13:25:18 -0700
changeset 30846 2b3f379840f0
parent 30730 d3ce7619db2c
child 36526 3b41f1c69604
permissions -rw-r--r--
8074432: Move jdeps and javap to jdk.jdeps module Reviewed-by: jjg, alanb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17808
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
17808
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     4
 *
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     8
 *
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    13
 * accompanied this code).
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    14
 *
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    18
 *
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    21
 * questions.
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    22
 */
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    23
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    24
/*
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    25
 * @test
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    26
 * @bug 6970173
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    27
 * @summary Debug pointer at bad position
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 17808
diff changeset
    28
 * @library /tools/lib
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    29
 * @modules jdk.jdeps/com.sun.tools.classfile
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.main
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    33
 *          jdk.compiler/com.sun.tools.javac.util
17808
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    34
 * @build ToolBox
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    35
 * @run main DebugPointerAtBadPositionTest
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    36
 */
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    37
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    38
import java.io.File;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    39
import java.nio.file.Paths;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    40
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    41
import com.sun.tools.classfile.ClassFile;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    42
import com.sun.tools.classfile.Code_attribute;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    43
import com.sun.tools.classfile.LineNumberTable_attribute;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    44
import com.sun.tools.classfile.Method;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    45
import com.sun.tools.javac.util.Assert;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    46
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    47
public class DebugPointerAtBadPositionTest {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    48
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    49
    static final String testSource =
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    50
            "public class AssertionTest {\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    51
            "    void lookForThisMethod() {\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    52
            "        int i;\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    53
            "        i = 33;\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    54
            "        assert // line 5\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    55
            "            i < 89:\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    56
            "        i < 100; // line 7\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    57
            "    }\n" +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    58
            "}";
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    59
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    60
    static final int[][] expectedLNT = {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    61
        {4, 0},
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    62
        {5, 3},
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    63
        {8, 34}
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    64
    };
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    65
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    66
    static final String methodToLookFor = "lookForThisMethod";
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    67
    static final String seekMethodNotFoundMsg =
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    68
        "The seek method was not found";
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    69
    static final String foundLNTLengthDifferentThanExpMsg =
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    70
        "The LineNumberTable found has a length different to the expected one";
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    71
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    72
    public static void main(String[] args) throws Exception {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    73
        new DebugPointerAtBadPositionTest().run();
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    74
    }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    75
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    76
    void run() throws Exception {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    77
        compileTestClass();
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    78
        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    79
                "AssertionTest.class").toUri()), methodToLookFor);
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    80
    }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    81
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    82
    void compileTestClass() throws Exception {
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 17808
diff changeset
    83
        ToolBox tb = new ToolBox();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 17808
diff changeset
    84
        tb.new JavacTask()
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 17808
diff changeset
    85
                .sources(testSource)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 17808
diff changeset
    86
                .run();
17808
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    87
    }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    88
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    89
    void checkClassFile(final File cfile, String methodToFind) throws Exception {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    90
        ClassFile classFile = ClassFile.read(cfile);
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    91
        boolean methodFound = false;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    92
        for (Method method : classFile.methods) {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    93
            if (method.getName(classFile.constant_pool).equals(methodToFind)) {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    94
                methodFound = true;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    95
                Code_attribute code = (Code_attribute) method.attributes.get("Code");
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    96
                LineNumberTable_attribute lnt =
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    97
                        (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    98
                Assert.check(lnt.line_number_table_length == expectedLNT.length,
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
    99
                        foundLNTLengthDifferentThanExpMsg);
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   100
                int i = 0;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   101
                for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   102
                    Assert.check(entry.line_number == expectedLNT[i][0] &&
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   103
                            entry.start_pc == expectedLNT[i][1],
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   104
                            "LNT entry at pos " + i + " differ from expected." +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   105
                            "Found " + entry.line_number + ":" + entry.start_pc +
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   106
                            ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   107
                    i++;
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   108
                }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   109
            }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   110
        }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   111
        Assert.check(methodFound, seekMethodNotFoundMsg);
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   112
    }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   113
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   114
    void error(String msg) {
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   115
        throw new AssertionError(msg);
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   116
    }
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   117
846139633c68 6970173: Debug pointer at bad position
vromero
parents:
diff changeset
   118
}