1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/T8019486/WrongLNTForLambdaTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,183 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * @test 1.31 + * @bug 8019486 8026861 8027142 1.32 + * @summary javac, generates erroneous LVT for a test case with lambda code 1.33 + * @library /tools/javac/lib 1.34 + * @build ToolBox 1.35 + * @run main WrongLNTForLambdaTest 1.36 + */ 1.37 + 1.38 +import java.io.File; 1.39 +import java.nio.file.Paths; 1.40 + 1.41 +import com.sun.tools.classfile.ClassFile; 1.42 +import com.sun.tools.classfile.Code_attribute; 1.43 +import com.sun.tools.classfile.LineNumberTable_attribute; 1.44 +import com.sun.tools.classfile.Method; 1.45 +import com.sun.tools.javac.util.Assert; 1.46 + 1.47 +public class WrongLNTForLambdaTest { 1.48 + 1.49 + static final String testSource = 1.50 + /* 01 */ "import java.util.List;\n" + 1.51 + /* 02 */ "import java.util.Arrays;\n" + 1.52 + /* 03 */ "import java.util.stream.Collectors;\n" + 1.53 + /* 04 */ "\n" + 1.54 + /* 05 */ "public class Foo {\n" + 1.55 + /* 06 */ " void bar(int value) {\n" + 1.56 + /* 07 */ " final List<Integer> numbers = Arrays.asList(1, 2, 3);\n" + 1.57 + /* 08 */ " final List<Integer> numbersPlusOne = \n" + 1.58 + /* 09 */ " numbers.stream().map(number -> number / 1).collect(Collectors.toList());\n" + 1.59 + /* 10 */ " }\n" + 1.60 + /* 11 */ " void variablesInLambdas(int value) {\n" + 1.61 + /* 12 */ " Runnable r1 = () -> {\n" + 1.62 + /* 13 */ " int i = value;\n" + 1.63 + /* 14 */ " class FooBar<T extends CharSequence> {\n" + 1.64 + /* 15 */ " public void run() {\n" + 1.65 + /* 16 */ " T t = null;\n" + 1.66 + /* 17 */ " }\n" + 1.67 + /* 18 */ " }\n" + 1.68 + /* 19 */ " };\n" + 1.69 + /* 20 */ " Runnable r2 = () -> System.err.println(1);\n" + 1.70 + /* 21 */ " Runnable r3 = (Runnable & java.io.Serializable) this::foo;\n" + 1.71 + /* 22 */ " Runnable r4 = super :: notify;\n" + 1.72 + /* 23 */ " }\n" + 1.73 + /* 24 */ " private void foo() {}\n" + 1.74 + /* 25 */ " void assignLambda() {\n" + 1.75 + /* 26 */ " Runnable r = () -> { };\n" + 1.76 + /* 27 */ " }\n" + 1.77 + /* 28 */ " void callLambda(int i, Runnable r) {\n" + 1.78 + /* 29 */ " callLambda(0,\n" + 1.79 + /* 30 */ " () -> { });\n" + 1.80 + /* 31 */ " }\n" + 1.81 + /* 32 */ "}"; 1.82 + 1.83 + static final int[][] simpleLambdaExpectedLNT = { 1.84 + // {line-number, start-pc}, 1.85 + {9, 0}, //number -> number / 1 1.86 + }; 1.87 + 1.88 + static final int[][] lambdaWithVarsExpectedLNT = { 1.89 + // {line-number, start-pc}, 1.90 + {13, 0}, //number -> number / 1 1.91 + {19, 2}, //number -> number / 1 1.92 + }; 1.93 + 1.94 + static final int[][] insideLambdaWithVarsExpectedLNT = { 1.95 + // {line-number, start-pc}, 1.96 + {16, 0}, //number -> number / 1 1.97 + {17, 2}, //number -> number / 1 1.98 + }; 1.99 + 1.100 + static final int[][] lambdaVoid2VoidExpectedLNT = { 1.101 + // {line-number, start-pc}, 1.102 + {20, 0}, //number -> number / 1 1.103 + }; 1.104 + 1.105 + static final int[][] deserializeExpectedLNT = { 1.106 + // {line-number, start-pc}, 1.107 + {05, 0}, //number -> number / 1 1.108 + }; 1.109 + 1.110 + static final int[][] lambdaBridgeExpectedLNT = { 1.111 + // {line-number, start-pc}, 1.112 + {22, 0}, //number -> number / 1 1.113 + }; 1.114 + 1.115 + static final int[][] assignmentExpectedLNT = { 1.116 + // {line-number, start-pc}, 1.117 + {26, 0}, //number -> number / 1 1.118 + {27, 6}, //number -> number / 1 1.119 + }; 1.120 + 1.121 + static final int[][] callExpectedLNT = { 1.122 + // {line-number, start-pc}, 1.123 + {29, 0}, //number -> number / 1 1.124 + {31, 10}, //number -> number / 1 1.125 + }; 1.126 + 1.127 + public static void main(String[] args) throws Exception { 1.128 + new WrongLNTForLambdaTest().run(); 1.129 + } 1.130 + 1.131 + void run() throws Exception { 1.132 + compileTestClass(); 1.133 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.134 + "Foo.class").toUri()), "lambda$bar$0", simpleLambdaExpectedLNT); 1.135 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.136 + "Foo.class").toUri()), "lambda$variablesInLambdas$1", lambdaWithVarsExpectedLNT); 1.137 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.138 + "Foo$1FooBar.class").toUri()), "run", insideLambdaWithVarsExpectedLNT); 1.139 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.140 + "Foo.class").toUri()), "lambda$variablesInLambdas$2", lambdaVoid2VoidExpectedLNT); 1.141 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.142 + "Foo.class").toUri()), "$deserializeLambda$", deserializeExpectedLNT); 1.143 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.144 + "Foo.class").toUri()), "lambda$MR$variablesInLambdas$notify$8bc4f5bd$1", lambdaBridgeExpectedLNT); 1.145 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.146 + "Foo.class").toUri()), "assignLambda", assignmentExpectedLNT); 1.147 + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), 1.148 + "Foo.class").toUri()), "callLambda", callExpectedLNT); 1.149 + } 1.150 + 1.151 + void compileTestClass() throws Exception { 1.152 + ToolBox.JavaToolArgs javacSuccessArgs = 1.153 + new ToolBox.JavaToolArgs().setSources(testSource); 1.154 + ToolBox.javac(javacSuccessArgs); 1.155 + } 1.156 + 1.157 + void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception { 1.158 + ClassFile classFile = ClassFile.read(cfile); 1.159 + boolean methodFound = false; 1.160 + for (Method method : classFile.methods) { 1.161 + if (method.getName(classFile.constant_pool).equals(methodToFind)) { 1.162 + methodFound = true; 1.163 + Code_attribute code = (Code_attribute) method.attributes.get("Code"); 1.164 + LineNumberTable_attribute lnt = 1.165 + (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); 1.166 + Assert.check(lnt.line_number_table_length == expectedLNT.length, 1.167 + "The LineNumberTable found has a length different to the expected one"); 1.168 + int i = 0; 1.169 + for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { 1.170 + Assert.check(entry.line_number == expectedLNT[i][0] && 1.171 + entry.start_pc == expectedLNT[i][1], 1.172 + "LNT entry at pos " + i + " differ from expected." + 1.173 + "Found " + entry.line_number + ":" + entry.start_pc + 1.174 + ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); 1.175 + i++; 1.176 + } 1.177 + } 1.178 + } 1.179 + Assert.check(methodFound, "The seek method was not found"); 1.180 + } 1.181 + 1.182 + void error(String msg) { 1.183 + throw new AssertionError(msg); 1.184 + } 1.185 + 1.186 +}