1) Wouldn't your statement
il.append(is);
Append your instruction AFTER the return instruction?
2) You might consider using the methods in ClassGen particularily
classGen.replaceMethod()...
and (after you are done)
classGen.getJavaClass()
3) I don't know if it is necessary in your case but once you move your
new instruction to where it should be (not after the return statement)
then you might need
il.setPositions();
and
mg.setMaxStack();
-----Original Message-----
From: Ian Hunter [mailto:[EMAIL PROTECTED]]
Sent: Friday, August 16, 2002 9:41 AM
To: [EMAIL PROTECTED]
Subject: 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:
<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]>