You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Ian Hunter <ih...@hotmail.com> on 2002/08/16 17:40:48 UTC

Simple Example

Hi BCeliers...

The following is a very simple example using 5.0 to append a static method 
call to each method using a ClassLoader. I've only just started looking at 
BCel, so forgive me if its completely stupid!

When it executes te instrumented method I get illegal ConstantPool index. 
Can anybody indicate what I've missed.

Also, can anybody indicate how I get a version of 5.1 without strapping up 
CVS?

Many Thanks
Ian Hunter

========================================================
import org.apache.bcel.Constants;
import org.apache.bcel.util.ClassLoader;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;

public class MyClassLoader extends org.apache.bcel.util.ClassLoader {

  protected JavaClass modifyClass(JavaClass clazz) {

    ConstantPoolGen cp =
      new ConstantPoolGen(clazz.getConstantPool());

    Method[] methods = clazz.getMethods();

    for(int i = 0; i < methods.length; i++) {
      MethodGen mg =
        new MethodGen(methods[i],clazz.getClassName(),cp);
      methods[i] = addcall(mg, cp);
    }

    return(clazz);
  }

  private Method addcall(MethodGen mg, ConstantPoolGen cpg) {

    InstructionList    il      = mg.getInstructionList();
    InstructionFactory factory = new InstructionFactory(cpg);

    Type[] argTypes = new Type[] {};
    InvokeInstruction is =
      factory.createInvoke("System",
			   "currentTimeMillis",
			   Type.LONG,
			   argTypes,
			   Constants.INVOKESTATIC);

    il.append(is);

    Method result = mg.getMethod();

    il.dispose();

    return(result);
  }

}



_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>