1 /*
2  * Copyright (c) 2010, 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  * @bug 7005371
27  * @summary  Multicatch: assertion error while generating LocalVariableTypeTable attribute
28  * @modules jdk.jdeps/com.sun.tools.classfile
29  * @compile -g SubTest.java
30  * @run main T7005371
31  */
32 
33 import com.sun.tools.classfile.Attribute;
34 import com.sun.tools.classfile.ClassFile;
35 import com.sun.tools.classfile.Code_attribute;
36 import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
37 import com.sun.tools.classfile.Method;
38 
39 import java.io.*;
40 
41 public class T7005371 {
42 
43     static final String SUBTEST_NAME = SubTest.class.getName() + ".class";
44     static final String TEST_METHOD_NAME = "test";
45     static final int LVT_LENGTH = 1;
46     static final String LVT_SIG_TYPE = "Ljava/util/List<Ljava/lang/String;>;";
47 
48 
main(String... args)49     public static void main(String... args) throws Exception {
50         new T7005371().run();
51     }
52 
run()53     public void run() throws Exception {
54         String workDir = System.getProperty("test.classes");
55         System.out.println(workDir);
56         File compiledTest = new File(workDir, SUBTEST_NAME);
57         verifyLocalVariableTypeTableAttr(compiledTest);
58     }
59 
verifyLocalVariableTypeTableAttr(File f)60     void verifyLocalVariableTypeTableAttr(File f) {
61         System.err.println("verify: " + f);
62         try {
63             ClassFile cf = ClassFile.read(f);
64             Method testMethod = null;
65             for (Method m : cf.methods) {
66                 if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
67                     testMethod = m;
68                     break;
69                 }
70             }
71             if (testMethod == null) {
72                 throw new Error("Missing method: " + TEST_METHOD_NAME);
73             }
74             Code_attribute code = (Code_attribute)testMethod.attributes.get(Attribute.Code);
75             if (code == null) {
76                 throw new Error("Missing Code attribute for method: " + TEST_METHOD_NAME);
77             }
78             LocalVariableTypeTable_attribute lvt_table =
79                     (LocalVariableTypeTable_attribute)code.attributes.get(Attribute.LocalVariableTypeTable);
80             if (lvt_table == null) {
81                 throw new Error("Missing LocalVariableTypeTable attribute for method: " + TEST_METHOD_NAME);
82             }
83             if (lvt_table.local_variable_table_length != LVT_LENGTH) {
84                 throw new Error("LocalVariableTypeTable has wrong size" +
85                         "\nfound: " + lvt_table.local_variable_table_length +
86                         "\nrequired: " + LVT_LENGTH);
87             }
88             String sig =
89                     cf.constant_pool.getUTF8Value(lvt_table.local_variable_table[0].signature_index);
90 
91             if (sig == null || !sig.equals(LVT_SIG_TYPE)) {
92                 throw new Error("LocalVariableTypeTable has wrong signature" +
93                         "\nfound: " + sig +
94                         "\nrequired: " + LVT_SIG_TYPE);
95             }
96         } catch (Exception e) {
97             e.printStackTrace();
98             throw new Error("error reading " + f +": " + e);
99         }
100     }
101 }
102