langtools/test/tools/javac/T8019486/WrongLVTForLambdaTest.java
changeset 21637 fcaf599f308f
parent 21636 fcec9002d5f4
parent 21533 14f6dddbb916
child 21638 d51c48db93d8
equal deleted inserted replaced
21636:fcec9002d5f4 21637:fcaf599f308f
     1 /*
       
     2  * Copyright (c) 2013, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * @test
       
    28  * @bug 8019486
       
    29  * @summary javac, generates erroneous LVT for a test case with lambda code
       
    30  * @library /tools/javac/lib
       
    31  * @build ToolBox
       
    32  * @run main WrongLVTForLambdaTest
       
    33  */
       
    34 
       
    35 import java.io.File;
       
    36 import java.nio.file.Paths;
       
    37 
       
    38 import com.sun.tools.classfile.ClassFile;
       
    39 import com.sun.tools.classfile.Code_attribute;
       
    40 import com.sun.tools.classfile.LineNumberTable_attribute;
       
    41 import com.sun.tools.classfile.Method;
       
    42 import com.sun.tools.javac.util.Assert;
       
    43 
       
    44 public class WrongLVTForLambdaTest {
       
    45 
       
    46     static final String testSource =
       
    47     /* 01 */        "import java.util.List;\n" +
       
    48     /* 02 */        "import java.util.Arrays;\n" +
       
    49     /* 03 */        "import java.util.stream.Collectors;\n" +
       
    50     /* 04 */        "\n" +
       
    51     /* 05 */        "public class Foo {\n" +
       
    52     /* 06 */        "    void bar(int value) {\n" +
       
    53     /* 07 */        "        final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" +
       
    54     /* 08 */        "        final List<Integer> numbersPlusOne = \n" +
       
    55     /* 09 */        "             numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" +
       
    56     /* 10 */        "    }\n" +
       
    57     /* 11 */        "}";
       
    58 
       
    59     static final int[][] expectedLNT = {
       
    60     //  {line-number, start-pc},
       
    61         {9,           0},       //number -> number / 1
       
    62     };
       
    63 
       
    64     static final String methodToLookFor = "lambda$0";
       
    65 
       
    66     public static void main(String[] args) throws Exception {
       
    67         new WrongLVTForLambdaTest().run();
       
    68     }
       
    69 
       
    70     void run() throws Exception {
       
    71         compileTestClass();
       
    72         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
       
    73                 "Foo.class").toUri()), methodToLookFor);
       
    74     }
       
    75 
       
    76     void compileTestClass() throws Exception {
       
    77         ToolBox.JavaToolArgs javacSuccessArgs =
       
    78                 new ToolBox.JavaToolArgs().setSources(testSource);
       
    79         ToolBox.javac(javacSuccessArgs);
       
    80     }
       
    81 
       
    82     void checkClassFile(final File cfile, String methodToFind) throws Exception {
       
    83         ClassFile classFile = ClassFile.read(cfile);
       
    84         boolean methodFound = false;
       
    85         for (Method method : classFile.methods) {
       
    86             if (method.getName(classFile.constant_pool).equals(methodToFind)) {
       
    87                 methodFound = true;
       
    88                 Code_attribute code = (Code_attribute) method.attributes.get("Code");
       
    89                 LineNumberTable_attribute lnt =
       
    90                         (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
       
    91                 Assert.check(lnt.line_number_table_length == expectedLNT.length,
       
    92                         "The LineNumberTable found has a length different to the expected one");
       
    93                 int i = 0;
       
    94                 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
       
    95                     Assert.check(entry.line_number == expectedLNT[i][0] &&
       
    96                             entry.start_pc == expectedLNT[i][1],
       
    97                             "LNT entry at pos " + i + " differ from expected." +
       
    98                             "Found " + entry.line_number + ":" + entry.start_pc +
       
    99                             ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
       
   100                     i++;
       
   101                 }
       
   102             }
       
   103         }
       
   104         Assert.check(methodFound, "The seek method was not found");
       
   105     }
       
   106 
       
   107     void error(String msg) {
       
   108         throw new AssertionError(msg);
       
   109     }
       
   110 
       
   111 }