jdk

Форк
0
/
WrongLNTForLambdaTest.java 
188 строк · 7.9 Кб
1
/*
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.
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.
8
 *
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).
14
 *
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.
18
 *
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
21
 * questions.
22
 */
23

24
/*
25
 * @test
26
 * @bug 8019486 8026861 8027142
27
 * @summary javac, generates erroneous LVT for a test case with lambda code
28
 * @library /tools/lib
29
 * @enablePreview
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
36
 */
37

38
import java.io.File;
39
import java.nio.file.Paths;
40

41
import com.sun.tools.javac.util.Assert;
42

43
import java.lang.classfile.*;
44
import java.lang.classfile.attribute.*;
45
import toolbox.JavacTask;
46
import toolbox.ToolBox;
47

48
public class WrongLNTForLambdaTest {
49

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" +
54
    /* 04 */        "\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" +
60
    /* 10 */        "    }\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" +
67
    /* 17 */        "                }\n" +
68
    /* 18 */        "            }\n" +
69
    /* 19 */        "        };\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" +
73
    /* 23 */        "    }\n" +
74
    /* 24 */        "    private void foo() {}\n" +
75
    /* 25 */        "    void assignLambda() {\n" +
76
    /* 26 */        "        Runnable r = () -> { };\n" +
77
    /* 27 */        "    }\n" +
78
    /* 28 */        "    void callLambda(int i, Runnable r) {\n" +
79
    /* 29 */        "        callLambda(0,\n" +
80
    /* 30 */        "                   () -> { });\n" +
81
    /* 31 */        "    }\n" +
82
    /* 32 */        "}";
83

84
    static final int[][] simpleLambdaExpectedLNT = {
85
    //  {line-number, start-pc},
86
        {9,           0},       //number -> number / 1
87
    };
88

89
    static final int[][] lambdaWithVarsExpectedLNT = {
90
    //  {line-number, start-pc},
91
        {13,           0},       //number -> number / 1
92
        {19,           2},       //number -> number / 1
93
    };
94

95
    static final int[][] insideLambdaWithVarsExpectedLNT = {
96
    //  {line-number, start-pc},
97
        {16,           0},       //number -> number / 1
98
        {17,           2},       //number -> number / 1
99
    };
100

101
    static final int[][] lambdaVoid2VoidExpectedLNT = {
102
    //  {line-number, start-pc},
103
        {20,           0},       //number -> number / 1
104
    };
105

106
    static final int[][] deserializeExpectedLNT = {
107
    //  {line-number, start-pc},
108
        {05,           0},       //number -> number / 1
109
    };
110

111
    static final int[][] lambdaBridgeExpectedLNT = {
112
    //  {line-number, start-pc},
113
        {22,           0},       //number -> number / 1
114
    };
115

116
    static final int[][] assignmentExpectedLNT = {
117
    //  {line-number, start-pc},
118
        {26,           0},       //number -> number / 1
119
        {27,           6},       //number -> number / 1
120
    };
121

122
    static final int[][] callExpectedLNT = {
123
    //  {line-number, start-pc},
124
        {29,           0},       //number -> number / 1
125
        {31,           10},       //number -> number / 1
126
    };
127

128
    public static void main(String[] args) throws Exception {
129
        new WrongLNTForLambdaTest().run();
130
    }
131

132
    ToolBox tb = new ToolBox();
133

134
    void run() throws Exception {
135
        compileTestClass();
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);
152
    }
153

154
    void compileTestClass() throws Exception {
155
        new JavacTask(tb)
156
                .sources(testSource)
157
                .run();
158
    }
159

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)) {
165
                methodFound = true;
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");
170
                int i = 0;
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]);
177
                    i++;
178
                }
179
            }
180
        }
181
        Assert.check(methodFound, "The seek method was not found");
182
    }
183

184
    void error(String msg) {
185
        throw new AssertionError(msg);
186
    }
187

188
}
189

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.