T6716452.java revision 2942:08092deced3f
1/*
2 * Copyright (c) 2008, 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 6716452
26 * @summary need a method to get an index of an attribute
27 * @modules jdk.jdeps/com.sun.tools.classfile
28 */
29
30import java.io.*;
31import com.sun.tools.classfile.*;
32
33public class T6716452 {
34    public static void main(String[] args) throws Exception {
35        new T6716452().run();
36    }
37
38    public void run() throws Exception {
39        File javaFile = writeTestFile();
40        File classFile = compileTestFile(javaFile);
41
42        ClassFile cf = ClassFile.read(classFile);
43        for (Method m: cf.methods) {
44            test(cf, m);
45        }
46
47        if (errors > 0)
48            throw new Exception(errors + " errors found");
49    }
50
51    void test(ClassFile cf, Method m) {
52        test(cf, m, Attribute.Code, Code_attribute.class);
53        test(cf, m, Attribute.Exceptions, Exceptions_attribute.class);
54    }
55
56    // test the result of Attributes.getIndex according to expectations
57    // encoded in the method's name
58    void test(ClassFile cf, Method m, String name, Class<?> c) {
59        int index = m.attributes.getIndex(cf.constant_pool, name);
60        try {
61            String m_name = m.getName(cf.constant_pool);
62            System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c);
63            boolean expect = (m_name.equals("<init>") && name.equals("Code"))
64                || (m_name.indexOf(name) != -1);
65            boolean found = (index != -1);
66            if (expect) {
67                if (found) {
68                    Attribute attr = m.attributes.get(index);
69                    if (!c.isAssignableFrom(attr.getClass())) {
70                        error(m + ": unexpected attribute found,"
71                              + " expected " + c.getName()
72                              + " found " + attr.getClass().getName());
73                    }
74                } else {
75                    error(m + ": expected attribute " + name + " not found");
76                }
77            } else {
78                if (found) {
79                    error(m + ": unexpected attribute " + name);
80                }
81            }
82        } catch (ConstantPoolException e) {
83            error(m + ": " + e);
84        }
85    }
86
87    File writeTestFile() throws IOException {
88        File f = new File("Test.java");
89        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
90        out.println("abstract class Test { ");
91        out.println("  abstract void m();");
92        out.println("  void m_Code() { }");
93        out.println("  abstract void m_Exceptions() throws Exception;");
94        out.println("  void m_Code_Exceptions() throws Exception { }");
95        out.println("}");
96        out.close();
97        return f;
98    }
99
100    File compileTestFile(File f) {
101        int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
102        if (rc != 0)
103            throw new Error("compilation failed. rc=" + rc);
104        String path = f.getPath();
105        return new File(path.substring(0, path.length() - 5) + ".class");
106    }
107
108    void error(String msg) {
109        System.err.println("error: " + msg);
110        errors++;
111    }
112
113    int errors;
114}
115