我们可以在BCEL的堆栈操作数上推送自定义类型对象吗?

时间:2011-06-06 08:27:18

标签: java bytecode bcel

在BCEL中,我们可以在Operand Stack 上推送原始类型。但现在我想知道是否可以在BCEL中在堆栈上推送自定义类型对象

我正在提供一些代码,以便它可以解释问题上下文

    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 - 如果我使用createNew()方法并生成新对象,那么我将如何进行 填写其字段值? 2 - 或者如果我首先使用PUSH在堆栈上推送引擎类型Obj的所有字段值 那么我可以在记忆和构建中构建对象。然后把它推到Stack上。 这些是我能想到的解决方案。

但我不知道正确的解决方案 所以还需要帮助...

1 个答案:

答案 0 :(得分:1)

NEW创建一个新对象并在堆栈上放置对它的引用。它需要一个常量池中类引用的索引,可以使用ConstantPoolGen.addClass获得。例如:

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

这取自ASTProgram.java,这是BCEL示例的一部分。

还有其他方法可以在堆栈上获取对象引用。例如,ACONST_NULL在堆栈上推送空引用,ALOAD从本地变量加载引用,或GETSTATIC从类中获取静态字段。