好吧,我正在尝试重新创建此方法:
public int getBaseX() {
return this.x + 10000;
}
它返回这些指令
0 aload_0
1 getfield #18 <TestClass.x>
4 sipush 10000
7 iadd
8 ireturn
现在我在使用 sipush 时遇到了麻烦,iadd。
ClassGen injCG = cg;
ConstantPoolGen cpg = injCG.getConstantPool();
InstructionList il = new InstructionList();
MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, f.getType(), Type.NO_ARGS, null,
name, injCG.getClassName(), il, cpg);
InstructionFactory instrF = new InstructionFactory(injCG, cpg);
if (!f.isStatic()) il.append(new org.apache.bcel.generic.ALOAD(0));
il.append(instrF.createFieldAccess(cg.getClassName(), f.getName(), f.getType(),
(f.isStatic() ? Constants.GETSTATIC : Constants.GETFIELD)));
il.append(InstructionFactory.createPop(1000));
il.append(InstructionFactory.createReturn(f.getType()));
//il.append(InstructionFactory.createReturn(f.getType()), InstructionFactory.createDup(1000));
mg.setMaxLocals();
mg.setMaxStack();
injCG.addMethod(mg.getMethod());
这就是我尝试过的,但失败了。