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