test/tools/javac/T8019486/WrongLVTForLambdaTest.java

Wed, 23 Oct 2013 23:02:17 +0200

author
jlahoda
date
Wed, 23 Oct 2013 23:02:17 +0200
changeset 2164
abc3eaccba73
permissions
-rw-r--r--

8027191: Fix for JDK-8026861 refers to an incorrect bug number
Summary: Reverting changeset b05db8c815e8, so that it can be applied again with a correct bug number
Reviewed-by: jjg

jlahoda@2164 1 /*
jlahoda@2164 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
jlahoda@2164 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlahoda@2164 4 *
jlahoda@2164 5 * This code is free software; you can redistribute it and/or modify it
jlahoda@2164 6 * under the terms of the GNU General Public License version 2 only, as
jlahoda@2164 7 * published by the Free Software Foundation. Oracle designates this
jlahoda@2164 8 * particular file as subject to the "Classpath" exception as provided
jlahoda@2164 9 * by Oracle in the LICENSE file that accompanied this code.
jlahoda@2164 10 *
jlahoda@2164 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlahoda@2164 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlahoda@2164 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlahoda@2164 14 * version 2 for more details (a copy is included in the LICENSE file that
jlahoda@2164 15 * accompanied this code).
jlahoda@2164 16 *
jlahoda@2164 17 * You should have received a copy of the GNU General Public License version
jlahoda@2164 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlahoda@2164 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlahoda@2164 20 *
jlahoda@2164 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlahoda@2164 22 * or visit www.oracle.com if you need additional information or have any
jlahoda@2164 23 * questions.
jlahoda@2164 24 */
jlahoda@2164 25
jlahoda@2164 26 /*
jlahoda@2164 27 * @test
jlahoda@2164 28 * @bug 8019486
jlahoda@2164 29 * @summary javac, generates erroneous LVT for a test case with lambda code
jlahoda@2164 30 * @library /tools/javac/lib
jlahoda@2164 31 * @build ToolBox
jlahoda@2164 32 * @run main WrongLVTForLambdaTest
jlahoda@2164 33 */
jlahoda@2164 34
jlahoda@2164 35 import java.io.File;
jlahoda@2164 36 import java.nio.file.Paths;
jlahoda@2164 37
jlahoda@2164 38 import com.sun.tools.classfile.ClassFile;
jlahoda@2164 39 import com.sun.tools.classfile.Code_attribute;
jlahoda@2164 40 import com.sun.tools.classfile.LineNumberTable_attribute;
jlahoda@2164 41 import com.sun.tools.classfile.Method;
jlahoda@2164 42 import com.sun.tools.javac.util.Assert;
jlahoda@2164 43
jlahoda@2164 44 public class WrongLVTForLambdaTest {
jlahoda@2164 45
jlahoda@2164 46 static final String testSource =
jlahoda@2164 47 /* 01 */ "import java.util.List;\n" +
jlahoda@2164 48 /* 02 */ "import java.util.Arrays;\n" +
jlahoda@2164 49 /* 03 */ "import java.util.stream.Collectors;\n" +
jlahoda@2164 50 /* 04 */ "\n" +
jlahoda@2164 51 /* 05 */ "public class Foo {\n" +
jlahoda@2164 52 /* 06 */ " void bar(int value) {\n" +
jlahoda@2164 53 /* 07 */ " final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" +
jlahoda@2164 54 /* 08 */ " final List<Integer> numbersPlusOne = \n" +
jlahoda@2164 55 /* 09 */ " numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" +
jlahoda@2164 56 /* 10 */ " }\n" +
jlahoda@2164 57 /* 11 */ "}";
jlahoda@2164 58
jlahoda@2164 59 static final int[][] expectedLNT = {
jlahoda@2164 60 // {line-number, start-pc},
jlahoda@2164 61 {9, 0}, //number -> number / 1
jlahoda@2164 62 };
jlahoda@2164 63
jlahoda@2164 64 public static void main(String[] args) throws Exception {
jlahoda@2164 65 new WrongLVTForLambdaTest().run();
jlahoda@2164 66 }
jlahoda@2164 67
jlahoda@2164 68 void run() throws Exception {
jlahoda@2164 69 compileTestClass();
jlahoda@2164 70 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
jlahoda@2164 71 "Foo.class").toUri()));
jlahoda@2164 72 }
jlahoda@2164 73
jlahoda@2164 74 void compileTestClass() throws Exception {
jlahoda@2164 75 ToolBox.JavaToolArgs javacSuccessArgs =
jlahoda@2164 76 new ToolBox.JavaToolArgs().setSources(testSource);
jlahoda@2164 77 ToolBox.javac(javacSuccessArgs);
jlahoda@2164 78 }
jlahoda@2164 79
jlahoda@2164 80 void checkClassFile(final File cfile) throws Exception {
jlahoda@2164 81 ClassFile classFile = ClassFile.read(cfile);
jlahoda@2164 82 int methodsFound = 0;
jlahoda@2164 83 for (Method method : classFile.methods) {
jlahoda@2164 84 if (method.getName(classFile.constant_pool).startsWith("lambda$")) {
jlahoda@2164 85 ++methodsFound;
jlahoda@2164 86 Code_attribute code = (Code_attribute) method.attributes.get("Code");
jlahoda@2164 87 LineNumberTable_attribute lnt =
jlahoda@2164 88 (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
jlahoda@2164 89 Assert.check(lnt.line_number_table_length == expectedLNT.length,
jlahoda@2164 90 "The LineNumberTable found has a length different to the expected one");
jlahoda@2164 91 int i = 0;
jlahoda@2164 92 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
jlahoda@2164 93 Assert.check(entry.line_number == expectedLNT[i][0] &&
jlahoda@2164 94 entry.start_pc == expectedLNT[i][1],
jlahoda@2164 95 "LNT entry at pos " + i + " differ from expected." +
jlahoda@2164 96 "Found " + entry.line_number + ":" + entry.start_pc +
jlahoda@2164 97 ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
jlahoda@2164 98 i++;
jlahoda@2164 99 }
jlahoda@2164 100 }
jlahoda@2164 101 }
jlahoda@2164 102 Assert.check(methodsFound == 1, "Expected to find one lambda method, found " + methodsFound);
jlahoda@2164 103 }
jlahoda@2164 104
jlahoda@2164 105 void error(String msg) {
jlahoda@2164 106 throw new AssertionError(msg);
jlahoda@2164 107 }
jlahoda@2164 108
jlahoda@2164 109 }

mercurial