test/tools/javac/linenumbers/FinallyLineNumberTest.java

Sat, 04 Aug 2018 12:55:17 +0100

author
aefimov
date
Sat, 04 Aug 2018 12:55:17 +0100
changeset 3736
7b508cdd33ae
parent 3371
7220be8747f0
permissions
-rw-r--r--

Merge

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

mercurial