1 /*
   2  * Copyright (c) 2013, 2015, 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  */
  23 
  24 /*
  25  * @test
  26  * @summary Ensure the LV table entries are generated for bindings
  27  * @modules jdk.jdeps/com.sun.tools.classfile
  28  * @compile -g LocalVariableTable.java
  29  * @run main LocalVariableTable
  30  */
  31 
  32 import java.io.*;
  33 import java.lang.annotation.*;
  34 import java.util.*;
  35 import com.sun.tools.classfile.*;
  36 
  37 /*
  38  * The test checks that a LocalVariableTable attribute is generated for the
  39  * method bodies containing patterns, and checks that the expected
  40  * set of entries is found in the attribute.
  41  *
  42  * The test looks for test cases represented by nested classes whose
  43  * name begins with "Pattern".  Each such class contains a method
  44  * with patterns, and because the test is compiled
  45  * with -g, these methods should have a LocalVariableTable.  The set of
  46  * expected names in the LVT is provided in an annotation on the class for
  47  * the test case.
  48  */
  49 //Copied from: test/langtools/tools/javac/lambda/LocalVariableTable.java
  50 public class LocalVariableTable {
  51     public static void main(String... args) throws Exception {
  52         new LocalVariableTable().run();
  53     }
  54 
  55     void run() throws Exception {
  56         // the declared classes are returned in an unspecified order,
  57         // so for neatness, sort them by name before processing them
  58         Class<?>[] classes = getClass().getDeclaredClasses();
  59         Arrays.sort(classes, (c1, c2) -> c1.getName().compareTo(c2.getName()));
  60 
  61         for (Class<?> c : classes) {
  62             if (c.getSimpleName().startsWith("Pattern"))
  63                 check(c);
  64         }
  65         if (errors > 0)
  66             throw new Exception(errors + " errors found");
  67     }
  68 
  69     /** Check an individual test case. */
  70     void check(Class<?> c) throws Exception {
  71         System.err.println("Checking " + c.getSimpleName());
  72 
  73         Expect expect = c.getAnnotation(Expect.class);
  74         if (expect == null) {
  75             error("@Expect not found for class " + c.getSimpleName());
  76             return;
  77         }
  78 
  79         ClassFile cf = ClassFile.read(getClass().getResource(c.getName() + ".class").openStream());
  80         Method m = getMethodByName(cf, c.getSimpleName().contains("Lambda") ? "lambda$" : "test");
  81         if (m == null) {
  82             error("test method not found");
  83             return;
  84         }
  85 
  86         Code_attribute code = (Code_attribute) m.attributes.get(Attribute.Code);
  87         if (code == null) {
  88             error("Code attribute not found");
  89             return;
  90         }
  91 
  92         LocalVariableTable_attribute lvt =
  93                 (LocalVariableTable_attribute) code.attributes.get(Attribute.LocalVariableTable);
  94         if (lvt == null) {
  95             error("LocalVariableTable attribute not found");
  96             return;
  97         }
  98 
  99         Set<String> foundNames = new LinkedHashSet<>();
 100         for (LocalVariableTable_attribute.Entry e: lvt.local_variable_table) {
 101             foundNames.add(cf.constant_pool.getUTF8Value(e.name_index));
 102         }
 103 
 104         Set<String> expectNames = new LinkedHashSet<>(Arrays.asList(expect.value()));
 105         if (!foundNames.equals(expectNames)) {
 106             Set<String> foundOnly = new LinkedHashSet<>(foundNames);
 107             foundOnly.removeAll(expectNames);
 108             for (String s: foundOnly)
 109                 error("Unexpected name found: " + s);
 110             Set<String> expectOnly = new LinkedHashSet<>(expectNames);
 111             expectOnly.removeAll(foundNames);
 112             for (String s: expectOnly)
 113                 error("Expected name not found: " + s);
 114         }
 115     }
 116 
 117     Method getMethodByName(ClassFile cf, String name) throws ConstantPoolException {
 118         for (Method m: cf.methods) {
 119             if (m.getName(cf.constant_pool).startsWith(name))
 120                 return m;
 121         }
 122         return null;
 123     }
 124 
 125     /** Report an error. */
 126     void error(String msg) {
 127         System.err.println("Error: " + msg);
 128         errors++;
 129     }
 130 
 131     int errors;
 132 
 133     /**
 134      * Annotation used to provide the set of names expected in the LVT attribute.
 135      */
 136     @Retention(RetentionPolicy.RUNTIME)
 137     @interface Expect {
 138         String[] value();
 139     }
 140 
 141     /*
 142      * ---------- Test cases ---------------------------------------------------
 143      */
 144 
 145     @Expect({ "o", "s" })
 146     static class Pattern_Simple {
 147         public static void test(Object o) {
 148             if (o instanceof String s) {}
 149         }
 150     }
 151 
 152     @Expect({ "s" })
 153     static class Pattern_Lambda {
 154         public static void test(Object o) {
 155             if (o instanceof String s) {
 156                 Runnable r = () -> {
 157                     s.length();
 158                 };
 159             }
 160         }
 161     }
 162 
 163 }
 164