Lookup BCEL Method for a particular java.lang.reflect.Method

Davanum Srinivas <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <[email protected]>
Folks,

Can someone help me port from serp/tt-bytecode to BCEL? We construct a JavaClass from a
java.lang.Class (see code below).  We have a java.lang.reflect.Method for which we need to find
the corresponding org.apache.bcel.classfile.Method. How do i do it? java.lang.reflect.Method's
toString() and org.apache.bcel.classfile.Method's toString() don't really output the same stuff. 

We need to get this information to move Apache Axis (http://xml.apache.org/axis/) from
Serp/tt-bytecode to BCEL.

Thanks in Advance,
dims

----------------------------------------------------------------------------------------------
    /**
     * Get Parameter Names using BCEL
     *
     * @param method the Java method we're interested in
     * @return list of names or null
     */
    public String[] getParameterNamesFromDebugInfo(java.lang.reflect.Method m) {
        Class c = m.getDeclaringClass();
        int numParams = m.getParameterTypes().length;
        Vector temp = new Vector();

        // Don't worry about it if there are no params.
        if (numParams == 0)
            return null;

        // Try to obtain a tt-bytecode class object
        JavaClass bclass = (JavaClass) clazzCache.get(c);

        if (bclass == null) {
            try {
                String className = c.getName();
                String classFile = className.replace('.', '/');
                InputStream is =
                        c.getClassLoader().getResourceAsStream(classFile + ".class");

                ClassParser parser = new ClassParser(is, className);
                bclass = parser.parse();

                clazzCache.put(c, bclass);
            } catch (IOException e) {
                // what now?
            }
        }

        Method[] methods = bclass.getMethods();
        Method method = null;
        // HOW DO I GET THE BCEL METHOD CORRESPONDING TO
        // a java.lang.reflect.Method?
        for (int i = 0; i < methods.length; i++) {
            if (m.getName() == methods[i].getName() &&
                    m.toString().equals(methods[i].toString())) {
                method = methods[i];
                break;
            }
        }

        // Obtain the exact method we're interested in.
        if (method == null)
            return null;

        // Get the Code object, which contains the local variable table.
        Code code = method.getCode();
        if (code == null)
            return null;

        LocalVariableTable attr = method.getLocalVariableTable();
        if (attr == null)
            return null;

        // OK, found it.  Now scan through the local variables and record
        // the names in the right indices.
        LocalVariable[] vars = attr.getLocalVariableTable();

        String[] argNames = new String[numParams + 1];
        argNames[0] = null; // don't know return name

        // NOTE: we scan through all the variables here, because I have been
        // told that jikes sometimes produces unpredictable ordering of the
        // local variable table.
        for (int j = 0; j < vars.length; j++) {
            LocalVariable var = vars[j];
            if (!var.getName().equals("this")) {
                if (temp.size() < var.getIndex() + 1)
                    temp.setSize(var.getIndex() + 1);
                temp.setElementAt(var.getName(), var.getIndex());
            }
        }
        int k = 0;
        for (int j = 0; j < temp.size(); j++) {
            if (temp.elementAt(j) != null) {
                k++;
                argNames[k] = (String) temp.elementAt(j);
                if (k + 1 == argNames.length)
                    break;
            }
        }
        return argNames;
    }
----------------------------------------------------------------------------------------------

=====
Davanum Srinivas - http://xml.apache.org/~dims/

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.