1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/LocalVariableTable.java Fri Oct 18 15:03:34 2013 -0700 1.3 @@ -0,0 +1,220 @@ 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. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8025998 8026749 1.30 + * @summary Missing LV table in lambda bodies 1.31 + * @compile -g LocalVariableTable.java 1.32 + * @run main LocalVariableTable 1.33 + */ 1.34 + 1.35 +import java.io.*; 1.36 +import java.lang.annotation.*; 1.37 +import java.util.*; 1.38 +import com.sun.tools.classfile.*; 1.39 + 1.40 +/* 1.41 + * The test checks that a LocalVariableTable attribute is generated for the 1.42 + * method bodies representing lambda expressions, and checks that the expected 1.43 + * set of entries is found in the attribute. 1.44 + * 1.45 + * Since the bug was about missing entries in the LVT, not malformed entries, 1.46 + * the test is not intended to be a detailed test of the contents of each 1.47 + * LocalVariableTable entry: it is assumed that if a entry is present, it 1.48 + * will have the correct contents. 1.49 + * 1.50 + * The test looks for test cases represented by nested classes whose 1.51 + * name begins with "Lambda". Each such class contains a lambda expression 1.52 + * that will mapped into a lambda method, and because the test is compiled 1.53 + * with -g, these methods should have a LocalVariableTable. The set of 1.54 + * expected names in the LVT is provided in an annotation on the class for 1.55 + * the test case. 1.56 + */ 1.57 +public class LocalVariableTable { 1.58 + public static void main(String... args) throws Exception { 1.59 + new LocalVariableTable().run(); 1.60 + } 1.61 + 1.62 + void run() throws Exception { 1.63 + // the declared classes are returned in an unspecified order, 1.64 + // so for neatness, sort them by name before processing them 1.65 + Class<?>[] classes = getClass().getDeclaredClasses(); 1.66 + Arrays.sort(classes, (c1, c2) -> c1.getName().compareTo(c2.getName())); 1.67 + 1.68 + for (Class<?> c : classes) { 1.69 + if (c.getSimpleName().startsWith("Lambda")) 1.70 + check(c); 1.71 + } 1.72 + if (errors > 0) 1.73 + throw new Exception(errors + " errors found"); 1.74 + } 1.75 + 1.76 + /** Check an individual test case. */ 1.77 + void check(Class<?> c) throws Exception { 1.78 + System.err.println("Checking " + c.getSimpleName()); 1.79 + 1.80 + Expect expect = c.getAnnotation(Expect.class); 1.81 + if (expect == null) { 1.82 + error("@Expect not found for class " + c.getSimpleName()); 1.83 + return; 1.84 + } 1.85 + 1.86 + ClassFile cf = ClassFile.read(getClass().getResource(c.getName() + ".class").openStream()); 1.87 + Method m = getLambdaMethod(cf); 1.88 + if (m == null) { 1.89 + error("lambda method not found"); 1.90 + return; 1.91 + } 1.92 + 1.93 + Code_attribute code = (Code_attribute) m.attributes.get(Attribute.Code); 1.94 + if (code == null) { 1.95 + error("Code attribute not found"); 1.96 + return; 1.97 + } 1.98 + 1.99 + LocalVariableTable_attribute lvt = 1.100 + (LocalVariableTable_attribute) code.attributes.get(Attribute.LocalVariableTable); 1.101 + if (lvt == null) { 1.102 + error("LocalVariableTable attribute not found"); 1.103 + return; 1.104 + } 1.105 + 1.106 + Set<String> foundNames = new LinkedHashSet<>(); 1.107 + for (LocalVariableTable_attribute.Entry e: lvt.local_variable_table) { 1.108 + foundNames.add(cf.constant_pool.getUTF8Value(e.name_index)); 1.109 + } 1.110 + 1.111 + Set<String> expectNames = new LinkedHashSet<>(Arrays.asList(expect.value())); 1.112 + if (!foundNames.equals(expectNames)) { 1.113 + Set<String> foundOnly = new LinkedHashSet<>(foundNames); 1.114 + foundOnly.removeAll(expectNames); 1.115 + for (String s: foundOnly) 1.116 + error("Unexpected name found: " + s); 1.117 + Set<String> expectOnly = new LinkedHashSet<>(expectNames); 1.118 + expectOnly.removeAll(foundNames); 1.119 + for (String s: expectOnly) 1.120 + error("Expected name not found: " + s); 1.121 + } 1.122 + } 1.123 + 1.124 + /** Get a method whose name begins "lambda$...". */ 1.125 + Method getLambdaMethod(ClassFile cf) throws ConstantPoolException { 1.126 + for (Method m: cf.methods) { 1.127 + if (m.getName(cf.constant_pool).startsWith("lambda$")) 1.128 + return m; 1.129 + } 1.130 + return null; 1.131 + } 1.132 + 1.133 + /** Report an error. */ 1.134 + void error(String msg) { 1.135 + System.err.println("Error: " + msg); 1.136 + errors++; 1.137 + } 1.138 + 1.139 + int errors; 1.140 + 1.141 + /** 1.142 + * Annotation used to provide the set of names expected in the LVT attribute. 1.143 + */ 1.144 + @Retention(RetentionPolicy.RUNTIME) 1.145 + @interface Expect { 1.146 + String[] value(); 1.147 + } 1.148 + 1.149 + /** Functional interface with nullary method. */ 1.150 + interface Run0 { 1.151 + public void run(); 1.152 + } 1.153 + 1.154 + /** Functional interface with 1-ary method. */ 1.155 + interface Run1 { 1.156 + public void run(int a0); 1.157 + } 1.158 + 1.159 + /** Functional interface with 2-ary method. */ 1.160 + interface Run2 { 1.161 + public void run(int a0, int a1); 1.162 + } 1.163 + 1.164 + /* 1.165 + * ---------- Test cases --------------------------------------------------- 1.166 + */ 1.167 + 1.168 + @Expect({ "x" }) 1.169 + static class Lambda_Args0_Local1 { 1.170 + Run0 r = () -> { int x = 0; }; 1.171 + } 1.172 + 1.173 + @Expect({ "x", "this" }) 1.174 + static class Lambda_Args0_Local1_this { 1.175 + int v; 1.176 + Run0 r = () -> { int x = v; }; 1.177 + } 1.178 + 1.179 + @Expect({ "a" }) 1.180 + static class Lambda_Args1_Local0 { 1.181 + Run1 r = (a) -> { }; 1.182 + } 1.183 + 1.184 + @Expect({ "a", "x" }) 1.185 + static class Lambda_Args1_Local1 { 1.186 + Run1 r = (a) -> { int x = a; }; 1.187 + } 1.188 + 1.189 + @Expect({ "a", "x" }) 1.190 + static class Lambda_Args1_Local1_Captured1 { 1.191 + void m() { 1.192 + int v = 0; 1.193 + Run1 r = (a) -> { int x = a + v; }; 1.194 + } 1.195 + } 1.196 + 1.197 + @Expect({ "a1", "a2", "x1", "x2", "this" }) 1.198 + static class Lambda_Args2_Local2_Captured2_this { 1.199 + int v; 1.200 + void m() { 1.201 + int v1 = 0; 1.202 + int v2 = 0; 1.203 + Run2 r = (a1, a2) -> { 1.204 + int x1 = a1 + v1 + v; 1.205 + int x2 = a2 + v2 + v; 1.206 + }; 1.207 + } 1.208 + } 1.209 + 1.210 + @Expect({ "e" }) 1.211 + static class Lambda_Try_Catch { 1.212 + private static Runnable asUncheckedRunnable(Closeable c) { 1.213 + return () -> { 1.214 + try { 1.215 + c.close(); 1.216 + } catch (IOException e) { 1.217 + throw new UncheckedIOException(e); 1.218 + } 1.219 + }; 1.220 + } 1.221 + } 1.222 +} 1.223 +