aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6970173 aoqi@0: * @summary Debug pointer at bad position aoqi@0: * @library /tools/javac/lib aoqi@0: * @build ToolBox aoqi@0: * @run main DebugPointerAtBadPositionTest aoqi@0: */ aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.nio.file.Paths; aoqi@0: aoqi@0: import com.sun.tools.classfile.ClassFile; aoqi@0: import com.sun.tools.classfile.Code_attribute; aoqi@0: import com.sun.tools.classfile.LineNumberTable_attribute; aoqi@0: import com.sun.tools.classfile.Method; aoqi@0: import com.sun.tools.javac.util.Assert; aoqi@0: aoqi@0: public class DebugPointerAtBadPositionTest { aoqi@0: aoqi@0: static final String testSource = aoqi@0: "public class AssertionTest {\n" + aoqi@0: " void lookForThisMethod() {\n" + aoqi@0: " int i;\n" + aoqi@0: " i = 33;\n" + aoqi@0: " assert // line 5\n" + aoqi@0: " i < 89:\n" + aoqi@0: " i < 100; // line 7\n" + aoqi@0: " }\n" + aoqi@0: "}"; aoqi@0: aoqi@0: static final int[][] expectedLNT = { aoqi@0: {4, 0}, aoqi@0: {5, 3}, aoqi@0: {8, 34} aoqi@0: }; aoqi@0: aoqi@0: static final String methodToLookFor = "lookForThisMethod"; aoqi@0: static final String seekMethodNotFoundMsg = aoqi@0: "The seek method was not found"; aoqi@0: static final String foundLNTLengthDifferentThanExpMsg = aoqi@0: "The LineNumberTable found has a length different to the expected one"; aoqi@0: aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: new DebugPointerAtBadPositionTest().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: compileTestClass(); aoqi@0: checkClassFile(new File(Paths.get(System.getProperty("user.dir"), aoqi@0: "AssertionTest.class").toUri()), methodToLookFor); aoqi@0: } aoqi@0: aoqi@0: void compileTestClass() throws Exception { aoqi@0: ToolBox.JavaToolArgs javacSuccessArgs = aoqi@0: new ToolBox.JavaToolArgs().setSources(testSource); aoqi@0: ToolBox.javac(javacSuccessArgs); aoqi@0: } aoqi@0: aoqi@0: void checkClassFile(final File cfile, String methodToFind) throws Exception { aoqi@0: ClassFile classFile = ClassFile.read(cfile); aoqi@0: boolean methodFound = false; aoqi@0: for (Method method : classFile.methods) { aoqi@0: if (method.getName(classFile.constant_pool).equals(methodToFind)) { aoqi@0: methodFound = true; aoqi@0: Code_attribute code = (Code_attribute) method.attributes.get("Code"); aoqi@0: LineNumberTable_attribute lnt = aoqi@0: (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); aoqi@0: Assert.check(lnt.line_number_table_length == expectedLNT.length, aoqi@0: foundLNTLengthDifferentThanExpMsg); aoqi@0: int i = 0; aoqi@0: for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { aoqi@0: Assert.check(entry.line_number == expectedLNT[i][0] && aoqi@0: entry.start_pc == expectedLNT[i][1], aoqi@0: "LNT entry at pos " + i + " differ from expected." + aoqi@0: "Found " + entry.line_number + ":" + entry.start_pc + aoqi@0: ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); aoqi@0: i++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: Assert.check(methodFound, seekMethodNotFoundMsg); aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: throw new AssertionError(msg); aoqi@0: } aoqi@0: aoqi@0: }