Skip to content

Instantly share code, notes, and snippets.

@hengyunabc
Created January 16, 2018 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengyunabc/92f3600705e6e5ce79290bbb42e4a0a7 to your computer and use it in GitHub Desktop.
Save hengyunabc/92f3600705e6e5ce79290bbb42e4a0a7 to your computer and use it in GitHub Desktop.
java asm bytecode. jsr instruction sample.
import java.util.*;
import org.objectweb.asm.*;
public class CDump implements Opcodes {
public static byte[] dump() throws Exception {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(0, "m", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
mv.visitTryCatchBlock(l0, l1, l1, null);
Label l2 = new Label();
Label l3 = new Label();
mv.visitTryCatchBlock(l2, l3, l1, null);
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ISTORE, 1);
mv.visitLabel(l0);
mv.visitIincInsn(1, 1);
mv.visitJumpInsn(GOTO, l2);
mv.visitLabel(l1);
mv.visitVarInsn(ASTORE, 3);
Label l4 = new Label();
mv.visitJumpInsn(JSR, l4);
mv.visitVarInsn(ALOAD, 3);
mv.visitInsn(ATHROW);
mv.visitLabel(l4);
mv.visitVarInsn(ASTORE, 2);
mv.visitIincInsn(1, -1);
mv.visitVarInsn(RET, 2);
mv.visitLabel(l2);
mv.visitJumpInsn(JSR, l4);
mv.visitLabel(l3);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 4);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment