test/tools/javac/linenumbers/FinallyLineNumberTest.java

Tue, 27 Oct 2015 10:35:14 +0100

author
aeriksso
date
Tue, 27 Oct 2015 10:35:14 +0100
changeset 3000
4044eb07194d
child 3371
7220be8747f0
permissions
-rw-r--r--

8134759: jdb: Incorrect stepping inside finally block
Summary: Add LineNumberTable attribute for return bytecodes split around finally code
Reviewed-by: mcimadamore

aeriksso@3000 1 /*
aeriksso@3000 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
aeriksso@3000 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aeriksso@3000 4 *
aeriksso@3000 5 * This code is free software; you can redistribute it and/or modify it
aeriksso@3000 6 * under the terms of the GNU General Public License version 2 only, as
aeriksso@3000 7 * published by the Free Software Foundation.
aeriksso@3000 8 *
aeriksso@3000 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aeriksso@3000 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aeriksso@3000 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aeriksso@3000 12 * version 2 for more details (a copy is included in the LICENSE file that
aeriksso@3000 13 * accompanied this code).
aeriksso@3000 14 *
aeriksso@3000 15 * You should have received a copy of the GNU General Public License version
aeriksso@3000 16 * 2 along with this work; if not, write to the Free Software Foundation,
aeriksso@3000 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aeriksso@3000 18 *
aeriksso@3000 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aeriksso@3000 20 * or visit www.oracle.com if you need additional information or have any
aeriksso@3000 21 * questions.
aeriksso@3000 22 */
aeriksso@3000 23
aeriksso@3000 24 /*
aeriksso@3000 25 * @test
aeriksso@3000 26 * @bug 8134759
aeriksso@3000 27 * @summary Add LineNumberTable attribute for return bytecodes split around finally code
aeriksso@3000 28 * @modules jdk.jdeps/com.sun.tools.classfile
aeriksso@3000 29 */
aeriksso@3000 30
aeriksso@3000 31 import com.sun.tools.classfile.ClassFile;
aeriksso@3000 32 import com.sun.tools.classfile.ConstantPoolException;
aeriksso@3000 33 import com.sun.tools.classfile.Method;
aeriksso@3000 34 import com.sun.tools.classfile.Attribute;
aeriksso@3000 35 import com.sun.tools.classfile.Code_attribute;
aeriksso@3000 36 import com.sun.tools.classfile.LineNumberTable_attribute;
aeriksso@3000 37 import com.sun.tools.classfile.LineNumberTable_attribute.Entry;
aeriksso@3000 38
aeriksso@3000 39 import java.io.File;
aeriksso@3000 40 import java.io.IOException;
aeriksso@3000 41
aeriksso@3000 42 public class FinallyLineNumberTest {
aeriksso@3000 43 public static void main(String[] args) throws Exception {
aeriksso@3000 44 // check that we have 5 consecutive entries for method()
aeriksso@3000 45 Entry[] lines = findEntries();
aeriksso@3000 46 if (lines == null) {
aeriksso@3000 47 throw new Exception("finally line number table could not be loaded");
aeriksso@3000 48 }
aeriksso@3000 49 if (lines.length != 4) {
aeriksso@3000 50 // Help debug
aeriksso@3000 51 System.err.println("LineTable error, got lines:");
aeriksso@3000 52 for (Entry e : lines) {
aeriksso@3000 53 System.err.println(e.line_number);
aeriksso@3000 54 }
aeriksso@3000 55 throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=4");
aeriksso@3000 56 }
aeriksso@3000 57
aeriksso@3000 58 // return null line, for the load null operation
aeriksso@3000 59 int current = lines[0].line_number;
aeriksso@3000 60 int first = current;
aeriksso@3000 61
aeriksso@3000 62 // finally line
aeriksso@3000 63 current = lines[1].line_number;
aeriksso@3000 64 if (current != first + 2) {
aeriksso@3000 65 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2));
aeriksso@3000 66 }
aeriksso@3000 67
aeriksso@3000 68 // return null line, for the return operation
aeriksso@3000 69 current = lines[2].line_number;
aeriksso@3000 70 if (current != first) {
aeriksso@3000 71 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + first);
aeriksso@3000 72 }
aeriksso@3000 73
aeriksso@3000 74 // finally line, for when exception is thrown
aeriksso@3000 75 current = lines[3].line_number;
aeriksso@3000 76 if (current != first + 2) {
aeriksso@3000 77 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2));
aeriksso@3000 78 }
aeriksso@3000 79 }
aeriksso@3000 80
aeriksso@3000 81 static Entry[] findEntries() throws IOException, ConstantPoolException {
aeriksso@3000 82 ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class"));
aeriksso@3000 83 for (Method m : self.methods) {
aeriksso@3000 84 if ("method".equals(m.getName(self.constant_pool))) {
aeriksso@3000 85 Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
aeriksso@3000 86 for (Attribute at : code_attribute.attributes) {
aeriksso@3000 87 if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
aeriksso@3000 88 return ((LineNumberTable_attribute)at).line_number_table;
aeriksso@3000 89 }
aeriksso@3000 90 }
aeriksso@3000 91 }
aeriksso@3000 92 }
aeriksso@3000 93 return null;
aeriksso@3000 94 }
aeriksso@3000 95
aeriksso@3000 96 // This method should get LineNumberTable entries for:
aeriksso@3000 97 // *) The load of the null
aeriksso@3000 98 // *) The finally code for when an exception is *not* thrown
aeriksso@3000 99 // *) The actual return, which should have the same line as the load of the null
aeriksso@3000 100 // *) The finally code for when an exception *is* thrown, should have the same line as above finally code
aeriksso@3000 101 public static String method(int field) {
aeriksso@3000 102 try {
aeriksso@3000 103 return null;
aeriksso@3000 104 } finally {
aeriksso@3000 105 field+=1; // Dummy
aeriksso@3000 106 }
aeriksso@3000 107 }
aeriksso@3000 108 }

mercurial