Java BCEL creating method with sipush?
- by user1446924
Well, I'm trying to recreate this method :
public int getBaseX() {
return this.x + 10000;
}
and it returns these instructions
0 aload_0
1 getfield #18 <TestClass.x>
4 sipush 10000
7 iadd
8 ireturn
Now I'm having trouble withe the 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());
that's what I have tried, but failed.