我想用BCEL改变一个方法。但我不知道如何更新异常表。以下是简化的代码:
ConstantPoolGen poolGen = classGen.getConstantPool();
InstructionList iList = new InstructionList(method.getCode().getCode());
MethodGen newMethodGen = new MethodGen(method, classGen.getClassName(), poolGen);
for (InstructionHandle handle : iList.getInstructionHandles().clone()) {
if (I_WANT_TO_WRAP_IT(handle)) {
iList.insert(handle, MAKE_WRAPPER(handle));
iList.delete(handle);
}
}
classGen.removeMethod(method);
newMethodGen.setMaxStack();
newMethodGen.setMaxLocals();
classGen.addMethod(newMethodGen.getMethod());在此之后,字节码被正确修改,但异常表与导致ClassFormatError的异常表保持不变,因为异常表指向不存在的PC。知道怎么处理这事吗?
发布于 2013-10-21 16:25:59
通常,您不需要处理这一点,因为BCEL应该处理它。在我看来,你的错误是使用与MethodGen不同的指令列表。因此,您正在修改底层代码,但是偏移没有得到正确处理。
试着使用
MethodGen newMethodGen = new MethodGen(method, classGen.getClassName(), poolGen);
InstructionList iList = newMethodGen.getInstructionList();以确保代码和MethodGen使用相同的列表。
https://stackoverflow.com/questions/19476483
复制相似问题