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