Hi Everyone,
We have inserted a patch in the bytecode at some locations. After that we have
dumped it in a modified classfile. The insertions have been done properly, but
the new class file is not getting executed. It gives the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at
<classname>.main(classname.java:10)
The code doing this instrumentation is:
private static Method countMethod(Method m)
{
Code code = m.getCode();
int flags = m.getAccessFlags();
// Sanity check.
if (((flags & Constants.ACC_NATIVE) != 0) ||
((flags & Constants.ACC_ABSTRACT) != 0) || (code == null)){
return m;
}
InstructionList patch = new InstructionList();
patch.append(new GETSTATIC(cp.addFieldref("java.lang.System", "out",
"Ljava/io/PrintStream;")));
patch.append(new LDC(cp.addString("true branch")));
patch.append(new
INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream", "println",
"(Ljava/lang/String;)V")));
InstructionList patch1 = new InstructionList();
patch1.append(new GETSTATIC(cp.addFieldref("java.lang.System",
"out",
"Ljava/io/PrintStream;")));
patch1.append(new LDC(cp.addString("false branch")));
patch1.append(new
INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream", "println",
"(Ljava/lang/String;)V")));
MethodGen mg = new MethodGen(m, className, cp);
InstructionList il = mg.getInstructionList();
for (Iterator i = il.iterator(); i.hasNext(); ){
InstructionHandle ih = (InstructionHandle) i.next();
System.out.println(ih.getPosition());
Instruction in = ih.getInstruction();
// Insert the increment instructions right at the beginning of
the method
if (in instanceof BranchInstruction){
BranchInstruction b =(BranchInstruction)in;
if(b instanceof IfInstruction){
il.append(in, patch);
InstructionHandle ih2 = b.getTarget();
InstructionHandle ih1 = ih;
while(!ih1.equals(b.getTarget()))
{
Instruction in1 = ih1.getInstruction();
if(in1 instanceof GotoInstruction){
System.out.println(ih1.getPosition()+"goto");
//System.out.println(in1);
il.append(ih2.getInstruction(), patch1);
System.out.println(ih2.getInstruction());
}
ih1 = (InstructionHandle) i.next();
}
}
}
}
//System.out.println( "code stack size: " + code.getMaxStack() );
//System.out.println( "msg size: " + mg.getMaxStack() );
if (code.getMaxStack() < 2){
mg.setMaxStack(2);
}
mg.setInstructionList(il);
mg.setMaxStack();
mg.setMaxLocals();
return mg.getMethod();
}
Can anyone please tell where we are going wrong??
Thanks!!
Sincerely,
Archana
Connect with friends all over the world. Get Yahoo! India Messenger at
http://in.messenger.yahoo.com/?wm=n/