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 Marco Bessi <be...@gmail.com> on 2010/09/01 16:29:54 UTC
Problems adding IF into a bytecode class
Hi to all,
I need to insert the followings bytecode instructions inside a method of a
.class file.
if(a == null){ //a is an object
Classe.staticMethod();
}
long x = System.nanoTime();
I try two way but I have always an Exception.!
TRY 1: (I create a InstructionList (ilFirst) where I insert the followings
instructions.)
InstructionList ilFirst = new InstructionList();
ilFirst.append(new ALOAD(a));
Instruction invokeInterIF = factory.createInvoke("memory.Classe",
"staticMethod", Type.VOID, Type.NO_ARGS, Constants.INVOKESTATIC);
ilFirst.append(invokeInterIF);
InstructionHandle ihEndIF =
ilFirst.append(factory.createInvoke("java.lang.System", "nanoTime",
Type.LONG, Type.NO_ARGS, Constants.INVOKESTATIC));
ilFirst.insert(invokeInterIF, new IFNULL(ihEndIF));
[continue]
TRY 2: (I create a InstructionList (ilFirst) where I insert the followings
instructions. I create another InstructionList (ilThanIf ) to insert the
instructions inside the IF)
InstructionList ilFirst = new InstructionList();
ilFirst.append(new ALOAD(a));
InstructionList ilThanIf = new InstructionList();
ilThanIf.insert(factory.createInvoke("memory.Classe", "staticMethod",
Type.VOID, Type.NO_ARGS, Constants.INVOKESTATIC));
ilFirst.append(InstructionFactory.createBranchInstruction((short) 199,
ilThanIf.getStart())); //IFNONNULL
ilFirst.append(factory.createInvoke("java.lang.System", "nanoTime",
Type.LONG, Type.NO_ARGS, Constants.INVOKESTATIC));
[continue]
The Exception is:
- TRY1:
Exception in thread "AWT-EventQueue-0"
org.apache.bcel.generic.ClassGenException: Assigning branch instruction
ifnull[198](3) -> invokestatic 110 to plain handle
-TRY 2:
Exception in thread "AWT-EventQueue-0"
org.apache.bcel.generic.ClassGenException: Invalid branch target position
offset for ifnonnull[199](3):-1: -1: invokestatic[184](3) 89
There are someone that know I can solve this problem? I haven't found
anything in the web about the InstructionHandle. I suppose that the problem
is there because the exceptions are on the IFNULL() or IFNONNULL() that
wants a InstructionHandle for parameter!
Thanks.
Marco.