test/tools/javac/T8019486/WrongLVTForLambdaTest.java

changeset 2164
abc3eaccba73
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/T8019486/WrongLVTForLambdaTest.java	Wed Oct 23 23:02:17 2013 +0200
     1.3 @@ -0,0 +1,109 @@
     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
    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 WrongLVTForLambdaTest
    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 WrongLVTForLambdaTest {
    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 */        "}";
    1.61 +
    1.62 +    static final int[][] expectedLNT = {
    1.63 +    //  {line-number, start-pc},
    1.64 +        {9,           0},       //number -> number / 1
    1.65 +    };
    1.66 +
    1.67 +    public static void main(String[] args) throws Exception {
    1.68 +        new WrongLVTForLambdaTest().run();
    1.69 +    }
    1.70 +
    1.71 +    void run() throws Exception {
    1.72 +        compileTestClass();
    1.73 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
    1.74 +                "Foo.class").toUri()));
    1.75 +    }
    1.76 +
    1.77 +    void compileTestClass() throws Exception {
    1.78 +        ToolBox.JavaToolArgs javacSuccessArgs =
    1.79 +                new ToolBox.JavaToolArgs().setSources(testSource);
    1.80 +        ToolBox.javac(javacSuccessArgs);
    1.81 +    }
    1.82 +
    1.83 +    void checkClassFile(final File cfile) throws Exception {
    1.84 +        ClassFile classFile = ClassFile.read(cfile);
    1.85 +        int methodsFound = 0;
    1.86 +        for (Method method : classFile.methods) {
    1.87 +            if (method.getName(classFile.constant_pool).startsWith("lambda$")) {
    1.88 +                ++methodsFound;
    1.89 +                Code_attribute code = (Code_attribute) method.attributes.get("Code");
    1.90 +                LineNumberTable_attribute lnt =
    1.91 +                        (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
    1.92 +                Assert.check(lnt.line_number_table_length == expectedLNT.length,
    1.93 +                        "The LineNumberTable found has a length different to the expected one");
    1.94 +                int i = 0;
    1.95 +                for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
    1.96 +                    Assert.check(entry.line_number == expectedLNT[i][0] &&
    1.97 +                            entry.start_pc == expectedLNT[i][1],
    1.98 +                            "LNT entry at pos " + i + " differ from expected." +
    1.99 +                            "Found " + entry.line_number + ":" + entry.start_pc +
   1.100 +                            ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
   1.101 +                    i++;
   1.102 +                }
   1.103 +            }
   1.104 +        }
   1.105 +        Assert.check(methodsFound == 1, "Expected to find one lambda method, found " + methodsFound);
   1.106 +    }
   1.107 +
   1.108 +    void error(String msg) {
   1.109 +        throw new AssertionError(msg);
   1.110 +    }
   1.111 +
   1.112 +}

mercurial