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