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

jlahoda@2165 1 /*
jlahoda@2165 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
jlahoda@2165 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlahoda@2165 4 *
jlahoda@2165 5 * This code is free software; you can redistribute it and/or modify it
jlahoda@2165 6 * under the terms of the GNU General Public License version 2 only, as
jlahoda@2165 7 * published by the Free Software Foundation. Oracle designates this
jlahoda@2165 8 * particular file as subject to the "Classpath" exception as provided
jlahoda@2165 9 * by Oracle in the LICENSE file that accompanied this code.
jlahoda@2165 10 *
jlahoda@2165 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlahoda@2165 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlahoda@2165 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlahoda@2165 14 * version 2 for more details (a copy is included in the LICENSE file that
jlahoda@2165 15 * accompanied this code).
jlahoda@2165 16 *
jlahoda@2165 17 * You should have received a copy of the GNU General Public License version
jlahoda@2165 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlahoda@2165 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlahoda@2165 20 *
jlahoda@2165 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlahoda@2165 22 * or visit www.oracle.com if you need additional information or have any
jlahoda@2165 23 * questions.
jlahoda@2165 24 */
jlahoda@2165 25
jlahoda@2165 26 /*
jlahoda@2165 27 * @test
jlahoda@2165 28 * @bug 8019486 8026861
jlahoda@2165 29 * @summary javac, generates erroneous LVT for a test case with lambda code
jlahoda@2165 30 * @library /tools/javac/lib
jlahoda@2165 31 * @build ToolBox
jlahoda@2165 32 * @run main WrongLNTForLambdaTest
jlahoda@2165 33 */
jlahoda@2165 34
jlahoda@2165 35 import java.io.File;
jlahoda@2165 36 import java.nio.file.Paths;
jlahoda@2165 37
jlahoda@2165 38 import com.sun.tools.classfile.ClassFile;
jlahoda@2165 39 import com.sun.tools.classfile.Code_attribute;
jlahoda@2165 40 import com.sun.tools.classfile.LineNumberTable_attribute;
jlahoda@2165 41 import com.sun.tools.classfile.Method;
jlahoda@2165 42 import com.sun.tools.javac.util.Assert;
jlahoda@2165 43
jlahoda@2165 44 public class WrongLNTForLambdaTest {
jlahoda@2165 45
jlahoda@2165 46 static final String testSource =
jlahoda@2165 47 /* 01 */ "import java.util.List;\n" +
jlahoda@2165 48 /* 02 */ "import java.util.Arrays;\n" +
jlahoda@2165 49 /* 03 */ "import java.util.stream.Collectors;\n" +
jlahoda@2165 50 /* 04 */ "\n" +
jlahoda@2165 51 /* 05 */ "public class Foo {\n" +
jlahoda@2165 52 /* 06 */ " void bar(int value) {\n" +
jlahoda@2165 53 /* 07 */ " final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" +
jlahoda@2165 54 /* 08 */ " final List<Integer> numbersPlusOne = \n" +
jlahoda@2165 55 /* 09 */ " numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" +
jlahoda@2165 56 /* 10 */ " }\n" +
jlahoda@2165 57 /* 11 */ " void variablesInLambdas(int value) {\n" +
jlahoda@2165 58 /* 12 */ " Runnable r1 = () -> {\n" +
jlahoda@2165 59 /* 13 */ " int i = value;\n" +
jlahoda@2165 60 /* 14 */ " class FooBar<T extends CharSequence> {\n" +
jlahoda@2165 61 /* 15 */ " public void run() {\n" +
jlahoda@2165 62 /* 16 */ " T t = null;\n" +
jlahoda@2165 63 /* 17 */ " }\n" +
jlahoda@2165 64 /* 18 */ " }\n" +
jlahoda@2165 65 /* 19 */ " };\n" +
jlahoda@2165 66 /* 20 */ " Runnable r2 = () -> System.err.println(1);\n" +
jlahoda@2165 67 /* 21 */ " Runnable r3 = (Runnable & java.io.Serializable) this::foo;\n" +
jlahoda@2165 68 /* 22 */ " Runnable r4 = super :: notify;\n" +
jlahoda@2165 69 /* 23 */ " }\n" +
jlahoda@2165 70 /* 24 */ " private void foo() {}\n" +
jlahoda@2165 71 /* 25 */ "}";
jlahoda@2165 72
jlahoda@2165 73 static final int[][] simpleLambdaExpectedLNT = {
jlahoda@2165 74 // {line-number, start-pc},
jlahoda@2165 75 {9, 0}, //number -> number / 1
jlahoda@2165 76 };
jlahoda@2165 77
jlahoda@2165 78 static final int[][] lambdaWithVarsExpectedLNT = {
jlahoda@2165 79 // {line-number, start-pc},
jlahoda@2165 80 {13, 0}, //number -> number / 1
jlahoda@2165 81 {19, 2}, //number -> number / 1
jlahoda@2165 82 };
jlahoda@2165 83
jlahoda@2165 84 static final int[][] insideLambdaWithVarsExpectedLNT = {
jlahoda@2165 85 // {line-number, start-pc},
jlahoda@2165 86 {16, 0}, //number -> number / 1
jlahoda@2165 87 {17, 2}, //number -> number / 1
jlahoda@2165 88 };
jlahoda@2165 89
jlahoda@2165 90 static final int[][] lambdaVoid2VoidExpectedLNT = {
jlahoda@2165 91 // {line-number, start-pc},
jlahoda@2165 92 {20, 0}, //number -> number / 1
jlahoda@2165 93 };
jlahoda@2165 94
jlahoda@2165 95 static final int[][] deserializeExpectedLNT = {
jlahoda@2165 96 // {line-number, start-pc},
jlahoda@2165 97 {05, 0}, //number -> number / 1
jlahoda@2165 98 };
jlahoda@2165 99
jlahoda@2165 100 static final int[][] lambdaBridgeExpectedLNT = {
jlahoda@2165 101 // {line-number, start-pc},
jlahoda@2165 102 {22, 0}, //number -> number / 1
jlahoda@2165 103 };
jlahoda@2165 104
jlahoda@2165 105 public static void main(String[] args) throws Exception {
jlahoda@2165 106 new WrongLNTForLambdaTest().run();
jlahoda@2165 107 }
jlahoda@2165 108
jlahoda@2165 109 void run() throws Exception {
jlahoda@2165 110 compileTestClass();
jlahoda@2165 111 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
jlahoda@2165 112 "Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT);
jlahoda@2165 113 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
jlahoda@2165 114 "Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT);
jlahoda@2165 115 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
jlahoda@2165 116 "Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT);
jlahoda@2165 117 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
jlahoda@2165 118 "Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT);
jlahoda@2165 119 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
jlahoda@2165 120 "Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT);
jlahoda@2165 121 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
jlahoda@2165 122 "Foo.class").toUri()), "lambda$MR$variablesInLambdas$notify$8bc4f5bd$1", lambdaBridgeExpectedLNT);
jlahoda@2165 123 }
jlahoda@2165 124
jlahoda@2165 125 void compileTestClass() throws Exception {
jlahoda@2165 126 ToolBox.JavaToolArgs javacSuccessArgs =
jlahoda@2165 127 new ToolBox.JavaToolArgs().setSources(testSource);
jlahoda@2165 128 ToolBox.javac(javacSuccessArgs);
jlahoda@2165 129 }
jlahoda@2165 130
jlahoda@2165 131 void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception {
jlahoda@2165 132 ClassFile classFile = ClassFile.read(cfile);
jlahoda@2165 133 boolean methodFound = false;
jlahoda@2165 134 for (Method method : classFile.methods) {
jlahoda@2165 135 if (method.getName(classFile.constant_pool).equals(methodToFind)) {
jlahoda@2165 136 methodFound = true;
jlahoda@2165 137 Code_attribute code = (Code_attribute) method.attributes.get("Code");
jlahoda@2165 138 LineNumberTable_attribute lnt =
jlahoda@2165 139 (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
jlahoda@2165 140 Assert.check(lnt.line_number_table_length == expectedLNT.length,
jlahoda@2165 141 "The LineNumberTable found has a length different to the expected one");
jlahoda@2165 142 int i = 0;
jlahoda@2165 143 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
jlahoda@2165 144 Assert.check(entry.line_number == expectedLNT[i][0] &&
jlahoda@2165 145 entry.start_pc == expectedLNT[i][1],
jlahoda@2165 146 "LNT entry at pos " + i + " differ from expected." +
jlahoda@2165 147 "Found " + entry.line_number + ":" + entry.start_pc +
jlahoda@2165 148 ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
jlahoda@2165 149 i++;
jlahoda@2165 150 }
jlahoda@2165 151 }
jlahoda@2165 152 }
jlahoda@2165 153 Assert.check(methodFound, "The seek method was not found");
jlahoda@2165 154 }
jlahoda@2165 155
jlahoda@2165 156 void error(String msg) {
jlahoda@2165 157 throw new AssertionError(msg);
jlahoda@2165 158 }
jlahoda@2165 159
jlahoda@2165 160 }

mercurial