Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
jlahoda@2165 | 1 | /* |
jlahoda@2165 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
jlahoda@2165 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jlahoda@2165 | 4 | * |
jlahoda@2165 | 5 | * This code is free software; you can redistribute it and/or modify it |
jlahoda@2165 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jlahoda@2165 | 7 | * published by the Free Software Foundation. Oracle designates this |
jlahoda@2165 | 8 | * particular file as subject to the "Classpath" exception as provided |
jlahoda@2165 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jlahoda@2165 | 10 | * |
jlahoda@2165 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jlahoda@2165 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jlahoda@2165 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jlahoda@2165 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jlahoda@2165 | 15 | * accompanied this code). |
jlahoda@2165 | 16 | * |
jlahoda@2165 | 17 | * You should have received a copy of the GNU General Public License version |
jlahoda@2165 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jlahoda@2165 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jlahoda@2165 | 20 | * |
jlahoda@2165 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jlahoda@2165 | 22 | * or visit www.oracle.com if you need additional information or have any |
jlahoda@2165 | 23 | * questions. |
jlahoda@2165 | 24 | */ |
jlahoda@2165 | 25 | |
jlahoda@2165 | 26 | /* |
jlahoda@2165 | 27 | * @test |
jlahoda@2186 | 28 | * @bug 8019486 8026861 8027142 |
jlahoda@2165 | 29 | * @summary javac, generates erroneous LVT for a test case with lambda code |
jlahoda@2165 | 30 | * @library /tools/javac/lib |
jlahoda@2165 | 31 | * @build ToolBox |
jlahoda@2165 | 32 | * @run main WrongLNTForLambdaTest |
jlahoda@2165 | 33 | */ |
jlahoda@2165 | 34 | |
jlahoda@2165 | 35 | import java.io.File; |
jlahoda@2165 | 36 | import java.nio.file.Paths; |
jlahoda@2165 | 37 | |
jlahoda@2165 | 38 | import com.sun.tools.classfile.ClassFile; |
jlahoda@2165 | 39 | import com.sun.tools.classfile.Code_attribute; |
jlahoda@2165 | 40 | import com.sun.tools.classfile.LineNumberTable_attribute; |
jlahoda@2165 | 41 | import com.sun.tools.classfile.Method; |
jlahoda@2165 | 42 | import com.sun.tools.javac.util.Assert; |
jlahoda@2165 | 43 | |
jlahoda@2165 | 44 | public class WrongLNTForLambdaTest { |
jlahoda@2165 | 45 | |
jlahoda@2165 | 46 | static final String testSource = |
jlahoda@2165 | 47 | /* 01 */ "import java.util.List;\n" + |
jlahoda@2165 | 48 | /* 02 */ "import java.util.Arrays;\n" + |
jlahoda@2165 | 49 | /* 03 */ "import java.util.stream.Collectors;\n" + |
jlahoda@2165 | 50 | /* 04 */ "\n" + |
jlahoda@2165 | 51 | /* 05 */ "public class Foo {\n" + |
jlahoda@2165 | 52 | /* 06 */ " void bar(int value) {\n" + |
jlahoda@2165 | 53 | /* 07 */ " final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" + |
jlahoda@2165 | 54 | /* 08 */ " final List<Integer> numbersPlusOne = \n" + |
jlahoda@2165 | 55 | /* 09 */ " numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" + |
jlahoda@2165 | 56 | /* 10 */ " }\n" + |
jlahoda@2165 | 57 | /* 11 */ " void variablesInLambdas(int value) {\n" + |
jlahoda@2165 | 58 | /* 12 */ " Runnable r1 = () -> {\n" + |
jlahoda@2165 | 59 | /* 13 */ " int i = value;\n" + |
jlahoda@2165 | 60 | /* 14 */ " class FooBar<T extends CharSequence> {\n" + |
jlahoda@2165 | 61 | /* 15 */ " public void run() {\n" + |
jlahoda@2165 | 62 | /* 16 */ " T t = null;\n" + |
jlahoda@2165 | 63 | /* 17 */ " }\n" + |
jlahoda@2165 | 64 | /* 18 */ " }\n" + |
jlahoda@2165 | 65 | /* 19 */ " };\n" + |
jlahoda@2165 | 66 | /* 20 */ " Runnable r2 = () -> System.err.println(1);\n" + |
jlahoda@2165 | 67 | /* 21 */ " Runnable r3 = (Runnable & java.io.Serializable) this::foo;\n" + |
jlahoda@2165 | 68 | /* 22 */ " Runnable r4 = super :: notify;\n" + |
jlahoda@2165 | 69 | /* 23 */ " }\n" + |
jlahoda@2165 | 70 | /* 24 */ " private void foo() {}\n" + |
jlahoda@2186 | 71 | /* 25 */ " void assignLambda() {\n" + |
jlahoda@2186 | 72 | /* 26 */ " Runnable r = () -> { };\n" + |
jlahoda@2186 | 73 | /* 27 */ " }\n" + |
jlahoda@2186 | 74 | /* 28 */ " void callLambda(int i, Runnable r) {\n" + |
jlahoda@2186 | 75 | /* 29 */ " callLambda(0,\n" + |
jlahoda@2186 | 76 | /* 30 */ " () -> { });\n" + |
jlahoda@2186 | 77 | /* 31 */ " }\n" + |
jlahoda@2186 | 78 | /* 32 */ "}"; |
jlahoda@2165 | 79 | |
jlahoda@2165 | 80 | static final int[][] simpleLambdaExpectedLNT = { |
jlahoda@2165 | 81 | // {line-number, start-pc}, |
jlahoda@2165 | 82 | {9, 0}, //number -> number / 1 |
jlahoda@2165 | 83 | }; |
jlahoda@2165 | 84 | |
jlahoda@2165 | 85 | static final int[][] lambdaWithVarsExpectedLNT = { |
jlahoda@2165 | 86 | // {line-number, start-pc}, |
jlahoda@2165 | 87 | {13, 0}, //number -> number / 1 |
jlahoda@2165 | 88 | {19, 2}, //number -> number / 1 |
jlahoda@2165 | 89 | }; |
jlahoda@2165 | 90 | |
jlahoda@2165 | 91 | static final int[][] insideLambdaWithVarsExpectedLNT = { |
jlahoda@2165 | 92 | // {line-number, start-pc}, |
jlahoda@2165 | 93 | {16, 0}, //number -> number / 1 |
jlahoda@2165 | 94 | {17, 2}, //number -> number / 1 |
jlahoda@2165 | 95 | }; |
jlahoda@2165 | 96 | |
jlahoda@2165 | 97 | static final int[][] lambdaVoid2VoidExpectedLNT = { |
jlahoda@2165 | 98 | // {line-number, start-pc}, |
jlahoda@2165 | 99 | {20, 0}, //number -> number / 1 |
jlahoda@2165 | 100 | }; |
jlahoda@2165 | 101 | |
jlahoda@2165 | 102 | static final int[][] deserializeExpectedLNT = { |
jlahoda@2165 | 103 | // {line-number, start-pc}, |
jlahoda@2165 | 104 | {05, 0}, //number -> number / 1 |
jlahoda@2165 | 105 | }; |
jlahoda@2165 | 106 | |
jlahoda@2165 | 107 | static final int[][] lambdaBridgeExpectedLNT = { |
jlahoda@2165 | 108 | // {line-number, start-pc}, |
jlahoda@2165 | 109 | {22, 0}, //number -> number / 1 |
jlahoda@2165 | 110 | }; |
jlahoda@2165 | 111 | |
jlahoda@2186 | 112 | static final int[][] assignmentExpectedLNT = { |
jlahoda@2186 | 113 | // {line-number, start-pc}, |
jlahoda@2186 | 114 | {26, 0}, //number -> number / 1 |
jlahoda@2186 | 115 | {27, 6}, //number -> number / 1 |
jlahoda@2186 | 116 | }; |
jlahoda@2186 | 117 | |
jlahoda@2186 | 118 | static final int[][] callExpectedLNT = { |
jlahoda@2186 | 119 | // {line-number, start-pc}, |
jlahoda@2186 | 120 | {29, 0}, //number -> number / 1 |
jlahoda@2186 | 121 | {31, 10}, //number -> number / 1 |
jlahoda@2186 | 122 | }; |
jlahoda@2186 | 123 | |
jlahoda@2165 | 124 | public static void main(String[] args) throws Exception { |
jlahoda@2165 | 125 | new WrongLNTForLambdaTest().run(); |
jlahoda@2165 | 126 | } |
jlahoda@2165 | 127 | |
jlahoda@2165 | 128 | void run() throws Exception { |
jlahoda@2165 | 129 | compileTestClass(); |
jlahoda@2165 | 130 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
jlahoda@2165 | 131 | "Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT); |
jlahoda@2165 | 132 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
jlahoda@2165 | 133 | "Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT); |
jlahoda@2165 | 134 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
jlahoda@2165 | 135 | "Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT); |
jlahoda@2165 | 136 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
jlahoda@2165 | 137 | "Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT); |
jlahoda@2165 | 138 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
jlahoda@2165 | 139 | "Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT); |
jlahoda@2165 | 140 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
rfield@2607 | 141 | "Foo.class").toUri()), "lambda$variablesInLambdas$3", lambdaBridgeExpectedLNT); |
jlahoda@2186 | 142 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
jlahoda@2186 | 143 | "Foo.class").toUri()), "assignLambda", assignmentExpectedLNT); |
jlahoda@2186 | 144 | checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
jlahoda@2186 | 145 | "Foo.class").toUri()), "callLambda", callExpectedLNT); |
jlahoda@2165 | 146 | } |
jlahoda@2165 | 147 | |
jlahoda@2165 | 148 | void compileTestClass() throws Exception { |
jlahoda@2165 | 149 | ToolBox.JavaToolArgs javacSuccessArgs = |
jlahoda@2165 | 150 | new ToolBox.JavaToolArgs().setSources(testSource); |
jlahoda@2165 | 151 | ToolBox.javac(javacSuccessArgs); |
jlahoda@2165 | 152 | } |
jlahoda@2165 | 153 | |
jlahoda@2165 | 154 | void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception { |
jlahoda@2165 | 155 | ClassFile classFile = ClassFile.read(cfile); |
jlahoda@2165 | 156 | boolean methodFound = false; |
jlahoda@2165 | 157 | for (Method method : classFile.methods) { |
jlahoda@2165 | 158 | if (method.getName(classFile.constant_pool).equals(methodToFind)) { |
jlahoda@2165 | 159 | methodFound = true; |
jlahoda@2165 | 160 | Code_attribute code = (Code_attribute) method.attributes.get("Code"); |
jlahoda@2165 | 161 | LineNumberTable_attribute lnt = |
jlahoda@2165 | 162 | (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); |
jlahoda@2165 | 163 | Assert.check(lnt.line_number_table_length == expectedLNT.length, |
jlahoda@2165 | 164 | "The LineNumberTable found has a length different to the expected one"); |
jlahoda@2165 | 165 | int i = 0; |
jlahoda@2165 | 166 | for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { |
jlahoda@2165 | 167 | Assert.check(entry.line_number == expectedLNT[i][0] && |
jlahoda@2165 | 168 | entry.start_pc == expectedLNT[i][1], |
jlahoda@2165 | 169 | "LNT entry at pos " + i + " differ from expected." + |
jlahoda@2165 | 170 | "Found " + entry.line_number + ":" + entry.start_pc + |
jlahoda@2165 | 171 | ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); |
jlahoda@2165 | 172 | i++; |
jlahoda@2165 | 173 | } |
jlahoda@2165 | 174 | } |
jlahoda@2165 | 175 | } |
jlahoda@2165 | 176 | Assert.check(methodFound, "The seek method was not found"); |
jlahoda@2165 | 177 | } |
jlahoda@2165 | 178 | |
jlahoda@2165 | 179 | void error(String msg) { |
jlahoda@2165 | 180 | throw new AssertionError(msg); |
jlahoda@2165 | 181 | } |
jlahoda@2165 | 182 | |
jlahoda@2165 | 183 | } |