jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marco Bessi <bessima...@gmail.com>
Subject Problems adding IF into a bytecode class
Date Wed, 01 Sep 2010 14:29:54 GMT
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.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message