1 /*
   2  * Copyright (c) 2013, 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  */
  23 
  24 /*
  25  * @test
  26  * @bug 7008643
  27  * @summary inlined finally clauses confuse debuggers
  28  * @library /tools/lib
  29  * @modules jdk.compiler/com.sun.tools.classfile
  30  *          jdk.compiler/com.sun.tools.javac.api
  31  *          jdk.compiler/com.sun.tools.javac.file
  32  *          jdk.compiler/com.sun.tools.javac.main
  33  *          jdk.compiler/com.sun.tools.javac.util
  34  * @build ToolBox
  35  * @run main InlinedFinallyConfuseDebuggersTest
  36  */
  37 
  38 import java.io.File;
  39 import java.nio.file.Paths;
  40 
  41 import com.sun.tools.classfile.ClassFile;
  42 import com.sun.tools.classfile.Code_attribute;
  43 import com.sun.tools.classfile.LineNumberTable_attribute;
  44 import com.sun.tools.classfile.Method;
  45 import com.sun.tools.javac.util.Assert;
  46 
  47 public class InlinedFinallyConfuseDebuggersTest {
  48 
  49     static final String testSource =
  50     /* 01 */        "public class InlinedFinallyTest {\n" +
  51     /* 02 */        "    void lookForThisMethod(int value) {\n" +
  52     /* 03 */        "        try {\n" +
  53     /* 04 */        "            if (value > 0) {\n" +
  54     /* 05 */        "                System.out.println(\"if\");\n" +
  55     /* 06 */        "                return;\n" +
  56     /* 07 */        "            }\n" +
  57     /* 08 */        "        } finally {\n" +
  58     /* 09 */        "            System.out.println(\"finally\");\n" +
  59     /* 10 */        "        }\n" +
  60     /* 11 */        "    }\n" +
  61     /* 12 */        "}";
  62 
  63     static final int[][] expectedLNT = {
  64     //  {line-number, start-pc},
  65         {4,           0},       //if (value > 0) {
  66         {5,           4},       //    System.out.println("if");
  67         {9,           12},      //System.out.println("finally");
  68         {6,           20},      //    return;
  69         {9,           21},      //System.out.println("finally");
  70         {10,          29},
  71         {9,           32},      //System.out.println("finally");
  72         {11,          43},
  73     };
  74 
  75     static final String methodToLookFor = "lookForThisMethod";
  76 
  77     public static void main(String[] args) throws Exception {
  78         new InlinedFinallyConfuseDebuggersTest().run();
  79     }
  80 
  81     ToolBox tb = new ToolBox();
  82 
  83     void run() throws Exception {
  84         compileTestClass();
  85         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
  86                 "InlinedFinallyTest.class").toUri()), methodToLookFor);
  87     }
  88 
  89     void compileTestClass() throws Exception {
  90         tb.new JavacTask()
  91                 .sources(testSource)
  92                 .run();
  93     }
  94 
  95     void checkClassFile(final File cfile, String methodToFind) throws Exception {
  96         ClassFile classFile = ClassFile.read(cfile);
  97         boolean methodFound = false;
  98         for (Method method : classFile.methods) {
  99             if (method.getName(classFile.constant_pool).equals(methodToFind)) {
 100                 methodFound = true;
 101                 Code_attribute code = (Code_attribute) method.attributes.get("Code");
 102                 LineNumberTable_attribute lnt =
 103                         (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
 104                 Assert.check(lnt.line_number_table_length == expectedLNT.length,
 105                         "The LineNumberTable found has a length different to the expected one");
 106                 int i = 0;
 107                 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
 108                     Assert.check(entry.line_number == expectedLNT[i][0] &&
 109                             entry.start_pc == expectedLNT[i][1],
 110                             "LNT entry at pos " + i + " differ from expected." +
 111                             "Found " + entry.line_number + ":" + entry.start_pc +
 112                             ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
 113                     i++;
 114                 }
 115             }
 116         }
 117         Assert.check(methodFound, "The seek method was not found");
 118     }
 119 
 120     void error(String msg) {
 121         throw new AssertionError(msg);
 122     }
 123 
 124 }