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