test/tools/javac/T8019486/WrongLNTForLambdaTest.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2186
6e0f31d61e56
parent 0
959103a6100f
child 2702
9ca8d8713094
permissions
-rw-r--r--

merge

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

mercurial