1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/LocalVariableTable.java Mon Oct 14 23:07:43 2013 -0700 1.3 @@ -0,0 +1,206 @@ 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 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.lang.annotation.*; 1.36 +import java.util.*; 1.37 +import com.sun.tools.classfile.*; 1.38 + 1.39 +/* 1.40 + * The test checks that a LocalVariableTable attribute is generated for the 1.41 + * method bodies representing lambda expressions, and checks that the expected 1.42 + * set of entries is found in the attribute. 1.43 + * 1.44 + * Since the bug was about missing entries in the LVT, not malformed entries, 1.45 + * the test is not intended to be a detailed test of the contents of each 1.46 + * LocalVariableTable entry: it is assumed that if a entry is present, it 1.47 + * will have the correct contents. 1.48 + * 1.49 + * The test looks for test cases represented by nested classes whose 1.50 + * name begins with "Lambda". Each such class contains a lambda expression 1.51 + * that will mapped into a lambda method, and because the test is compiled 1.52 + * with -g, these methods should have a LocalVariableTable. The set of 1.53 + * expected names in the LVT is provided in an annotation on the class for 1.54 + * the test case. 1.55 + */ 1.56 +public class LocalVariableTable { 1.57 + public static void main(String... args) throws Exception { 1.58 + new LocalVariableTable().run(); 1.59 + } 1.60 + 1.61 + void run() throws Exception { 1.62 + // the declared classes are returned in an unspecified order, 1.63 + // so for neatness, sort them by name before processing them 1.64 + Class<?>[] classes = getClass().getDeclaredClasses(); 1.65 + Arrays.sort(classes, (c1, c2) -> c1.getName().compareTo(c2.getName())); 1.66 + 1.67 + for (Class<?> c : classes) { 1.68 + if (c.getSimpleName().startsWith("Lambda")) 1.69 + check(c); 1.70 + } 1.71 + if (errors > 0) 1.72 + throw new Exception(errors + " errors found"); 1.73 + } 1.74 + 1.75 + /** Check an individual test case. */ 1.76 + void check(Class<?> c) throws Exception { 1.77 + System.err.println("Checking " + c.getSimpleName()); 1.78 + 1.79 + Expect expect = c.getAnnotation(Expect.class); 1.80 + if (expect == null) { 1.81 + error("@Expect not found for class " + c.getSimpleName()); 1.82 + return; 1.83 + } 1.84 + 1.85 + ClassFile cf = ClassFile.read(getClass().getResource(c.getName() + ".class").openStream()); 1.86 + Method m = getLambdaMethod(cf); 1.87 + if (m == null) { 1.88 + error("lambda method not found"); 1.89 + return; 1.90 + } 1.91 + 1.92 + Code_attribute code = (Code_attribute) m.attributes.get(Attribute.Code); 1.93 + if (code == null) { 1.94 + error("Code attribute not found"); 1.95 + return; 1.96 + } 1.97 + 1.98 + LocalVariableTable_attribute lvt = 1.99 + (LocalVariableTable_attribute) code.attributes.get(Attribute.LocalVariableTable); 1.100 + if (lvt == null) { 1.101 + error("LocalVariableTable attribute not found"); 1.102 + return; 1.103 + } 1.104 + 1.105 + Set<String> foundNames = new LinkedHashSet<>(); 1.106 + for (LocalVariableTable_attribute.Entry e: lvt.local_variable_table) { 1.107 + foundNames.add(cf.constant_pool.getUTF8Value(e.name_index)); 1.108 + } 1.109 + 1.110 + Set<String> expectNames = new LinkedHashSet<>(Arrays.asList(expect.value())); 1.111 + if (!foundNames.equals(expectNames)) { 1.112 + Set<String> foundOnly = new LinkedHashSet<>(foundNames); 1.113 + foundOnly.removeAll(expectNames); 1.114 + for (String s: foundOnly) 1.115 + error("Unexpected name found: " + s); 1.116 + Set<String> expectOnly = new LinkedHashSet<>(expectNames); 1.117 + expectOnly.removeAll(foundNames); 1.118 + for (String s: expectOnly) 1.119 + error("Expected name not found: " + s); 1.120 + } 1.121 + } 1.122 + 1.123 + /** Get a method whose name begins "lambda$...". */ 1.124 + Method getLambdaMethod(ClassFile cf) throws ConstantPoolException { 1.125 + for (Method m: cf.methods) { 1.126 + if (m.getName(cf.constant_pool).startsWith("lambda$")) 1.127 + return m; 1.128 + } 1.129 + return null; 1.130 + } 1.131 + 1.132 + /** Report an error. */ 1.133 + void error(String msg) { 1.134 + System.err.println("Error: " + msg); 1.135 + errors++; 1.136 + } 1.137 + 1.138 + int errors; 1.139 + 1.140 + /** 1.141 + * Annotation used to provide the set of names expected in the LVT attribute. 1.142 + */ 1.143 + @Retention(RetentionPolicy.RUNTIME) 1.144 + @interface Expect { 1.145 + String[] value(); 1.146 + } 1.147 + 1.148 + /** Functional interface with nullary method. */ 1.149 + interface Run0 { 1.150 + public void run(); 1.151 + } 1.152 + 1.153 + /** Functional interface with 1-ary method. */ 1.154 + interface Run1 { 1.155 + public void run(int a0); 1.156 + } 1.157 + 1.158 + /** Functional interface with 2-ary method. */ 1.159 + interface Run2 { 1.160 + public void run(int a0, int a1); 1.161 + } 1.162 + 1.163 + /* 1.164 + * ---------- Test cases --------------------------------------------------- 1.165 + */ 1.166 + 1.167 + @Expect({ "x" }) 1.168 + static class Lambda_Args0_Local1 { 1.169 + Run0 r = () -> { int x = 0; }; 1.170 + } 1.171 + 1.172 + @Expect({ "x", "this" }) 1.173 + static class Lambda_Args0_Local1_this { 1.174 + int v; 1.175 + Run0 r = () -> { int x = v; }; 1.176 + } 1.177 + 1.178 + @Expect({ "a" }) 1.179 + static class Lambda_Args1_Local0 { 1.180 + Run1 r = (a) -> { }; 1.181 + } 1.182 + 1.183 + @Expect({ "a", "x" }) 1.184 + static class Lambda_Args1_Local1 { 1.185 + Run1 r = (a) -> { int x = a; }; 1.186 + } 1.187 + 1.188 + @Expect({ "a", "x" }) 1.189 + static class Lambda_Args1_Local1_Captured1 { 1.190 + void m() { 1.191 + int v = 0; 1.192 + Run1 r = (a) -> { int x = a + v; }; 1.193 + } 1.194 + } 1.195 + 1.196 + @Expect({ "a1", "a2", "x1", "x2", "this" }) 1.197 + static class Lambda_Args2_Local2_Captured2_this { 1.198 + int v; 1.199 + void m() { 1.200 + int v1 = 0; 1.201 + int v2 = 0; 1.202 + Run2 r = (a1, a2) -> { 1.203 + int x1 = a1 + v1 + v; 1.204 + int x2 = a2 + v2 + v; 1.205 + }; 1.206 + } 1.207 + } 1.208 +} 1.209 +