Hi Andrew

  -> I did not use setMethodAt() method , the line
  
  clazz.setConstantPool(cp.getFinalConstantPool()); 

  solved the problem.

  -> Regarding classgen.addMethod(); i used this method. It worked and
added the method dynamically to class, but i want to update my class
file too..
     So i used 
        
cg.getJavaClass().setConstantPool(cg.getConstantPool().getFinalConstantP
ool()); 
                    cg.getJavaClass().dump(clazz.getClassName() +
".class");

   and the timestamp shows it is updated, but when i tried to execute
the updated class..it raised me exception[1]..you can see my source code
at [2]

I am getting somewhere wrong in setting the constantpool. But i could
not figure it out as the docs of methods are not up to the mark (
ofcourse it is opensource)

  -> Do u have any idea of where I can find good examples using BCEL? If
so please let me know.

  -> A small example on this try--catch scenario would be of great help.
 
 Sorry for big list of questions, Hope you can help me...and thanks for
the help so far

Thank you
Reddy

[1]  

Exception in thread "main" java.lang.ClassFormatError: HelloBCEL
(Illegal consta
nt pool index)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)


[2]


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

                        
                    cg.addMethod(addmeth);
                                    
                    Method[] meths = cg.getJavaClass().getMethods();
        
                    for(int i = 0; i< meths.length; i++ ){
                    System.out.println(meths[i].getName());
                }

         try {
        
cg.getJavaClass().setConstantPool(cg.getConstantPool().getFinalConstantP
ool()); 
                    cg.getJavaClass().dump(clazz.getClassName() +
".class");
                    }catch(Exception e){System.out.println(e);} 

                
        }


        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, new String[] },"getMarks",clazz.getClassName(), il1, cp) ;
                        il1.append(new PUSH(cp,10));
                        il1.append(factory.createReturn(Type.INT));
                        mg.setMaxStack();
                        return mg.getMethod();
                }
}






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


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