1 /* 2 * Copyright (c) 2013, 2015, 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.compiler/com.sun.tools.classfile 30 * jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.file 32 * jdk.compiler/com.sun.tools.javac.main 33 * jdk.compiler/com.sun.tools.javac.util 34 * @build ToolBox 35 * @run main DebugPointerAtBadPositionTest 36 */ 37 38 import java.io.File; 39 import java.nio.file.Paths; 40 41 import com.sun.tools.classfile.ClassFile; 42 import com.sun.tools.classfile.Code_attribute; 43 import com.sun.tools.classfile.LineNumberTable_attribute; 44 import com.sun.tools.classfile.Method; 45 import com.sun.tools.javac.util.Assert; 46 47 public class DebugPointerAtBadPositionTest { 48 49 static final String testSource = 50 "public class AssertionTest {\n" + 51 " void lookForThisMethod() {\n" + 52 " int i;\n" + 53 " i = 33;\n" + 54 " assert // line 5\n" + 55 " i < 89:\n" + 56 " i < 100; // line 7\n" + 57 " }\n" + 58 "}"; 59 60 static final int[][] expectedLNT = { 61 {4, 0}, 62 {5, 3}, 63 {8, 34} 64 }; 65 66 static final String methodToLookFor = "lookForThisMethod"; 67 static final String seekMethodNotFoundMsg = 68 "The seek method was not found"; 69 static final String foundLNTLengthDifferentThanExpMsg = 70 "The LineNumberTable found has a length different to the expected one"; 71 72 public static void main(String[] args) throws Exception { 73 new DebugPointerAtBadPositionTest().run(); 74 } 75 76 void run() throws Exception { 77 compileTestClass(); 78 checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 79 "AssertionTest.class").toUri()), methodToLookFor); 80 } 81 82 void compileTestClass() throws Exception { 83 ToolBox tb = new ToolBox(); 84 tb.new JavacTask() 85 .sources(testSource) 86 .run(); 87 } 88 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 foundLNTLengthDifferentThanExpMsg); 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, seekMethodNotFoundMsg); 112 } 113 114 void error(String msg) { 115 throw new AssertionError(msg); 116 } 117 118 }