2011-01-28 66 views
1

我正在與BCEL一起嘗試將System.out.println()調用添加到每個方法的第一行(init和clinit方法除外),以查看調用哪些方法以及當Java:使用BCEL向每個方法添加調試調用

這是我的代碼的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"))); 

編輯的類的字節碼,但由於某些原因,類將不會在這之後的工作看起來不錯。有什麼我做錯了嗎?

回答

1

您推送兩個參數,對於某些方法,這可能會大於該方法的最大堆棧大小。您還需要調整該方法的最大堆棧大小。

如果你看一下javap的輸出,你會看到

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

的方法,其中協議棧是< 2,你需要將其撞至2

+0

爲此,只需在代碼後面添加對MethodGen類的方法setMaxStack()的調用即可。 – 2011-01-28 21:56:31

1

問題解決了,我用.addUtf8代替.addString