test/tools/javac/T8019486/WrongLNTForLambdaTest.java

changeset 2165
864dafc5ab7a
child 2186
6e0f31d61e56
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/T8019486/WrongLNTForLambdaTest.java	Wed Oct 23 07:50:04 2013 +0200
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @test
    1.31 + * @bug 8019486 8026861
    1.32 + * @summary javac, generates erroneous LVT for a test case with lambda code
    1.33 + * @library /tools/javac/lib
    1.34 + * @build ToolBox
    1.35 + * @run main WrongLNTForLambdaTest
    1.36 + */
    1.37 +
    1.38 +import java.io.File;
    1.39 +import java.nio.file.Paths;
    1.40 +
    1.41 +import com.sun.tools.classfile.ClassFile;
    1.42 +import com.sun.tools.classfile.Code_attribute;
    1.43 +import com.sun.tools.classfile.LineNumberTable_attribute;
    1.44 +import com.sun.tools.classfile.Method;
    1.45 +import com.sun.tools.javac.util.Assert;
    1.46 +
    1.47 +public class WrongLNTForLambdaTest {
    1.48 +
    1.49 +    static final String testSource =
    1.50 +    /* 01 */        "import java.util.List;\n" +
    1.51 +    /* 02 */        "import java.util.Arrays;\n" +
    1.52 +    /* 03 */        "import java.util.stream.Collectors;\n" +
    1.53 +    /* 04 */        "\n" +
    1.54 +    /* 05 */        "public class Foo {\n" +
    1.55 +    /* 06 */        "    void bar(int value) {\n" +
    1.56 +    /* 07 */        "        final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" +
    1.57 +    /* 08 */        "        final List<Integer> numbersPlusOne = \n" +
    1.58 +    /* 09 */        "             numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" +
    1.59 +    /* 10 */        "    }\n" +
    1.60 +    /* 11 */        "    void variablesInLambdas(int value) {\n" +
    1.61 +    /* 12 */        "        Runnable r1 = () -> {\n" +
    1.62 +    /* 13 */        "            int i  = value;\n" +
    1.63 +    /* 14 */        "            class FooBar<T extends CharSequence> {\n" +
    1.64 +    /* 15 */        "                public void run() {\n" +
    1.65 +    /* 16 */        "                    T t = null;\n" +
    1.66 +    /* 17 */        "                }\n" +
    1.67 +    /* 18 */        "            }\n" +
    1.68 +    /* 19 */        "        };\n" +
    1.69 +    /* 20 */        "        Runnable r2 = () -> System.err.println(1);\n" +
    1.70 +    /* 21 */        "        Runnable r3 = (Runnable & java.io.Serializable) this::foo;\n" +
    1.71 +    /* 22 */        "        Runnable r4 = super :: notify;\n" +
    1.72 +    /* 23 */        "    }\n" +
    1.73 +    /* 24 */        "    private void foo() {}\n" +
    1.74 +    /* 25 */        "}";
    1.75 +
    1.76 +    static final int[][] simpleLambdaExpectedLNT = {
    1.77 +    //  {line-number, start-pc},
    1.78 +        {9,           0},       //number -> number / 1
    1.79 +    };
    1.80 +
    1.81 +    static final int[][] lambdaWithVarsExpectedLNT = {
    1.82 +    //  {line-number, start-pc},
    1.83 +        {13,           0},       //number -> number / 1
    1.84 +        {19,           2},       //number -> number / 1
    1.85 +    };
    1.86 +
    1.87 +    static final int[][] insideLambdaWithVarsExpectedLNT = {
    1.88 +    //  {line-number, start-pc},
    1.89 +        {16,           0},       //number -> number / 1
    1.90 +        {17,           2},       //number -> number / 1
    1.91 +    };
    1.92 +
    1.93 +    static final int[][] lambdaVoid2VoidExpectedLNT = {
    1.94 +    //  {line-number, start-pc},
    1.95 +        {20,           0},       //number -> number / 1
    1.96 +    };
    1.97 +
    1.98 +    static final int[][] deserializeExpectedLNT = {
    1.99 +    //  {line-number, start-pc},
   1.100 +        {05,           0},       //number -> number / 1
   1.101 +    };
   1.102 +
   1.103 +    static final int[][] lambdaBridgeExpectedLNT = {
   1.104 +    //  {line-number, start-pc},
   1.105 +        {22,           0},       //number -> number / 1
   1.106 +    };
   1.107 +
   1.108 +    public static void main(String[] args) throws Exception {
   1.109 +        new WrongLNTForLambdaTest().run();
   1.110 +    }
   1.111 +
   1.112 +    void run() throws Exception {
   1.113 +        compileTestClass();
   1.114 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   1.115 +                "Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT);
   1.116 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   1.117 +                "Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT);
   1.118 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   1.119 +                "Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT);
   1.120 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   1.121 +                "Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT);
   1.122 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   1.123 +                "Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT);
   1.124 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   1.125 +                "Foo.class").toUri()), "lambda$MR$variablesInLambdas$notify$8bc4f5bd$1", lambdaBridgeExpectedLNT);
   1.126 +    }
   1.127 +
   1.128 +    void compileTestClass() throws Exception {
   1.129 +        ToolBox.JavaToolArgs javacSuccessArgs =
   1.130 +                new ToolBox.JavaToolArgs().setSources(testSource);
   1.131 +        ToolBox.javac(javacSuccessArgs);
   1.132 +    }
   1.133 +
   1.134 +    void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception {
   1.135 +        ClassFile classFile = ClassFile.read(cfile);
   1.136 +        boolean methodFound = false;
   1.137 +        for (Method method : classFile.methods) {
   1.138 +            if (method.getName(classFile.constant_pool).equals(methodToFind)) {
   1.139 +                methodFound = true;
   1.140 +                Code_attribute code = (Code_attribute) method.attributes.get("Code");
   1.141 +                LineNumberTable_attribute lnt =
   1.142 +                        (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
   1.143 +                Assert.check(lnt.line_number_table_length == expectedLNT.length,
   1.144 +                        "The LineNumberTable found has a length different to the expected one");
   1.145 +                int i = 0;
   1.146 +                for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
   1.147 +                    Assert.check(entry.line_number == expectedLNT[i][0] &&
   1.148 +                            entry.start_pc == expectedLNT[i][1],
   1.149 +                            "LNT entry at pos " + i + " differ from expected." +
   1.150 +                            "Found " + entry.line_number + ":" + entry.start_pc +
   1.151 +                            ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
   1.152 +                    i++;
   1.153 +                }
   1.154 +            }
   1.155 +        }
   1.156 +        Assert.check(methodFound, "The seek method was not found");
   1.157 +    }
   1.158 +
   1.159 +    void error(String msg) {
   1.160 +        throw new AssertionError(msg);
   1.161 +    }
   1.162 +
   1.163 +}

mercurial