2

In BCEL we can push primitive types on Operand Stack. BUT now I want to know if it is possible to push a Custom Type Object on Stack in BCEL?

I am giving some code so that it can explain the Problem Context

    class Automobile {

public void getEngine(int carNo,EngineClass engineClassObj){

System.out.println("EngineNo="+engineClassObj.sisNo);
}
}

Now when i load "Automobile" class in memory.

ilist = new InstructionList();
ilist.append(InstructionConstants.ALOAD_0);
ilist.append(new PUSH(pgen,345));

////Hear Now i have to also push the Object on Stack

ilist.append(ifact.createInvoke(_invoking_ClassName,_invoking_MethodName, Type.INT,*
new Type[] { Type.INT,Type.OBJECT }, Constants.INVOKEVIRTUAL));

ilist.append(InstructionConstants.IRETURN);

1-if i use createNew() method and generate new object then how i am going to fill its fields value? 2-or if i firstly push all fields values of Engine type Obj on Stack using PUSH then i can some how construct object on memory & then push it on Stack. these are some solution i can think of.

But i don't know the right solution So Still need help ...

zaree
  • 641
  • 2
  • 6
  • 15

1 Answers1

1

NEW creates a new object and places a reference to it on stack. It needs an index to the class reference in the constant pool which can be obtained with ConstantPoolGen.addClass. For example:

il = new InstructionList();
il.append(new NEW(cp.addClass("java.lang.StringBuffer")));

This is taken from ASTProgram.java which is a part of BCEL examples.

There are other ways to get an object reference on stack as well. For example, ACONST_NULL pushes a null reference on stack, ALOAD loads a reference from local variable or GETSTATIC fetches static field from class.

vitaut
  • 49,672
  • 25
  • 199
  • 336
  • when i use ALoad _0 then it will gave this exception::Unable to pop operand off an empty stack.This will point out that we need to push some thing on Stack. in case of load it what do u say abt it! – zaree Jun 06 '11 at 10:13
  • @zaree: Reference to what object do you want to put on stack? If it is a null reference use ACONST_NULL. Otherwise it should come from somewhere, for example, from a local variable in which case use ALOAD with an index of local variable. Or you can create an object using NEW. – vitaut Jun 06 '11 at 10:27
  • can u elaborate your ans within any example – zaree Jun 06 '11 at 10:55
  • @zaree: Sure. I've added an example of using NEW. – vitaut Jun 06 '11 at 11:05