test/tools/javac/T8019486/WrongLNTForLambdaTest.java

Wed, 13 Aug 2014 14:50:00 -0700

author
katleman
date
Wed, 13 Aug 2014 14:50:00 -0700
changeset 2549
0b6cc4ea670f
parent 2186
6e0f31d61e56
child 2525
2eb010b6cb22
child 2607
10e9228e77b0
permissions
-rw-r--r--

Added tag jdk8u40-b01 for changeset bf89a471779d

     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 8027142
    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 */        "    void assignLambda() {\n" +
    72     /* 26 */        "        Runnable r = () -> { };\n" +
    73     /* 27 */        "    }\n" +
    74     /* 28 */        "    void callLambda(int i, Runnable r) {\n" +
    75     /* 29 */        "        callLambda(0,\n" +
    76     /* 30 */        "                   () -> { });\n" +
    77     /* 31 */        "    }\n" +
    78     /* 32 */        "}";
    80     static final int[][] simpleLambdaExpectedLNT = {
    81     //  {line-number, start-pc},
    82         {9,           0},       //number -> number / 1
    83     };
    85     static final int[][] lambdaWithVarsExpectedLNT = {
    86     //  {line-number, start-pc},
    87         {13,           0},       //number -> number / 1
    88         {19,           2},       //number -> number / 1
    89     };
    91     static final int[][] insideLambdaWithVarsExpectedLNT = {
    92     //  {line-number, start-pc},
    93         {16,           0},       //number -> number / 1
    94         {17,           2},       //number -> number / 1
    95     };
    97     static final int[][] lambdaVoid2VoidExpectedLNT = {
    98     //  {line-number, start-pc},
    99         {20,           0},       //number -> number / 1
   100     };
   102     static final int[][] deserializeExpectedLNT = {
   103     //  {line-number, start-pc},
   104         {05,           0},       //number -> number / 1
   105     };
   107     static final int[][] lambdaBridgeExpectedLNT = {
   108     //  {line-number, start-pc},
   109         {22,           0},       //number -> number / 1
   110     };
   112     static final int[][] assignmentExpectedLNT = {
   113     //  {line-number, start-pc},
   114         {26,           0},       //number -> number / 1
   115         {27,           6},       //number -> number / 1
   116     };
   118     static final int[][] callExpectedLNT = {
   119     //  {line-number, start-pc},
   120         {29,           0},       //number -> number / 1
   121         {31,           10},       //number -> number / 1
   122     };
   124     public static void main(String[] args) throws Exception {
   125         new WrongLNTForLambdaTest().run();
   126     }
   128     void run() throws Exception {
   129         compileTestClass();
   130         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   131                 "Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT);
   132         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   133                 "Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT);
   134         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   135                 "Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT);
   136         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   137                 "Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT);
   138         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   139                 "Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT);
   140         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   141                 "Foo.class").toUri()), "lambda$MR$variablesInLambdas$notify$8bc4f5bd$1", lambdaBridgeExpectedLNT);
   142         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   143                 "Foo.class").toUri()), "assignLambda", assignmentExpectedLNT);
   144         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
   145                 "Foo.class").toUri()), "callLambda", callExpectedLNT);
   146     }
   148     void compileTestClass() throws Exception {
   149         ToolBox.JavaToolArgs javacSuccessArgs =
   150                 new ToolBox.JavaToolArgs().setSources(testSource);
   151         ToolBox.javac(javacSuccessArgs);
   152     }
   154     void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception {
   155         ClassFile classFile = ClassFile.read(cfile);
   156         boolean methodFound = false;
   157         for (Method method : classFile.methods) {
   158             if (method.getName(classFile.constant_pool).equals(methodToFind)) {
   159                 methodFound = true;
   160                 Code_attribute code = (Code_attribute) method.attributes.get("Code");
   161                 LineNumberTable_attribute lnt =
   162                         (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
   163                 Assert.check(lnt.line_number_table_length == expectedLNT.length,
   164                         "The LineNumberTable found has a length different to the expected one");
   165                 int i = 0;
   166                 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
   167                     Assert.check(entry.line_number == expectedLNT[i][0] &&
   168                             entry.start_pc == expectedLNT[i][1],
   169                             "LNT entry at pos " + i + " differ from expected." +
   170                             "Found " + entry.line_number + ":" + entry.start_pc +
   171                             ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
   172                     i++;
   173                 }
   174             }
   175         }
   176         Assert.check(methodFound, "The seek method was not found");
   177     }
   179     void error(String msg) {
   180         throw new AssertionError(msg);
   181     }
   183 }

mercurial