test/langtools/tools/javac/T8192885/AddGotoAfterForLoopToLNTTest.java
changeset 48819 ee513596f3ee
parent 48818 ec4a84ba2aaf
parent 48680 f944d1b7ab25
child 48820 9a411a9a17f0
equal deleted inserted replaced
48818:ec4a84ba2aaf 48819:ee513596f3ee
     1 /*
       
     2  * Copyright (c) 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  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8192885
       
    27  * @summary Compiler in JDK 10-ea+33 misses to include entry in LineNumberTable for goto instruction of foreach loop
       
    28  * @library /tools/lib
       
    29  * @modules jdk.jdeps/com.sun.tools.classfile
       
    30  *          jdk.compiler/com.sun.tools.javac.api
       
    31  *          jdk.compiler/com.sun.tools.javac.main
       
    32  *          jdk.compiler/com.sun.tools.javac.util
       
    33  *          jdk.jdeps/com.sun.tools.javap
       
    34  * @build toolbox.ToolBox toolbox.JavacTask
       
    35  * @run main AddGotoAfterForLoopToLNTTest
       
    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 import toolbox.JavacTask;
       
    48 import toolbox.ToolBox;
       
    49 
       
    50 public class AddGotoAfterForLoopToLNTTest {
       
    51 
       
    52     static final String testSource =
       
    53     /* 01 */        "class GotoAtEnhancedForTest {\n" +
       
    54     /* 02 */        "    void lookForThisMethod() {\n" +
       
    55     /* 03 */        "        for (Object o : java.util.Collections.emptyList()) {\n" +
       
    56     /* 04 */        "        }\n" +
       
    57     /* 05 */        "    }\n" +
       
    58     /* 06 */        "}";
       
    59 
       
    60     static final int[][] expectedLNT = {
       
    61     //  {line-number, start-pc},
       
    62         {3,           0},
       
    63         {4,           25},
       
    64         {3,           28},
       
    65         {5,           30},
       
    66     };
       
    67 
       
    68     static final String methodToLookFor = "lookForThisMethod";
       
    69 
       
    70     public static void main(String[] args) throws Exception {
       
    71         new AddGotoAfterForLoopToLNTTest().run();
       
    72     }
       
    73 
       
    74     ToolBox tb = new ToolBox();
       
    75 
       
    76     void run() throws Exception {
       
    77         compileTestClass();
       
    78         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
       
    79                 "GotoAtEnhancedForTest.class").toUri()), methodToLookFor);
       
    80     }
       
    81 
       
    82     void compileTestClass() throws Exception {
       
    83         new JavacTask(tb)
       
    84                 .sources(testSource)
       
    85                 .run();
       
    86     }
       
    87 
       
    88     void checkClassFile(final File cfile, String methodToFind) throws Exception {
       
    89         ClassFile classFile = ClassFile.read(cfile);
       
    90         boolean methodFound = false;
       
    91         for (Method method : classFile.methods) {
       
    92             if (method.getName(classFile.constant_pool).equals(methodToFind)) {
       
    93                 methodFound = true;
       
    94                 Code_attribute code = (Code_attribute) method.attributes.get("Code");
       
    95                 LineNumberTable_attribute lnt =
       
    96                         (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
       
    97                 Assert.check(lnt.line_number_table_length == expectedLNT.length,
       
    98                         "The LineNumberTable found has a length different to the expected one");
       
    99                 int i = 0;
       
   100                 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
       
   101                     Assert.check(entry.line_number == expectedLNT[i][0] &&
       
   102                             entry.start_pc == expectedLNT[i][1],
       
   103                             "LNT entry at pos " + i + " differ from expected." +
       
   104                             "Found " + entry.line_number + ":" + entry.start_pc +
       
   105                             ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
       
   106                     i++;
       
   107                 }
       
   108             }
       
   109         }
       
   110         Assert.check(methodFound, "The seek method was not found");
       
   111     }
       
   112 
       
   113     void error(String msg) {
       
   114         throw new AssertionError(msg);
       
   115     }
       
   116 }