如何使用BCEL更改静态字段的值?

时间:2013-09-18 18:07:00

标签: java bytecode bytecode-manipulation bcel

我想使用BCEL重置静态字段,例如

private static final int myValue = 1;

myValue = 2。使用另一个字节码库(如ASM)是不可能的。

1 个答案:

答案 0 :(得分:1)

我的问题中的代码:Injecting code in an existing method using BCEL用于编辑静态数组。然而,我后来改为编辑局部变量。 编辑静态变量的代码是这样的:

InstructionList il = new InstructionList();
InstructionFactory f = new InstructionFactory(constantPoolGen);
il.append(f.createGetStatic("MyClassName","MyVariableName",Type.INT));
il.append(new PUSH(contantPoolGen, 2));
il.append(new ISTORE());

我使用的InstructionList被注入一个方法,所以我不确定这是否适合你..