我正在使用 asm 在每个执行的函数中插入一个回调函数。我如何打印参数值?
我正在使用 MethodAdapter.visitCode 将我的函数注入到每个运行的函数中。
我想将函数参数插入一个数组并将我的回调函数发送到这个数组并将参数返回到堆栈,以便函数可以继续使用它们
以下代码将方法参数插入到数组中,并作为 Object 数组发送到回调函数。我无法将参数返回到原始函数
@Override public void visitCode()
{
int paramLength = paramTypes.length;
System.out.println(className + "." + methodName + ": paramLength = " + paramLength);
// Create array with length equal to number of parameters
mv.visitIntInsn(Opcodes.BIPUSH, paramLength);
mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
mv.visitVarInsn(Opcodes.ASTORE, paramLength);
// Fill the created array with method parameters
int i = 0;
for (Type tp : paramTypes)
{
System.out.println("tp.getClassName() = " + tp.getClassName());
mv.visitVarInsn(Opcodes.ALOAD, paramLength);
mv.visitIntInsn(Opcodes.BIPUSH, i);
if (tp.equals(Type.BOOLEAN_TYPE))
{
mv.visitVarInsn(Opcodes.ILOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
}
else if (tp.equals(Type.BYTE_TYPE))
{
mv.visitVarInsn(Opcodes.ILOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
}
else if (tp.equals(Type.CHAR_TYPE))
{
mv.visitVarInsn(Opcodes.ILOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
}
else if (tp.equals(Type.SHORT_TYPE))
{
mv.visitVarInsn(Opcodes.ILOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
}
else if (tp.equals(Type.INT_TYPE))
{
mv.visitVarInsn(Opcodes.ILOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
}
else if (tp.equals(Type.LONG_TYPE))
{
mv.visitVarInsn(Opcodes.LLOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
i++;
}
else if (tp.equals(Type.FLOAT_TYPE))
{
mv.visitVarInsn(Opcodes.FLOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
}
else if (tp.equals(Type.DOUBLE_TYPE))
{
mv.visitVarInsn(Opcodes.DLOAD, i);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
i++;
}
else
mv.visitVarInsn(Opcodes.ALOAD, i);
mv.visitInsn(Opcodes.AASTORE);
i++;
}
//System.out.println("end for");
// Load class name and method name
this.visitLdcInsn(className);
this.visitLdcInsn(methodName);
// Load the array of parameters that we created
this.visitVarInsn(Opcodes.ALOAD, i);
this.visitMethodInsn(Opcodes.INVOKESTATIC, "callbackpackage/CallBack", "callbackfunc", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V");
super.visitCode();
}