test/tools/javac/T8019486/WrongLNTForLambdaTest.java

Wed, 23 Oct 2013 07:50:04 +0200

author
jlahoda
date
Wed, 23 Oct 2013 07:50:04 +0200
changeset 2165
864dafc5ab7a
child 2186
6e0f31d61e56
permissions
-rw-r--r--

8026861: Wrong LineNumberTable for variable declarations in lambdas
Summary: Setting or correcting positions for many trees produced by LambdaToMethod.
Reviewed-by: vromero, rfield

     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  */
    26 /*
    27  * @test
    28  * @bug 8019486 8026861
    29  * @summary javac, generates erroneous LVT for a test case with lambda code
    30  * @library /tools/javac/lib
    31  * @build ToolBox
    32  * @run main WrongLNTForLambdaTest
    33  */
    35 import java.io.File;
    36 import java.nio.file.Paths;
    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;
    44 public class WrongLNTForLambdaTest {
    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 */        "    void variablesInLambdas(int value) {\n" +
    58     /* 12 */        "        Runnable r1 = () -> {\n" +
    59     /* 13 */        "            int i  = value;\n" +
    60     /* 14 */        "            class FooBar<T extends CharSequence> {\n" +
    61     /* 15 */        "                public void run() {\n" +
    62     /* 16 */        "                    T t = null;\n" +
    63     /* 17 */        "                }\n" +
    64     /* 18 */        "            }\n" +
    65     /* 19 */        "        };\n" +
    66     /* 20 */        "        Runnable r2 = () -> System.err.println(1);\n" +
    67     /* 21 */        "        Runnable r3 = (Runnable & java.io.Serializable) this::foo;\n" +
    68     /* 22 */        "        Runnable r4 = super :: notify;\n" +
    69     /* 23 */        "    }\n" +
    70     /* 24 */        "    private void foo() {}\n" +
    71     /* 25 */        "}";
    73     static final int[][] simpleLambdaExpectedLNT = {
    74     //  {line-number, start-pc},
    75         {9,           0},       //number -> number / 1
    76     };
    78     static final int[][] lambdaWithVarsExpectedLNT = {
    79     //  {line-number, start-pc},
    80         {13,           0},       //number -> number / 1
    81         {19,           2},       //number -> number / 1
    82     };
    84     static final int[][] insideLambdaWithVarsExpectedLNT = {
    85     //  {line-number, start-pc},
    86         {16,           0},       //number -> number / 1
    87         {17,           2},       //number -> number / 1
    88     };
    90     static final int[][] lambdaVoid2VoidExpectedLNT = {
    91     //  {line-number, start-pc},
    92         {20,           0},       //number -> number / 1
    93     };
    95     static final int[][] deserializeExpectedLNT = {
    96     //  {line-number, start-pc},
    97         {05,           0},       //number -> number / 1
    98     };
   100     static final int[][] lambdaBridgeExpectedLNT = {
   101     //  {line-number, start-pc},
   102         {22,           0},       //number -> number / 1
   103     };
   105     public static void main(String[] args) throws Exception {
   106         new WrongLNTForLambdaTest().run();
   107     }
   109     void run() throws Exception {
   110         compileTestClass();
   111         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   112                 "Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT);
   113         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   114                 "Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT);
   115         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   116                 "Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT);
   117         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   118                 "Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT);
   119         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   120                 "Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT);
   121         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   122                 "Foo.class").toUri()), "lambda$MR$variablesInLambdas$notify$8bc4f5bd$1", lambdaBridgeExpectedLNT);
   123     }
   125     void compileTestClass() throws Exception {
   126         ToolBox.JavaToolArgs javacSuccessArgs =
   127                 new ToolBox.JavaToolArgs().setSources(testSource);
   128         ToolBox.javac(javacSuccessArgs);
   129     }
   131     void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception {
   132         ClassFile classFile = ClassFile.read(cfile);
   133         boolean methodFound = false;
   134         for (Method method : classFile.methods) {
   135             if (method.getName(classFile.constant_pool).equals(methodToFind)) {
   136                 methodFound = true;
   137                 Code_attribute code = (Code_attribute) method.attributes.get("Code");
   138                 LineNumberTable_attribute lnt =
   139                         (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
   140                 Assert.check(lnt.line_number_table_length == expectedLNT.length,
   141                         "The LineNumberTable found has a length different to the expected one");
   142                 int i = 0;
   143                 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
   144                     Assert.check(entry.line_number == expectedLNT[i][0] &&
   145                             entry.start_pc == expectedLNT[i][1],
   146                             "LNT entry at pos " + i + " differ from expected." +
   147                             "Found " + entry.line_number + ":" + entry.start_pc +
   148                             ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
   149                     i++;
   150                 }
   151             }
   152         }
   153         Assert.check(methodFound, "The seek method was not found");
   154     }
   156     void error(String msg) {
   157         throw new AssertionError(msg);
   158     }
   160 }

mercurial