2
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
26
* @bug 8019486 8026861 8027142
27
* @summary javac, generates erroneous LVT for a test case with lambda code
30
* @modules jdk.compiler/com.sun.tools.javac.api
31
* jdk.compiler/com.sun.tools.javac.main
32
* jdk.compiler/com.sun.tools.javac.util
33
* java.base/jdk.internal.classfile.impl
34
* @build toolbox.ToolBox toolbox.JavacTask
35
* @run main WrongLNTForLambdaTest
39
import java.nio.file.Paths;
41
import com.sun.tools.javac.util.Assert;
43
import java.lang.classfile.*;
44
import java.lang.classfile.attribute.*;
45
import toolbox.JavacTask;
46
import toolbox.ToolBox;
48
public class WrongLNTForLambdaTest {
50
static final String testSource =
51
/* 01 */ "import java.util.List;\n" +
52
/* 02 */ "import java.util.Arrays;\n" +
53
/* 03 */ "import java.util.stream.Collectors;\n" +
55
/* 05 */ "public class Foo {\n" +
56
/* 06 */ " void bar(int value) {\n" +
57
/* 07 */ " final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" +
58
/* 08 */ " final List<Integer> numbersPlusOne = \n" +
59
/* 09 */ " numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" +
61
/* 11 */ " void variablesInLambdas(int value) {\n" +
62
/* 12 */ " Runnable r1 = () -> {\n" +
63
/* 13 */ " int i = value;\n" +
64
/* 14 */ " class FooBar<T extends CharSequence> {\n" +
65
/* 15 */ " public void run() {\n" +
66
/* 16 */ " T t = null;\n" +
70
/* 20 */ " Runnable r2 = () -> System.err.println(1);\n" +
71
/* 21 */ " Runnable r3 = (Runnable & java.io.Serializable) this::foo;\n" +
72
/* 22 */ " Runnable r4 = super :: notify;\n" +
74
/* 24 */ " private void foo() {}\n" +
75
/* 25 */ " void assignLambda() {\n" +
76
/* 26 */ " Runnable r = () -> { };\n" +
78
/* 28 */ " void callLambda(int i, Runnable r) {\n" +
79
/* 29 */ " callLambda(0,\n" +
80
/* 30 */ " () -> { });\n" +
84
static final int[][] simpleLambdaExpectedLNT = {
85
// {line-number, start-pc},
86
{9, 0}, //number -> number / 1
89
static final int[][] lambdaWithVarsExpectedLNT = {
90
// {line-number, start-pc},
91
{13, 0}, //number -> number / 1
92
{19, 2}, //number -> number / 1
95
static final int[][] insideLambdaWithVarsExpectedLNT = {
96
// {line-number, start-pc},
97
{16, 0}, //number -> number / 1
98
{17, 2}, //number -> number / 1
101
static final int[][] lambdaVoid2VoidExpectedLNT = {
102
// {line-number, start-pc},
103
{20, 0}, //number -> number / 1
106
static final int[][] deserializeExpectedLNT = {
107
// {line-number, start-pc},
108
{05, 0}, //number -> number / 1
111
static final int[][] lambdaBridgeExpectedLNT = {
112
// {line-number, start-pc},
113
{22, 0}, //number -> number / 1
116
static final int[][] assignmentExpectedLNT = {
117
// {line-number, start-pc},
118
{26, 0}, //number -> number / 1
119
{27, 6}, //number -> number / 1
122
static final int[][] callExpectedLNT = {
123
// {line-number, start-pc},
124
{29, 0}, //number -> number / 1
125
{31, 10}, //number -> number / 1
128
public static void main(String[] args) throws Exception {
129
new WrongLNTForLambdaTest().run();
132
ToolBox tb = new ToolBox();
134
void run() throws Exception {
136
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
137
"Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT);
138
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
139
"Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT);
140
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
141
"Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT);
142
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
143
"Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT);
144
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
145
"Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT);
146
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
147
"Foo.class").toUri()), "lambda$variablesInLambdas$3", lambdaBridgeExpectedLNT);
148
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
149
"Foo.class").toUri()), "assignLambda", assignmentExpectedLNT);
150
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
151
"Foo.class").toUri()), "callLambda", callExpectedLNT);
154
void compileTestClass() throws Exception {
160
void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception {
161
ClassModel classFile = ClassFile.of().parse(cfile.toPath());
162
boolean methodFound = false;
163
for (MethodModel method : classFile.methods()) {
164
if (method.methodName().equalsString(methodToFind)) {
166
CodeAttribute code = method.findAttribute(Attributes.code()).orElseThrow();
167
LineNumberTableAttribute lnt = code.findAttribute(Attributes.lineNumberTable()).orElseThrow();
168
Assert.check(lnt.lineNumbers().size() == expectedLNT.length,
169
"The LineNumberTable found has a length different to the expected one");
171
for (LineNumberInfo entry: lnt.lineNumbers()) {
172
Assert.check(entry.lineNumber() == expectedLNT[i][0] &&
173
entry.startPc() == expectedLNT[i][1],
174
"LNT entry at pos " + i + " differ from expected." +
175
"Found " + entry.lineNumber() + ":" + entry.startPc() +
176
". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
181
Assert.check(methodFound, "The seek method was not found");
184
void error(String msg) {
185
throw new AssertionError(msg);