jjg@2114: /* jjg@2114: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. jjg@2114: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@2114: * jjg@2114: * This code is free software; you can redistribute it and/or modify it jjg@2114: * under the terms of the GNU General Public License version 2 only, as jjg@2114: * published by the Free Software Foundation. jjg@2114: * jjg@2114: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@2114: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@2114: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@2114: * version 2 for more details (a copy is included in the LICENSE file that jjg@2114: * accompanied this code). jjg@2114: * jjg@2114: * You should have received a copy of the GNU General Public License version jjg@2114: * 2 along with this work; if not, write to the Free Software Foundation, jjg@2114: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@2114: * jjg@2114: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@2114: * or visit www.oracle.com if you need additional information or have any jjg@2114: * questions. jjg@2114: */ jjg@2114: jjg@2114: /* jjg@2114: * @test jjg@2114: * @bug 8025998 jjg@2114: * @summary Missing LV table in lambda bodies jjg@2114: * @compile -g LocalVariableTable.java jjg@2114: * @run main LocalVariableTable jjg@2114: */ jjg@2114: jjg@2114: import java.lang.annotation.*; jjg@2114: import java.util.*; jjg@2114: import com.sun.tools.classfile.*; jjg@2114: jjg@2114: /* jjg@2114: * The test checks that a LocalVariableTable attribute is generated for the jjg@2114: * method bodies representing lambda expressions, and checks that the expected jjg@2114: * set of entries is found in the attribute. jjg@2114: * jjg@2114: * Since the bug was about missing entries in the LVT, not malformed entries, jjg@2114: * the test is not intended to be a detailed test of the contents of each jjg@2114: * LocalVariableTable entry: it is assumed that if a entry is present, it jjg@2114: * will have the correct contents. jjg@2114: * jjg@2114: * The test looks for test cases represented by nested classes whose jjg@2114: * name begins with "Lambda". Each such class contains a lambda expression jjg@2114: * that will mapped into a lambda method, and because the test is compiled jjg@2114: * with -g, these methods should have a LocalVariableTable. The set of jjg@2114: * expected names in the LVT is provided in an annotation on the class for jjg@2114: * the test case. jjg@2114: */ jjg@2114: public class LocalVariableTable { jjg@2114: public static void main(String... args) throws Exception { jjg@2114: new LocalVariableTable().run(); jjg@2114: } jjg@2114: jjg@2114: void run() throws Exception { jjg@2114: // the declared classes are returned in an unspecified order, jjg@2114: // so for neatness, sort them by name before processing them jjg@2114: Class[] classes = getClass().getDeclaredClasses(); jjg@2114: Arrays.sort(classes, (c1, c2) -> c1.getName().compareTo(c2.getName())); jjg@2114: jjg@2114: for (Class c : classes) { jjg@2114: if (c.getSimpleName().startsWith("Lambda")) jjg@2114: check(c); jjg@2114: } jjg@2114: if (errors > 0) jjg@2114: throw new Exception(errors + " errors found"); jjg@2114: } jjg@2114: jjg@2114: /** Check an individual test case. */ jjg@2114: void check(Class c) throws Exception { jjg@2114: System.err.println("Checking " + c.getSimpleName()); jjg@2114: jjg@2114: Expect expect = c.getAnnotation(Expect.class); jjg@2114: if (expect == null) { jjg@2114: error("@Expect not found for class " + c.getSimpleName()); jjg@2114: return; jjg@2114: } jjg@2114: jjg@2114: ClassFile cf = ClassFile.read(getClass().getResource(c.getName() + ".class").openStream()); jjg@2114: Method m = getLambdaMethod(cf); jjg@2114: if (m == null) { jjg@2114: error("lambda method not found"); jjg@2114: return; jjg@2114: } jjg@2114: jjg@2114: Code_attribute code = (Code_attribute) m.attributes.get(Attribute.Code); jjg@2114: if (code == null) { jjg@2114: error("Code attribute not found"); jjg@2114: return; jjg@2114: } jjg@2114: jjg@2114: LocalVariableTable_attribute lvt = jjg@2114: (LocalVariableTable_attribute) code.attributes.get(Attribute.LocalVariableTable); jjg@2114: if (lvt == null) { jjg@2114: error("LocalVariableTable attribute not found"); jjg@2114: return; jjg@2114: } jjg@2114: jjg@2114: Set foundNames = new LinkedHashSet<>(); jjg@2114: for (LocalVariableTable_attribute.Entry e: lvt.local_variable_table) { jjg@2114: foundNames.add(cf.constant_pool.getUTF8Value(e.name_index)); jjg@2114: } jjg@2114: jjg@2114: Set expectNames = new LinkedHashSet<>(Arrays.asList(expect.value())); jjg@2114: if (!foundNames.equals(expectNames)) { jjg@2114: Set foundOnly = new LinkedHashSet<>(foundNames); jjg@2114: foundOnly.removeAll(expectNames); jjg@2114: for (String s: foundOnly) jjg@2114: error("Unexpected name found: " + s); jjg@2114: Set expectOnly = new LinkedHashSet<>(expectNames); jjg@2114: expectOnly.removeAll(foundNames); jjg@2114: for (String s: expectOnly) jjg@2114: error("Expected name not found: " + s); jjg@2114: } jjg@2114: } jjg@2114: jjg@2114: /** Get a method whose name begins "lambda$...". */ jjg@2114: Method getLambdaMethod(ClassFile cf) throws ConstantPoolException { jjg@2114: for (Method m: cf.methods) { jjg@2114: if (m.getName(cf.constant_pool).startsWith("lambda$")) jjg@2114: return m; jjg@2114: } jjg@2114: return null; jjg@2114: } jjg@2114: jjg@2114: /** Report an error. */ jjg@2114: void error(String msg) { jjg@2114: System.err.println("Error: " + msg); jjg@2114: errors++; jjg@2114: } jjg@2114: jjg@2114: int errors; jjg@2114: jjg@2114: /** jjg@2114: * Annotation used to provide the set of names expected in the LVT attribute. jjg@2114: */ jjg@2114: @Retention(RetentionPolicy.RUNTIME) jjg@2114: @interface Expect { jjg@2114: String[] value(); jjg@2114: } jjg@2114: jjg@2114: /** Functional interface with nullary method. */ jjg@2114: interface Run0 { jjg@2114: public void run(); jjg@2114: } jjg@2114: jjg@2114: /** Functional interface with 1-ary method. */ jjg@2114: interface Run1 { jjg@2114: public void run(int a0); jjg@2114: } jjg@2114: jjg@2114: /** Functional interface with 2-ary method. */ jjg@2114: interface Run2 { jjg@2114: public void run(int a0, int a1); jjg@2114: } jjg@2114: jjg@2114: /* jjg@2114: * ---------- Test cases --------------------------------------------------- jjg@2114: */ jjg@2114: jjg@2114: @Expect({ "x" }) jjg@2114: static class Lambda_Args0_Local1 { jjg@2114: Run0 r = () -> { int x = 0; }; jjg@2114: } jjg@2114: jjg@2114: @Expect({ "x", "this" }) jjg@2114: static class Lambda_Args0_Local1_this { jjg@2114: int v; jjg@2114: Run0 r = () -> { int x = v; }; jjg@2114: } jjg@2114: jjg@2114: @Expect({ "a" }) jjg@2114: static class Lambda_Args1_Local0 { jjg@2114: Run1 r = (a) -> { }; jjg@2114: } jjg@2114: jjg@2114: @Expect({ "a", "x" }) jjg@2114: static class Lambda_Args1_Local1 { jjg@2114: Run1 r = (a) -> { int x = a; }; jjg@2114: } jjg@2114: jjg@2114: @Expect({ "a", "x" }) jjg@2114: static class Lambda_Args1_Local1_Captured1 { jjg@2114: void m() { jjg@2114: int v = 0; jjg@2114: Run1 r = (a) -> { int x = a + v; }; jjg@2114: } jjg@2114: } jjg@2114: jjg@2114: @Expect({ "a1", "a2", "x1", "x2", "this" }) jjg@2114: static class Lambda_Args2_Local2_Captured2_this { jjg@2114: int v; jjg@2114: void m() { jjg@2114: int v1 = 0; jjg@2114: int v2 = 0; jjg@2114: Run2 r = (a1, a2) -> { jjg@2114: int x1 = a1 + v1 + v; jjg@2114: int x2 = a2 + v2 + v; jjg@2114: }; jjg@2114: } jjg@2114: } jjg@2114: } jjg@2114: