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