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");

  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.

 Currently carrying a test case:: adding a method dynamically to already
existing class.. The code is as follows


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

public class addMethodBCEL{

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 addmeth = addMethod(clazz,cp,factory);

                        
                    System.out.println(addmeth.getName());
                                    
                 try {
                       clazz.setConstantPool(cp.getFinalConstantPool());

                       clazz.dump(clazz.getClassName() + ".class");
                    }catch(Exception e){System.out.println(e);} 
        
                    Method[] meths = clazz.getMethods();

                    for(int i = 0; i< meths.length; i++ ){
                    System.out.println(meths[i].getName());
                }
                
        }


        public static Method addMethod(JavaClass clazz, ConstantPoolGen
cp, InstructionFactory factory){
                        InstructionList il1 = new InstructionList();
                        MethodGen mg = new
MethodGen(Constants.ACC_STATIC | Constants.ACC_PUBLIC, Type.INT,
Type.NO_ARGS, null,"getMarks",clazz.getClassName(), il1, cp) ;
                        il1.append(new PUSH(cp,80));
                        il1.append(factory.createReturn(Type.INT));
                        mg.setMaxStack();
                        return mg.getMethod();
                }
}


 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.

 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