首页 文章

ASM字节码方法参数值

提问于
浏览
1

如何使用ASM字节代码查看或访问方法参数值或对象?

2 回答

  • 1

    考虑到方法参数类型,您可以执行以下操作:

    int off = (access | Opcodes.ACC_STATIC) == 0 ? 0 : 1;
    int opcode = Type.getArgumentTypes(desc)[param + off].getOpcode(Opcodes.IALOAD);
    mv.visitVarIns(opcode, param);
    ...
    

    其中 param 是方法参数编号, accessdesc 是从ClassVisitor.html#visitMethod的相应参数获得的值 .

  • 1

    方法参数是前几个局部变量 . 要访问第一个arg,字节码助记符看起来像 aload_0iload_0lload_0 等,具体取决于参数's type. For arguments past the fourth, you' d说 aload 4 等 .

    注意,实例方法的第一个参数是对 this 的引用 . 所以第一个参数将是本地#1,你会得到它像 aload_1 等 .

    但是你'd generate bytecode with the ASM stuff... do that. It looks like you' d说出类似于 mv.visitVarInsn(ALOAD, 0); 的东西,其中 mv 是你的MethodVisitor . 0 将替换为局部变量索引 .

相关问题