Koduru, Rajendra Kumar Reddy wrote:
Hi first let me thank you for ur reply..
Now it is working, the missing things were


clazz.setConstantPool(cp.getFinalConstantPool()); clazz.dump(clazz.getClassName() + ".class");

I think you already had the second line, and I think the lack of the first would have caused verification errors if you had actually updated the method in the class using setMethodAt. Are you sure these were the only lines you had to add, or did you actually have to add the setMethodAt code also? (more below)



yes what you were saying is correct that it is limited only to subset of methods, but it was my first program and was starting to expiriment.

So got few more doubts, hope that you can help me in this

  what in the case of the set of methods that you were mentioning..if i
want to instrument, then what should i do

  my idea is to go thru instruction list and fidn wehter the current
instruction is return instruction and insert this instrumentation just
one line above..am i right? That is my next expiriment.

this is a good start, and it may actually be sufficient depending on your requirements. if you want to see your output even when the method exits by throwing an exception, you should add an exception range that spans the entire function and a handler that prints your instrumentation output and then rethrows the exception. at least, i think that's how try {} finally{} is implemented. you should disassemble a method with a try finally to make sure i got that right.



Currently carrying a test case:: adding a method dynamically to already existing class.. The code is as follows [...] I know that i am misssing to add the method to the clazz..but did not find any API for that..please let me know if there is something like that.

ClassGen.addMethod? http://jakarta.apache.org/bcel/apidocs/org/apache/bcel/generic/ClassGen.html#addMethod(org.apache.bcel.classfile.Method)


Hope that you can help me in this regard.

Thank you
Reddy



-----Original Message-----
From: Andrew Huntwork [mailto:[EMAIL PROTECTED] Sent: Montag, 20. Dezember 2004 17:14
To: BCEL Users List
Subject: Re: Hi! Help regarding Inserting instructions in a already
existing class file and updating the modified class file



i think you need to call setMethodAt, replaceMethod, or setMethods.

http://jakarta.apache.org/bcel/apidocs/org/apache/bcel/generic/ClassGen.
html#setMethodAt(org.apache.bcel.classfile.Method,%20int)

btw, your printing technique will only work for a very specialized subset of methods:
- single return at end of method and
- no exceptions and
- no finally


you really want to add this code to method 1 (with code M1):
System.out.println("start");
try { M1 }
finally {System.out.println("end"); }

you should see how that compiles (using javap or something) and replicate it using bcel.

Koduru, Rajendra Kumar Reddy wrote:

Hi,

I just started using BCEL, I want to instrument methods in my class
with system.out.println at start and end of methods


Ex:

[1]
public class HelloBCEL {


public static void main(String[] argv) { String name = null;
try { System.out.print("Please enter your name> "); name = in.readLine(); } catch(IOException e) { return; } System.out.println("Hello, " + name); } }


I want to instrument all the methods of above class with
system.out.println("starting") at the start and
system.out.println("ending") at the end, that is something which looks
like this

[2]
public static void main(String[] argv) { System.out.println("starting");
String name = null;
try { System.out.print("Please enter your name> "); name = in.readLine(); } catch(IOException e) { return; } System.out.println("Hello, " + name); } System.out.println("ending");
}


So i have used BCEL and written a clas which updates the class [1]

into

class [2], The BCEL code written to insert statements is written as
follows



import java.io.*;
import org.apache.bcel.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;

public class insertBCEL{

public static ObjectType p_stream  = null;

         public static void main( String args[] ) throws IOException {

                    ClassParser cparse = new
ClassParser("HelloBCEL.class");

                    JavaClass clazz = cparse.parse();

                    ClassGen cg = new ClassGen(clazz);

                    ConstantPool cp1 = clazz.getConstantPool();

ConstantPoolGen cp = new ConstantPoolGen(cp1);
InstructionFactory factory = new
InstructionFactory(cp);
p_stream = new ObjectType("java.io.PrintStream");

Method[] meths = clazz.getMethods();


                    for(int i = 0; i< meths.length; i++ ){

Code code = meths[i].getCode();

if(code != null) System.out.println(code);

System.out.println(meths[i]);

                      meths[i] =
instrumentMeths(clazz,meths[i],cp,factory);

Code code1 = meths[i].getCode();

if(code1 != null)
System.out.println(code1); }


try {
cg.getJavaClass().dump("HelloBCEL.class");
}catch(Exception e){System.out.println(e);} }



public static Method instrumentMeths(JavaClass clazz, Method method ,ConstantPoolGen cp, InstructionFactory factory){

                        MethodGen mg = new
MethodGen(method,clazz.getClassName(),cp);

InstructionList il = mg.getInstructionList();
InstructionHandle[] ihans =
il.getInstructionHandles();


                        InstructionHandle ihs = il.getStart();  
                        InstructionHandle ihe = il.getEnd();
        
                        InstructionList ils = new InstructionList();
                        InstructionList ile = new InstructionList();
                                
        


ils.append(factory.createFieldAccess("java.lang.System","out",p_stream,C

onstants.GETSTATIC));
ils.append(new PUSH(cp,"starting"));

ils.append(factory.createInvoke("java.io.PrintStream", "println",
Type.VOID, new Type[] { Type.STRING }, Constants.INVOKEVIRTUAL));





ile.append(factory.createFieldAccess("java.lang.System","out",p_stream,C

onstants.GETSTATIC));
ile.append(new PUSH(cp,"ending"));

ile.append(factory.createInvoke("java.io.PrintStream", "println",
Type.VOID, new Type[] { Type.STRING }, Constants.INVOKEVIRTUAL)); InstructionHandle ihss = il.insert(ihs,ils);
il.redirectBranches(ihs, ihss); InstructionHandle ihee = il.insert(ihe,ile);
il.redirectBranches(ihe, ihee); mg.setMaxStack();
return mg.getMethod();
}
}


Then i tried to execute the instrumented class java HelloBCEL , but it
doesnot instrument the strings "starting" and "ending" for the class,
pLease let me know if i am wrong somewhere in the above code.

 Hope that you can help me in this regard.

Thank you in advance,
Reddy.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to