6716452: (classfile) need a method to get the index of an attribute

Tue, 08 Jul 2008 17:53:03 -0700

author
jjg
date
Tue, 08 Jul 2008 17:53:03 -0700
changeset 67
1bdd8cea398e
parent 66
df47f7f4c95a
child 68
c33f7ddeeff2

6716452: (classfile) need a method to get the index of an attribute
Reviewed-by: ksrini

src/share/classes/com/sun/tools/classfile/Attributes.java file | annotate | diff | comparison | revisions
test/tools/javap/T6716452.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/classfile/Attributes.java	Tue Jul 08 17:25:50 2008 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/classfile/Attributes.java	Tue Jul 08 17:53:03 2008 -0700
     1.3 @@ -78,6 +78,19 @@
     1.4          return map.get(name);
     1.5      }
     1.6  
     1.7 +    public int getIndex(ConstantPool constant_pool, String name) {
     1.8 +        for (int i = 0; i < attrs.length; i++) {
     1.9 +            Attribute attr = attrs[i];
    1.10 +            try {
    1.11 +                if (attr != null && attr.getName(constant_pool).equals(name))
    1.12 +                    return i;
    1.13 +            } catch (ConstantPoolException e) {
    1.14 +                // ignore invalid entries
    1.15 +            }
    1.16 +        }
    1.17 +        return -1;
    1.18 +    }
    1.19 +
    1.20      public int size() {
    1.21          return attrs.length;
    1.22      }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javap/T6716452.java	Tue Jul 08 17:53:03 2008 -0700
     2.3 @@ -0,0 +1,113 @@
     2.4 +/*
     2.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    2.24 + * have any questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test 6716452
    2.29 + * @summary need a method to get an index of an attribute
    2.30 + */
    2.31 +
    2.32 +import java.io.*;
    2.33 +import com.sun.tools.classfile.*;
    2.34 +
    2.35 +public class T6716452 {
    2.36 +    public static void main(String[] args) throws Exception {
    2.37 +        new T6716452().run();
    2.38 +    }
    2.39 +
    2.40 +    public void run() throws Exception {
    2.41 +        File javaFile = writeTestFile();
    2.42 +        File classFile = compileTestFile(javaFile);
    2.43 +
    2.44 +        ClassFile cf = ClassFile.read(classFile);
    2.45 +        for (Method m: cf.methods) {
    2.46 +            test(cf, m);
    2.47 +        }
    2.48 +
    2.49 +        if (errors > 0)
    2.50 +            throw new Exception(errors + " errors found");
    2.51 +    }
    2.52 +
    2.53 +    void test(ClassFile cf, Method m) {
    2.54 +        test(cf, m, Attribute.Code, Code_attribute.class);
    2.55 +        test(cf, m, Attribute.Exceptions, Exceptions_attribute.class);
    2.56 +    }
    2.57 +
    2.58 +    // test the result of Attributes.getIndex according to expectations
    2.59 +    // encoded in the method's name
    2.60 +    void test(ClassFile cf, Method m, String name, Class<?> c) {
    2.61 +        int index = m.attributes.getIndex(cf.constant_pool, name);
    2.62 +        try {
    2.63 +            String m_name = m.getName(cf.constant_pool);
    2.64 +            System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c);
    2.65 +            boolean expect = (m_name.equals("<init>") && name.equals("Code"))
    2.66 +                || (m_name.indexOf(name) != -1);
    2.67 +            boolean found = (index != -1);
    2.68 +            if (expect) {
    2.69 +                if (found) {
    2.70 +                    Attribute attr = m.attributes.get(index);
    2.71 +                    if (!c.isAssignableFrom(attr.getClass())) {
    2.72 +                        error(m + ": unexpected attribute found,"
    2.73 +                              + " expected " + c.getName()
    2.74 +                              + " found " + attr.getClass().getName());
    2.75 +                    }
    2.76 +                } else {
    2.77 +                    error(m + ": expected attribute " + name + " not found");
    2.78 +                }
    2.79 +            } else {
    2.80 +                if (found) {
    2.81 +                    error(m + ": unexpected attribute " + name);
    2.82 +                }
    2.83 +            }
    2.84 +        } catch (ConstantPoolException e) {
    2.85 +            error(m + ": " + e);
    2.86 +        }
    2.87 +    }
    2.88 +
    2.89 +    File writeTestFile() throws IOException {
    2.90 +        File f = new File("Test.java");
    2.91 +        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
    2.92 +        out.println("abstract class Test { ");
    2.93 +        out.println("  abstract void m();");
    2.94 +        out.println("  void m_Code() { }");
    2.95 +        out.println("  abstract void m_Exceptions() throws Exception;");
    2.96 +        out.println("  void m_Code_Exceptions() throws Exception { }");
    2.97 +        out.println("}");
    2.98 +        out.close();
    2.99 +        return f;
   2.100 +    }
   2.101 +
   2.102 +    File compileTestFile(File f) {
   2.103 +        int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
   2.104 +        if (rc != 0)
   2.105 +            throw new Error("compilation failed. rc=" + rc);
   2.106 +        String path = f.getPath();
   2.107 +        return new File(path.substring(0, path.length() - 5) + ".class");
   2.108 +    }
   2.109 +
   2.110 +    void error(String msg) {
   2.111 +        System.err.println("error: " + msg);
   2.112 +        errors++;
   2.113 +    }
   2.114 +
   2.115 +    int errors;
   2.116 +}

mercurial