2

在 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 的所有字段值推送到堆栈上,那么我可以了解如何在内存上构造对象然后将其推送到堆栈上。这些是我能想到的一些解决方案。

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

4

1 回答 1

1

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

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

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

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

于 2011-06-06T08:35:49.540 回答