test/tools/javac/linenumbers/FinallyLineNumberTest.java

Wed, 07 Jun 2017 00:04:12 -0700

author
shshahma
date
Wed, 07 Jun 2017 00:04:12 -0700
changeset 3371
7220be8747f0
parent 3000
4044eb07194d
permissions
-rw-r--r--

8180660: missing LNT entry for finally block
Reviewed-by: mcimadamore, vromero
Contributed-by: maurizio.cimadamore@oracle.com, vicente.romero@oracle.com

aeriksso@3000 1 /*
shshahma@3371 2 * Copyright (c) 2015, 2017, 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.IOException;
aeriksso@3000 40
aeriksso@3000 41 public class FinallyLineNumberTest {
aeriksso@3000 42 public static void main(String[] args) throws Exception {
aeriksso@3000 43 // check that we have 5 consecutive entries for method()
aeriksso@3000 44 Entry[] lines = findEntries();
aeriksso@3000 45 if (lines == null) {
aeriksso@3000 46 throw new Exception("finally line number table could not be loaded");
aeriksso@3000 47 }
shshahma@3371 48 if (lines.length != 5) {
aeriksso@3000 49 // Help debug
aeriksso@3000 50 System.err.println("LineTable error, got lines:");
aeriksso@3000 51 for (Entry e : lines) {
aeriksso@3000 52 System.err.println(e.line_number);
aeriksso@3000 53 }
shshahma@3371 54 throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=5");
aeriksso@3000 55 }
aeriksso@3000 56
aeriksso@3000 57 // return null line, for the load null operation
aeriksso@3000 58 int current = lines[0].line_number;
aeriksso@3000 59 int first = current;
aeriksso@3000 60
aeriksso@3000 61 // finally line
aeriksso@3000 62 current = lines[1].line_number;
aeriksso@3000 63 if (current != first + 2) {
aeriksso@3000 64 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2));
aeriksso@3000 65 }
aeriksso@3000 66
aeriksso@3000 67 // return null line, for the return operation
aeriksso@3000 68 current = lines[2].line_number;
aeriksso@3000 69 if (current != first) {
aeriksso@3000 70 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + first);
aeriksso@3000 71 }
aeriksso@3000 72
shshahma@3371 73 // for when exception is thrown
aeriksso@3000 74 current = lines[3].line_number;
aeriksso@3000 75 if (current != first + 2) {
aeriksso@3000 76 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2));
aeriksso@3000 77 }
shshahma@3371 78
shshahma@3371 79 // the '}' closing the finally block
shshahma@3371 80 current = lines[4].line_number;
shshahma@3371 81 if (current != first + 3) {
shshahma@3371 82 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 3));
shshahma@3371 83 }
aeriksso@3000 84 }
aeriksso@3000 85
aeriksso@3000 86 static Entry[] findEntries() throws IOException, ConstantPoolException {
aeriksso@3000 87 ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class"));
aeriksso@3000 88 for (Method m : self.methods) {
aeriksso@3000 89 if ("method".equals(m.getName(self.constant_pool))) {
aeriksso@3000 90 Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
aeriksso@3000 91 for (Attribute at : code_attribute.attributes) {
aeriksso@3000 92 if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
aeriksso@3000 93 return ((LineNumberTable_attribute)at).line_number_table;
aeriksso@3000 94 }
aeriksso@3000 95 }
aeriksso@3000 96 }
aeriksso@3000 97 }
aeriksso@3000 98 return null;
aeriksso@3000 99 }
aeriksso@3000 100
aeriksso@3000 101 // This method should get LineNumberTable entries for:
aeriksso@3000 102 // *) The load of the null
aeriksso@3000 103 // *) The finally code for when an exception is *not* thrown
aeriksso@3000 104 // *) The actual return, which should have the same line as the load of the null
aeriksso@3000 105 // *) The finally code for when an exception *is* thrown, should have the same line as above finally code
aeriksso@3000 106 public static String method(int field) {
aeriksso@3000 107 try {
aeriksso@3000 108 return null;
aeriksso@3000 109 } finally {
aeriksso@3000 110 field+=1; // Dummy
aeriksso@3000 111 }
aeriksso@3000 112 }
aeriksso@3000 113 }

mercurial