gpt4 book ai didi

java - BCEL 更新异常(exception)表

转载 作者:行者123 更新时间:2023-12-01 13:58:44 25 4
gpt4 key购买 nike

我想更改使用 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。知道如何处理这个问题吗?

最佳答案

通常您不需要处理这个问题,因为 BCEL 应该会处理它。在我看来,您的错误是使用了与 MethodGen 不同的指令列表。因此,您正在修改底层代码,但偏移量未正确处理。

尝试使用

MethodGen newMethodGen = new MethodGen(method, classGen.getClassName(), poolGen);
InstructionList iList = newMethodGen.getInstructionList();

确保您的代码和 MethodGen 使用相同的列表。

关于java - BCEL 更新异常(exception)表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19476483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com