aeriksso@3000: /* shshahma@3371: * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. aeriksso@3000: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aeriksso@3000: * aeriksso@3000: * This code is free software; you can redistribute it and/or modify it aeriksso@3000: * under the terms of the GNU General Public License version 2 only, as aeriksso@3000: * published by the Free Software Foundation. aeriksso@3000: * aeriksso@3000: * This code is distributed in the hope that it will be useful, but WITHOUT aeriksso@3000: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aeriksso@3000: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aeriksso@3000: * version 2 for more details (a copy is included in the LICENSE file that aeriksso@3000: * accompanied this code). aeriksso@3000: * aeriksso@3000: * You should have received a copy of the GNU General Public License version aeriksso@3000: * 2 along with this work; if not, write to the Free Software Foundation, aeriksso@3000: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aeriksso@3000: * aeriksso@3000: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aeriksso@3000: * or visit www.oracle.com if you need additional information or have any aeriksso@3000: * questions. aeriksso@3000: */ aeriksso@3000: aeriksso@3000: /* aeriksso@3000: * @test aeriksso@3000: * @bug 8134759 aeriksso@3000: * @summary Add LineNumberTable attribute for return bytecodes split around finally code aeriksso@3000: * @modules jdk.jdeps/com.sun.tools.classfile aeriksso@3000: */ aeriksso@3000: aeriksso@3000: import com.sun.tools.classfile.ClassFile; aeriksso@3000: import com.sun.tools.classfile.ConstantPoolException; aeriksso@3000: import com.sun.tools.classfile.Method; aeriksso@3000: import com.sun.tools.classfile.Attribute; aeriksso@3000: import com.sun.tools.classfile.Code_attribute; aeriksso@3000: import com.sun.tools.classfile.LineNumberTable_attribute; aeriksso@3000: import com.sun.tools.classfile.LineNumberTable_attribute.Entry; aeriksso@3000: aeriksso@3000: import java.io.IOException; aeriksso@3000: aeriksso@3000: public class FinallyLineNumberTest { aeriksso@3000: public static void main(String[] args) throws Exception { aeriksso@3000: // check that we have 5 consecutive entries for method() aeriksso@3000: Entry[] lines = findEntries(); aeriksso@3000: if (lines == null) { aeriksso@3000: throw new Exception("finally line number table could not be loaded"); aeriksso@3000: } shshahma@3371: if (lines.length != 5) { aeriksso@3000: // Help debug aeriksso@3000: System.err.println("LineTable error, got lines:"); aeriksso@3000: for (Entry e : lines) { aeriksso@3000: System.err.println(e.line_number); aeriksso@3000: } shshahma@3371: throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=5"); aeriksso@3000: } aeriksso@3000: aeriksso@3000: // return null line, for the load null operation aeriksso@3000: int current = lines[0].line_number; aeriksso@3000: int first = current; aeriksso@3000: aeriksso@3000: // finally line aeriksso@3000: current = lines[1].line_number; aeriksso@3000: if (current != first + 2) { aeriksso@3000: throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2)); aeriksso@3000: } aeriksso@3000: aeriksso@3000: // return null line, for the return operation aeriksso@3000: current = lines[2].line_number; aeriksso@3000: if (current != first) { aeriksso@3000: throw new Exception("finally line number table incorrect: got=" + current + " expected=" + first); aeriksso@3000: } aeriksso@3000: shshahma@3371: // for when exception is thrown aeriksso@3000: current = lines[3].line_number; aeriksso@3000: if (current != first + 2) { aeriksso@3000: throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2)); aeriksso@3000: } shshahma@3371: shshahma@3371: // the '}' closing the finally block shshahma@3371: current = lines[4].line_number; shshahma@3371: if (current != first + 3) { shshahma@3371: throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 3)); shshahma@3371: } aeriksso@3000: } aeriksso@3000: aeriksso@3000: static Entry[] findEntries() throws IOException, ConstantPoolException { aeriksso@3000: ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class")); aeriksso@3000: for (Method m : self.methods) { aeriksso@3000: if ("method".equals(m.getName(self.constant_pool))) { aeriksso@3000: Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code); aeriksso@3000: for (Attribute at : code_attribute.attributes) { aeriksso@3000: if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) { aeriksso@3000: return ((LineNumberTable_attribute)at).line_number_table; aeriksso@3000: } aeriksso@3000: } aeriksso@3000: } aeriksso@3000: } aeriksso@3000: return null; aeriksso@3000: } aeriksso@3000: aeriksso@3000: // This method should get LineNumberTable entries for: aeriksso@3000: // *) The load of the null aeriksso@3000: // *) The finally code for when an exception is *not* thrown aeriksso@3000: // *) The actual return, which should have the same line as the load of the null aeriksso@3000: // *) The finally code for when an exception *is* thrown, should have the same line as above finally code aeriksso@3000: public static String method(int field) { aeriksso@3000: try { aeriksso@3000: return null; aeriksso@3000: } finally { aeriksso@3000: field+=1; // Dummy aeriksso@3000: } aeriksso@3000: } aeriksso@3000: }