ASM参数编号

时间:2017-11-23 12:36:02

标签: java java-bytecode-asm bytecode-manipulation jvm-bytecode

假设我有一个虚拟课

public class B {
    public Object run(long v, String f){
        if(v == 2) {
            if(f.equals("x")) {
                return new Object();
            }
        }
        return null;
    }
}

asmifier将为run方法创建以下内容:

{
mv = cw.visitMethod(ACC_PUBLIC, "run", "(JLjava/lang/String;)Ljava/lang/Object;", null, null);
mv.visitCode();
mv.visitVarInsn(LLOAD, 1);
mv.visitLdcInsn(new Long(2L));
mv.visitInsn(LCMP);
Label l0 = new Label();
mv.visitJumpInsn(IFNE, l0);
mv.visitVarInsn(ALOAD, 3);
mv.visitLdcInsn("x");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
mv.visitJumpInsn(IFEQ, l0);
mv.visitTypeInsn(NEW, "java/lang/Object");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(ARETURN);
mv.visitLabel(l0);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
mv.visitMaxs(4, 4);
mv.visitEnd();
}

我不明白这一点:

mv.visitVarInsn(ALOAD, 3);

我看到它的方式,run只有三个参数:

0: `this`
1: v:J
2: f:Ljava/lang/String

那么为什么f现在使用3而不是2加载?

1 个答案:

答案 0 :(得分:1)

这是因为Java Virtual Machine Specification chapter 2.6.1

  

long或double类型的值占用两个连续的local   变量

  

Java虚拟机使用局部变量在方法调用上传递参数。

因此,您的示例的参数是:

0: `this`
1: v:J
3: f:Ljava/lang/String