WrongLNTForLambdaTest.java revision 3294:9adfb22ff08f
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 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.file 31 * jdk.compiler/com.sun.tools.javac.main 32 * jdk.compiler/com.sun.tools.javac.util 33 * jdk.jdeps/com.sun.tools.classfile 34 * jdk.jdeps/com.sun.tools.javap 35 * @build ToolBox 36 * @run main WrongLNTForLambdaTest 37 */ 38 39import java.io.File; 40import java.nio.file.Paths; 41 42import com.sun.tools.classfile.ClassFile; 43import com.sun.tools.classfile.Code_attribute; 44import com.sun.tools.classfile.LineNumberTable_attribute; 45import com.sun.tools.classfile.Method; 46import com.sun.tools.javac.util.Assert; 47 48public 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 tb.new JavacTask() 156 .sources(testSource) 157 .run(); 158 } 159 160 void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception { 161 ClassFile classFile = ClassFile.read(cfile); 162 boolean methodFound = false; 163 for (Method method : classFile.methods) { 164 if (method.getName(classFile.constant_pool).equals(methodToFind)) { 165 methodFound = true; 166 Code_attribute code = (Code_attribute) method.attributes.get("Code"); 167 LineNumberTable_attribute lnt = 168 (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); 169 Assert.check(lnt.line_number_table_length == expectedLNT.length, 170 "The LineNumberTable found has a length different to the expected one"); 171 int i = 0; 172 for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { 173 Assert.check(entry.line_number == expectedLNT[i][0] && 174 entry.start_pc == expectedLNT[i][1], 175 "LNT entry at pos " + i + " differ from expected." + 176 "Found " + entry.line_number + ":" + entry.start_pc + 177 ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); 178 i++; 179 } 180 } 181 } 182 Assert.check(methodFound, "The seek method was not found"); 183 } 184 185 void error(String msg) { 186 throw new AssertionError(msg); 187 } 188 189} 190