Re: Lookup BCEL Method for a particular java.lang.reflect.Method / Create JavaClass from java.lang.Class

Davanum Srinivas <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <[email protected]>
I went with a slightly different solution, by Constructing the signature for the
java.lang.reflect.Method and check with org.apache.bcel.classfile.Method's getSignature(). One
more use case we has was to construct a JavaClass given a java.lang.Class. See BCEL.java at
http://cvs.apache.org/viewcvs.cgi/xml-axis/java/src/org/apache/axis/utils/bytecode/ on how we got
around both problems. 

Wish #1: Wish this was a helper method in JavaClass with the following Signature
public org.apache.bcel.classfile.Method getMethod(java.lang.reflect.Method). 

Wish #2: Wish i could create a JavaClass from java.lang.Class as shown below.
Class clazz = Class.forName("XXXX");
ClassParser parser = new ClassParser(clazz);
JavaClass jclazz = parser.parse();

I can send in patches for both if anyone is interested.

Thanks,
dims

--- Juozas Baliuka <[email protected]> wrote:
> 
> if declaring class is the same, you can loop and compare method names and 
> argument type names returned from reflection and BCEL method representations.
> 
> 
> At 04:25 2002.06.12 -0700, Davanum Srinivas wrote:
> >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
> >
> >--
> >To unsubscribe, e-mail:   <mailto:[email protected]>
> >For additional commands, e-mail: <mailto:[email protected]>
> 
> 
> 
> --
> To unsubscribe, e-mail:   <mailto:[email protected]>
> For additional commands, e-mail: <mailto:[email protected]>
> 


=====
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.