Normally you don’t need to deal with this as BCEL should take care of it. It seems to me that your mistake is to use a different instruction list than MethodGen
. So you’re modifying the underlying code but the offsets are not processed correctly.
Try to use
MethodGen newMethodGen = new MethodGen(method, classGen.getClassName(), poolGen);
InstructionList iList = newMethodGen.getInstructionList();
to ensure that the same list is used by your code and MethodGen
.