Java BCEL creating method with sipush?

Posted by user1446924 on Stack Overflow See other posts from Stack Overflow or by user1446924
Published on 2012-06-10T04:34:59Z Indexed on 2012/06/10 4:40 UTC
Read the original article Hit count: 189

Filed under:

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.

© Stack Overflow or respective owner

Related posts about java