test/tools/javac/T8019486/WrongLVTForLambdaTest.java

changeset 2165
864dafc5ab7a
equal deleted inserted replaced
2164:abc3eaccba73 2165:864dafc5ab7a
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 */
25
26 /*
27 * @test
28 * @bug 8019486
29 * @summary javac, generates erroneous LVT for a test case with lambda code
30 * @library /tools/javac/lib
31 * @build ToolBox
32 * @run main WrongLVTForLambdaTest
33 */
34
35 import java.io.File;
36 import java.nio.file.Paths;
37
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;
43
44 public class WrongLVTForLambdaTest {
45
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 */ "}";
58
59 static final int[][] expectedLNT = {
60 // {line-number, start-pc},
61 {9, 0}, //number -> number / 1
62 };
63
64 public static void main(String[] args) throws Exception {
65 new WrongLVTForLambdaTest().run();
66 }
67
68 void run() throws Exception {
69 compileTestClass();
70 checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
71 "Foo.class").toUri()));
72 }
73
74 void compileTestClass() throws Exception {
75 ToolBox.JavaToolArgs javacSuccessArgs =
76 new ToolBox.JavaToolArgs().setSources(testSource);
77 ToolBox.javac(javacSuccessArgs);
78 }
79
80 void checkClassFile(final File cfile) throws Exception {
81 ClassFile classFile = ClassFile.read(cfile);
82 int methodsFound = 0;
83 for (Method method : classFile.methods) {
84 if (method.getName(classFile.constant_pool).startsWith("lambda$")) {
85 ++methodsFound;
86 Code_attribute code = (Code_attribute) method.attributes.get("Code");
87 LineNumberTable_attribute lnt =
88 (LineNumberTable_attribute) code.attributes.get("LineNumberTable");
89 Assert.check(lnt.line_number_table_length == expectedLNT.length,
90 "The LineNumberTable found has a length different to the expected one");
91 int i = 0;
92 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
93 Assert.check(entry.line_number == expectedLNT[i][0] &&
94 entry.start_pc == expectedLNT[i][1],
95 "LNT entry at pos " + i + " differ from expected." +
96 "Found " + entry.line_number + ":" + entry.start_pc +
97 ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
98 i++;
99 }
100 }
101 }
102 Assert.check(methodsFound == 1, "Expected to find one lambda method, found " + methodsFound);
103 }
104
105 void error(String msg) {
106 throw new AssertionError(msg);
107 }
108
109 }

mercurial