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