ASM 参数编号
ASM parameter numbering
假设我有一个假人class
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
加载?
这是因为 Java Virtual Machine Specification chapter 2.6.1 说
A value of type long or type double occupies two consecutive local
variables.
和
The Java Virtual Machine uses local variables to pass parameters on method invocation.
因此,您的示例的参数是:
0: `this`
1: v:J
3: f:Ljava/lang/String
假设我有一个假人class
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
加载?
这是因为 Java Virtual Machine Specification chapter 2.6.1 说
A value of type long or type double occupies two consecutive local variables.
和
The Java Virtual Machine uses local variables to pass parameters on method invocation.
因此,您的示例的参数是:
0: `this`
1: v:J
3: f:Ljava/lang/String