java使用ASM字节码工具调用类方法和实例方法,asm字节码,调用静态方法://int
分享于 点击 1610 次 点评:46
java使用ASM字节码工具调用类方法和实例方法,asm字节码,调用静态方法://int
调用静态方法:
//int c=sample.InvokeMethod.add(2,3);mv.visitInsn(ICONST_2); //arg1mv.visitInsn(ICONST_3); //arg2mv.visitMethodInsn(INVOKESTATIC, "sample/InvokeMethod", //class name "add", //static method name "(II)I"); //return type is integer
调用实例方法:
//obj.cal(4,5);mv.visitVarInsn(ALOAD, 2); //load object from stackmv.visitInsn(ICONST_4); //arg1mv.visitInsn(ICONST_5); //arg2mv.visitMethodInsn(INVOKEVIRTUAL, "sample/InvokeMethod", "cal", "(II)V");
实例化对象,即调用构造函数:
//sample.InvokeMethod obj=new sample.InvokeMethod(6,7);mv.visitTypeInsn(NEW, "sample/InvokeMethod");mv.visitInsn(DUP);mv.visitIntInsn(BIPUSH, 6);mv.visitIntInsn(BIPUSH, 7);mv.visitMethodInsn(INVOKESPECIAL, "sample/InvokeMethod", "<init>", "()V");mv.visitVarInsn(ASTORE, 4);
用户点评