Java 如何使用BCEL更改静态字段的值?
例如,我想使用BCEL重置静态字段Java 如何使用BCEL更改静态字段的值?,java,bytecode,bytecode-manipulation,bcel,Java,Bytecode,Bytecode Manipulation,Bcel,例如,我想使用BCEL重置静态字段 private static final int myValue = 1; 至myValue=2。使用另一个字节码库(如ASM)是不可能的。我问题中的代码:用于编辑静态数组。但后来我将其更改为编辑局部变量。 编辑静态变量的代码如下所示: InstructionList il = new InstructionList(); InstructionFactory f = new InstructionFactory(constantPoolGen); il.a
private static final int myValue = 1;
至myValue=2
。使用另一个字节码库(如ASM)是不可能的。我问题中的代码:用于编辑静态数组。但后来我将其更改为编辑局部变量。
编辑静态变量的代码如下所示:
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());
我使用的指令列表被注入了一个方法中,因此我不确定这是否适用于您。查找ConstantValue属性