Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
1 /*
2 * Copyright (c) 2013, 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 */
24 /*
25 * @test
26 * @bug 8025998 8026749 8054220 8058227
27 * @summary Missing LV table in lambda bodies
28 * @compile -g LocalVariableTable.java
29 * @run main LocalVariableTable
30 */
32 import java.io.*;
33 import java.lang.annotation.*;
34 import java.util.*;
35 import com.sun.tools.classfile.*;
37 /*
38 * The test checks that a LocalVariableTable attribute is generated for the
39 * method bodies representing lambda expressions, and checks that the expected
40 * set of entries is found in the attribute.
41 *
42 * Since the bug was about missing entries in the LVT, not malformed entries,
43 * the test is not intended to be a detailed test of the contents of each
44 * LocalVariableTable entry: it is assumed that if a entry is present, it
45 * will have the correct contents.
46 *
47 * The test looks for test cases represented by nested classes whose
48 * name begins with "Lambda". Each such class contains a lambda expression
49 * that will mapped into a lambda method, and because the test is compiled
50 * with -g, these methods should have a LocalVariableTable. The set of
51 * expected names in the LVT is provided in an annotation on the class for
52 * the test case.
53 */
54 public class LocalVariableTable {
55 public static void main(String... args) throws Exception {
56 new LocalVariableTable().run();
57 }
59 void run() throws Exception {
60 // the declared classes are returned in an unspecified order,
61 // so for neatness, sort them by name before processing them
62 Class<?>[] classes = getClass().getDeclaredClasses();
63 Arrays.sort(classes, (c1, c2) -> c1.getName().compareTo(c2.getName()));
65 for (Class<?> c : classes) {
66 if (c.getSimpleName().startsWith("Lambda"))
67 check(c);
68 }
69 if (errors > 0)
70 throw new Exception(errors + " errors found");
71 }
73 /** Check an individual test case. */
74 void check(Class<?> c) throws Exception {
75 System.err.println("Checking " + c.getSimpleName());
77 Expect expect = c.getAnnotation(Expect.class);
78 if (expect == null) {
79 error("@Expect not found for class " + c.getSimpleName());
80 return;
81 }
83 ClassFile cf = ClassFile.read(getClass().getResource(c.getName() + ".class").openStream());
84 Method m = getLambdaMethod(cf);
85 if (m == null) {
86 error("lambda method not found");
87 return;
88 }
90 Code_attribute code = (Code_attribute) m.attributes.get(Attribute.Code);
91 if (code == null) {
92 error("Code attribute not found");
93 return;
94 }
96 LocalVariableTable_attribute lvt =
97 (LocalVariableTable_attribute) code.attributes.get(Attribute.LocalVariableTable);
98 if (lvt == null) {
99 error("LocalVariableTable attribute not found");
100 return;
101 }
103 Set<String> foundNames = new LinkedHashSet<>();
104 for (LocalVariableTable_attribute.Entry e: lvt.local_variable_table) {
105 foundNames.add(cf.constant_pool.getUTF8Value(e.name_index));
106 }
108 Set<String> expectNames = new LinkedHashSet<>(Arrays.asList(expect.value()));
109 if (!foundNames.equals(expectNames)) {
110 Set<String> foundOnly = new LinkedHashSet<>(foundNames);
111 foundOnly.removeAll(expectNames);
112 for (String s: foundOnly)
113 error("Unexpected name found: " + s);
114 Set<String> expectOnly = new LinkedHashSet<>(expectNames);
115 expectOnly.removeAll(foundNames);
116 for (String s: expectOnly)
117 error("Expected name not found: " + s);
118 }
119 }
121 /** Get a method whose name begins "lambda$...". */
122 Method getLambdaMethod(ClassFile cf) throws ConstantPoolException {
123 for (Method m: cf.methods) {
124 if (m.getName(cf.constant_pool).startsWith("lambda$"))
125 return m;
126 }
127 return null;
128 }
130 /** Report an error. */
131 void error(String msg) {
132 System.err.println("Error: " + msg);
133 errors++;
134 }
136 int errors;
138 /**
139 * Annotation used to provide the set of names expected in the LVT attribute.
140 */
141 @Retention(RetentionPolicy.RUNTIME)
142 @interface Expect {
143 String[] value();
144 }
146 /** Functional interface with nullary method. */
147 interface Run0 {
148 public void run();
149 }
151 /** Functional interface with 1-ary method. */
152 interface Run1 {
153 public void run(int a0);
154 }
156 /** Functional interface with 2-ary method. */
157 interface Run2 {
158 public void run(int a0, int a1);
159 }
161 /*
162 * ---------- Test cases ---------------------------------------------------
163 */
165 @Expect({ "x" })
166 static class Lambda_Args0_Local1 {
167 Run0 r = () -> { int x = 0; };
168 }
170 @Expect({ "x", "this" })
171 static class Lambda_Args0_Local1_this {
172 int v;
173 Run0 r = () -> { int x = v; };
174 }
176 @Expect({ "a" })
177 static class Lambda_Args1_Local0 {
178 Run1 r = (a) -> { };
179 }
181 @Expect({ "a", "x" })
182 static class Lambda_Args1_Local1 {
183 Run1 r = (a) -> { int x = a; };
184 }
186 @Expect({ "a", "x", "v" })
187 static class Lambda_Args1_Local1_Captured1 {
188 void m() {
189 int v = 0;
190 Run1 r = (a) -> { int x = a + v; };
191 }
192 }
194 @Expect({ "a1", "a2", "x1", "x2", "this", "v1", "v2" })
195 static class Lambda_Args2_Local2_Captured2_this {
196 int v;
197 void m() {
198 int v1 = 0;
199 int v2 = 0;
200 Run2 r = (a1, a2) -> {
201 int x1 = a1 + v1 + v;
202 int x2 = a2 + v2 + v;
203 };
204 }
205 }
207 @Expect({ "e", "c" })
208 static class Lambda_Try_Catch {
209 private static Runnable asUncheckedRunnable(Closeable c) {
210 return () -> {
211 try {
212 c.close();
213 } catch (IOException e) {
214 throw new UncheckedIOException(e);
215 }
216 };
217 }
218 }
219 }