Java:使用BCEL为每个方法添加调试调用

时间:2011-01-28 20:50:09

标签: java methods bytecode bcel

我正在使用BCEL尝试将System.out.println()调用添加到每个方法的第一行(init和clinit方法除外),以查看调用的方法以及何时调用

这是我的代码atm(带有一些伪):

    Instruction ins = null;
    f (first instruction is ALOAD_0) {
       ins = get next instruction
    } else {
       ins = this instruction;
    }

    list.insert(ins, new GETSTATIC(cgen.getConstantPool().addFieldref("java/lang/System", "out", "Ljava/io/PrintStream;")));
    list.insert(ins, new LDC(cgen.getConstantPool().addUtf8("debug message")));
    list.insert(ins, new INVOKEVIRTUAL(cgen.getConstantPool().addMethodref("java/io/PrintStream", "println", "(Ljava/lang/String;)V")));

编辑的类在字节码中看起来很好,但由于某种原因,该类在此之后将无效。有什么我做错了吗?

2 个答案:

答案 0 :(得分:1)

您正在推送两个参数,对于某些方法,这可能大于该方法的最大堆栈大小。您还需要调整方法的最大堆栈大小。

如果查看javap输出,您将看到

Code:
    Stack=4, Locals=8, Args_size=3

对于Stack为< 2你需要把它碰到2。

答案 1 :(得分:1)

问题解决了,我使用.addUtf8代替.addString