| Refresh | Home EGTry.com

generate instruction with methodvisitor


put a constant onto the stack
 mv.visitLdcInsn("Hello");

load an object from the local array

 mv.visitVarInsn(ALOAD, 0);

store an object from localvaraible array onto stack

 mv.visitVarInsn(ILOAD, 6); //6 the index of the localvariable

 mv.visitVarInsn(LLOAD, 100);

get static field from a class

 mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");

invoke a non-static method. in stack: object, arguments

 mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");

invoke a constructor. stack: argument

 mv.visitMethodInsn(INVOKESPECIAL, "package1/package2/ClassName", "<init>", "(Ljava/lang/String;)V");

invoke a static method. in stack: arguments

 mv.visitMethodInsn(INVOKESTATIC, "package/ClassName", "AStaticMethod", "(Ljava/lang/String;)V");

duplicate the top value of stack

 mv.visitInsn(DUP);

 mv.visitInsn(DUP2);

return from the method

 mv.visitInsn(RETURN); //return no value