test/tools/javac/linenumbers/FinallyLineNumberTest.java

Mon, 17 Oct 2016 21:20:17 -0700

author
asaha
date
Mon, 17 Oct 2016 21:20:17 -0700
changeset 3306
867c01c1ae44
parent 3000
4044eb07194d
child 3371
7220be8747f0
permissions
-rw-r--r--

8168115: Remove and retag jdk8u122-b03 in 8u repo
Reviewed-by: coffeys

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

mercurial