jlahoda@2165: /* jlahoda@2165: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. jlahoda@2165: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jlahoda@2165: * jlahoda@2165: * This code is free software; you can redistribute it and/or modify it jlahoda@2165: * under the terms of the GNU General Public License version 2 only, as jlahoda@2165: * published by the Free Software Foundation. Oracle designates this jlahoda@2165: * particular file as subject to the "Classpath" exception as provided jlahoda@2165: * by Oracle in the LICENSE file that accompanied this code. jlahoda@2165: * jlahoda@2165: * This code is distributed in the hope that it will be useful, but WITHOUT jlahoda@2165: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jlahoda@2165: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jlahoda@2165: * version 2 for more details (a copy is included in the LICENSE file that jlahoda@2165: * accompanied this code). jlahoda@2165: * jlahoda@2165: * You should have received a copy of the GNU General Public License version jlahoda@2165: * 2 along with this work; if not, write to the Free Software Foundation, jlahoda@2165: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jlahoda@2165: * jlahoda@2165: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jlahoda@2165: * or visit www.oracle.com if you need additional information or have any jlahoda@2165: * questions. jlahoda@2165: */ jlahoda@2165: jlahoda@2165: /* jlahoda@2165: * @test jlahoda@2165: * @bug 8019486 8026861 jlahoda@2165: * @summary javac, generates erroneous LVT for a test case with lambda code jlahoda@2165: * @library /tools/javac/lib jlahoda@2165: * @build ToolBox jlahoda@2165: * @run main WrongLNTForLambdaTest jlahoda@2165: */ jlahoda@2165: jlahoda@2165: import java.io.File; jlahoda@2165: import java.nio.file.Paths; jlahoda@2165: jlahoda@2165: import com.sun.tools.classfile.ClassFile; jlahoda@2165: import com.sun.tools.classfile.Code_attribute; jlahoda@2165: import com.sun.tools.classfile.LineNumberTable_attribute; jlahoda@2165: import com.sun.tools.classfile.Method; jlahoda@2165: import com.sun.tools.javac.util.Assert; jlahoda@2165: jlahoda@2165: public class WrongLNTForLambdaTest { jlahoda@2165: jlahoda@2165: static final String testSource = jlahoda@2165: /* 01 */ "import java.util.List;\n" + jlahoda@2165: /* 02 */ "import java.util.Arrays;\n" + jlahoda@2165: /* 03 */ "import java.util.stream.Collectors;\n" + jlahoda@2165: /* 04 */ "\n" + jlahoda@2165: /* 05 */ "public class Foo {\n" + jlahoda@2165: /* 06 */ " void bar(int value) {\n" + jlahoda@2165: /* 07 */ " final List numbers = Arrays.asList(1, 2, 3);\n" + jlahoda@2165: /* 08 */ " final List numbersPlusOne = \n" + jlahoda@2165: /* 09 */ " numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" + jlahoda@2165: /* 10 */ " }\n" + jlahoda@2165: /* 11 */ " void variablesInLambdas(int value) {\n" + jlahoda@2165: /* 12 */ " Runnable r1 = () -> {\n" + jlahoda@2165: /* 13 */ " int i = value;\n" + jlahoda@2165: /* 14 */ " class FooBar {\n" + jlahoda@2165: /* 15 */ " public void run() {\n" + jlahoda@2165: /* 16 */ " T t = null;\n" + jlahoda@2165: /* 17 */ " }\n" + jlahoda@2165: /* 18 */ " }\n" + jlahoda@2165: /* 19 */ " };\n" + jlahoda@2165: /* 20 */ " Runnable r2 = () -> System.err.println(1);\n" + jlahoda@2165: /* 21 */ " Runnable r3 = (Runnable & java.io.Serializable) this::foo;\n" + jlahoda@2165: /* 22 */ " Runnable r4 = super :: notify;\n" + jlahoda@2165: /* 23 */ " }\n" + jlahoda@2165: /* 24 */ " private void foo() {}\n" + jlahoda@2165: /* 25 */ "}"; jlahoda@2165: jlahoda@2165: static final int[][] simpleLambdaExpectedLNT = { jlahoda@2165: // {line-number, start-pc}, jlahoda@2165: {9, 0}, //number -> number / 1 jlahoda@2165: }; jlahoda@2165: jlahoda@2165: static final int[][] lambdaWithVarsExpectedLNT = { jlahoda@2165: // {line-number, start-pc}, jlahoda@2165: {13, 0}, //number -> number / 1 jlahoda@2165: {19, 2}, //number -> number / 1 jlahoda@2165: }; jlahoda@2165: jlahoda@2165: static final int[][] insideLambdaWithVarsExpectedLNT = { jlahoda@2165: // {line-number, start-pc}, jlahoda@2165: {16, 0}, //number -> number / 1 jlahoda@2165: {17, 2}, //number -> number / 1 jlahoda@2165: }; jlahoda@2165: jlahoda@2165: static final int[][] lambdaVoid2VoidExpectedLNT = { jlahoda@2165: // {line-number, start-pc}, jlahoda@2165: {20, 0}, //number -> number / 1 jlahoda@2165: }; jlahoda@2165: jlahoda@2165: static final int[][] deserializeExpectedLNT = { jlahoda@2165: // {line-number, start-pc}, jlahoda@2165: {05, 0}, //number -> number / 1 jlahoda@2165: }; jlahoda@2165: jlahoda@2165: static final int[][] lambdaBridgeExpectedLNT = { jlahoda@2165: // {line-number, start-pc}, jlahoda@2165: {22, 0}, //number -> number / 1 jlahoda@2165: }; jlahoda@2165: jlahoda@2165: public static void main(String[] args) throws Exception { jlahoda@2165: new WrongLNTForLambdaTest().run(); jlahoda@2165: } jlahoda@2165: jlahoda@2165: void run() throws Exception { jlahoda@2165: compileTestClass(); jlahoda@2165: checkClassFile(new File(Paths.get(System.getProperty("user.dir"), jlahoda@2165: "Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT); jlahoda@2165: checkClassFile(new File(Paths.get(System.getProperty("user.dir"), jlahoda@2165: "Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT); jlahoda@2165: checkClassFile(new File(Paths.get(System.getProperty("user.dir"), jlahoda@2165: "Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT); jlahoda@2165: checkClassFile(new File(Paths.get(System.getProperty("user.dir"), jlahoda@2165: "Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT); jlahoda@2165: checkClassFile(new File(Paths.get(System.getProperty("user.dir"), jlahoda@2165: "Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT); jlahoda@2165: checkClassFile(new File(Paths.get(System.getProperty("user.dir"), jlahoda@2165: "Foo.class").toUri()), "lambda$MR$variablesInLambdas$notify$8bc4f5bd$1", lambdaBridgeExpectedLNT); jlahoda@2165: } jlahoda@2165: jlahoda@2165: void compileTestClass() throws Exception { jlahoda@2165: ToolBox.JavaToolArgs javacSuccessArgs = jlahoda@2165: new ToolBox.JavaToolArgs().setSources(testSource); jlahoda@2165: ToolBox.javac(javacSuccessArgs); jlahoda@2165: } jlahoda@2165: jlahoda@2165: void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception { jlahoda@2165: ClassFile classFile = ClassFile.read(cfile); jlahoda@2165: boolean methodFound = false; jlahoda@2165: for (Method method : classFile.methods) { jlahoda@2165: if (method.getName(classFile.constant_pool).equals(methodToFind)) { jlahoda@2165: methodFound = true; jlahoda@2165: Code_attribute code = (Code_attribute) method.attributes.get("Code"); jlahoda@2165: LineNumberTable_attribute lnt = jlahoda@2165: (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); jlahoda@2165: Assert.check(lnt.line_number_table_length == expectedLNT.length, jlahoda@2165: "The LineNumberTable found has a length different to the expected one"); jlahoda@2165: int i = 0; jlahoda@2165: for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { jlahoda@2165: Assert.check(entry.line_number == expectedLNT[i][0] && jlahoda@2165: entry.start_pc == expectedLNT[i][1], jlahoda@2165: "LNT entry at pos " + i + " differ from expected." + jlahoda@2165: "Found " + entry.line_number + ":" + entry.start_pc + jlahoda@2165: ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); jlahoda@2165: i++; jlahoda@2165: } jlahoda@2165: } jlahoda@2165: } jlahoda@2165: Assert.check(methodFound, "The seek method was not found"); jlahoda@2165: } jlahoda@2165: jlahoda@2165: void error(String msg) { jlahoda@2165: throw new AssertionError(msg); jlahoda@2165: } jlahoda@2165: jlahoda@2165: }