我正在嘗試將integer變數添加x到使用 java ASMint命名的靜態變數中count。
經過大量搜索,我找到了一種將固定整數添加到靜態 int 變數的方法
InsnList numCounting = new InsnList();
// insert count
numCounting.add(new FieldInsnNode(Opcodes.GETSTATIC, classNode_c.name, "count", "I"));
numCounting.add(new InsnNode(Opcodes.ICONST_1));
numCounting.add(new InsnNode(Opcodes.IADD));
numCounting.add(new FieldInsnNode(Opcodes.PUTSTATIC, classNode_c.name, "count", "I"));
mn.instructions.insert(node, numCounting);
我如何概括這一點,以便我可以添加一個任意int到count?謝謝
uj5u.com熱心網友回復:
我現在無法真正測驗此代碼,但這應該可以作業
InsnList numCounting = new InsnList();
// insert count
numCounting.add(new FieldInsnNode(Opcodes.GETSTATIC, classNode_c.name, "count", "I"));
numCounting.add(new LdcInsnNode( x )); // Load a constant onto the stack, asm will put that constant in the constant pool for you
numCounting.add(new InsnNode(Opcodes.IADD));
numCounting.add(new FieldInsnNode(Opcodes.PUTSTATIC, classNode_c.name, "count", "I"));
mn.instructions.insert(node, numCounting);
(我通常使用 asm 的方法是用純 java 撰寫我想用 asm 生成的方法,編譯它并用 javap 查看生成的位元組碼。這通常是一個很好的指示使用什么操作碼。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394217.html
標籤:爪哇 字节码 java-字节码-asm
上一篇:按下退出按鈕時的事件:JOptionPane.showInputDialog
下一篇:java.lang.NoClassDefFoundError:groovy/lang/Script但類已加載...?
