66N/A/*
553N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
66N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66N/A *
66N/A * This code is free software; you can redistribute it and/or modify it
66N/A * under the terms of the GNU General Public License version 2 only, as
66N/A * published by the Free Software Foundation.
66N/A *
66N/A * This code is distributed in the hope that it will be useful, but WITHOUT
66N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
66N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
66N/A * version 2 for more details (a copy is included in the LICENSE file that
66N/A * accompanied this code).
66N/A *
66N/A * You should have received a copy of the GNU General Public License version
66N/A * 2 along with this work; if not, write to the Free Software Foundation,
66N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
66N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
66N/A */
66N/A
66N/A/*
66N/A * @test 6716452
66N/A * @summary need a method to get an index of an attribute
66N/A */
66N/A
66N/Aimport java.io.*;
66N/Aimport com.sun.tools.classfile.*;
66N/A
66N/Apublic class T6716452 {
66N/A public static void main(String[] args) throws Exception {
66N/A new T6716452().run();
66N/A }
66N/A
66N/A public void run() throws Exception {
66N/A File javaFile = writeTestFile();
66N/A File classFile = compileTestFile(javaFile);
66N/A
66N/A ClassFile cf = ClassFile.read(classFile);
66N/A for (Method m: cf.methods) {
66N/A test(cf, m);
66N/A }
66N/A
66N/A if (errors > 0)
66N/A throw new Exception(errors + " errors found");
66N/A }
66N/A
66N/A void test(ClassFile cf, Method m) {
66N/A test(cf, m, Attribute.Code, Code_attribute.class);
66N/A test(cf, m, Attribute.Exceptions, Exceptions_attribute.class);
66N/A }
66N/A
66N/A // test the result of Attributes.getIndex according to expectations
66N/A // encoded in the method's name
66N/A void test(ClassFile cf, Method m, String name, Class<?> c) {
66N/A int index = m.attributes.getIndex(cf.constant_pool, name);
66N/A try {
66N/A String m_name = m.getName(cf.constant_pool);
66N/A System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c);
66N/A boolean expect = (m_name.equals("<init>") && name.equals("Code"))
66N/A || (m_name.indexOf(name) != -1);
66N/A boolean found = (index != -1);
66N/A if (expect) {
66N/A if (found) {
66N/A Attribute attr = m.attributes.get(index);
66N/A if (!c.isAssignableFrom(attr.getClass())) {
66N/A error(m + ": unexpected attribute found,"
66N/A + " expected " + c.getName()
66N/A + " found " + attr.getClass().getName());
66N/A }
66N/A } else {
66N/A error(m + ": expected attribute " + name + " not found");
66N/A }
66N/A } else {
66N/A if (found) {
66N/A error(m + ": unexpected attribute " + name);
66N/A }
66N/A }
66N/A } catch (ConstantPoolException e) {
66N/A error(m + ": " + e);
66N/A }
66N/A }
66N/A
66N/A File writeTestFile() throws IOException {
66N/A File f = new File("Test.java");
66N/A PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
66N/A out.println("abstract class Test { ");
66N/A out.println(" abstract void m();");
66N/A out.println(" void m_Code() { }");
66N/A out.println(" abstract void m_Exceptions() throws Exception;");
66N/A out.println(" void m_Code_Exceptions() throws Exception { }");
66N/A out.println("}");
66N/A out.close();
66N/A return f;
66N/A }
66N/A
66N/A File compileTestFile(File f) {
66N/A int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
66N/A if (rc != 0)
66N/A throw new Error("compilation failed. rc=" + rc);
66N/A String path = f.getPath();
66N/A return new File(path.substring(0, path.length() - 5) + ".class");
66N/A }
66N/A
66N/A void error(String msg) {
66N/A System.err.println("error: " + msg);
66N/A errors++;
66N/A }
66N/A
66N/A int errors;
66N/A}