Last active
September 8, 2023 19:53
-
-
Save VijayKrishna/1ca807c952187a7d8c4d to your computer and use it in GitHub Desktop.
Revisions
-
VijayKrishna revised this gist
May 26, 2014 . 1 changed file with 16 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,6 +11,12 @@ import org.objectweb.asm.Opcodes; import org.objectweb.asm.commons.AdviceAdapter; /** * Class visitor that adapts a java class to insert code before * the return instructions in the methods of the class. * @author vijay * */ public class ReturnAdapter extends ClassVisitor { private String className; @@ -37,13 +43,21 @@ public static void main(String[] args) throws IOException { File inFile = new File(classFile); FileInputStream in = new FileInputStream(inFile); // adapting the class. ClassReader cr = new ClassReader(in); ClassWriter cw = new ClassWriter(ClassReader.EXPAND_FRAMES); ReturnAdapter returnAdapter = new ReturnAdapter(cw, className); cr.accept(returnAdapter, 0); } } /** * Method Visitor that inserts code right before its return instruction(s), * using the onMethodExit(int opcode) method of the AdviceAdapter class, * from ASM(.ow2.org). * @author vijay * */ class MethodReturnAdapter extends AdviceAdapter { public MethodReturnAdapter( int api, @@ -67,6 +81,7 @@ public MethodReturnAdapter( protected void onMethodExit(int opcode) { if(opcode != Opcodes.ATHROW) { mv.visitVarInsn(Opcodes.ALOAD, 42); // and/or any other visit instructions. } } } -
VijayKrishna created this gist
May 26, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,72 @@ package self.vpalepu.stackoverflow; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import org.objectweb.asm.commons.AdviceAdapter; public class ReturnAdapter extends ClassVisitor { private String className; public ReturnAdapter(ClassVisitor cv, String className) { super(Opcodes.ASM4, cv); } @Override public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv; mv = cv.visitMethod(access, name, desc, signature, exceptions); mv = new MethodReturnAdapter(Opcodes.ASM4, className, access, name, desc, mv); return mv; } public static void main(String[] args) throws IOException { String classFile = args[0];//path of the class file String className = args[1];//name of the class File inFile = new File(classFile); FileInputStream in = new FileInputStream(inFile); ClassReader cr = new ClassReader(in); ClassWriter cw = new ClassWriter(ClassReader.EXPAND_FRAMES); ReturnAdapter returnAdapter = new ReturnAdapter(cw, className); cr.accept(returnAdapter, 0); } } class MethodReturnAdapter extends AdviceAdapter { public MethodReturnAdapter( int api, String owner, int access, String name, String desc, MethodVisitor mv) { super(Opcodes.ASM4, mv, access, name, desc); } public MethodReturnAdapter( MethodVisitor mv, int access, String name, String desc) { super(Opcodes.ASM4, mv, access, name, desc); } @Override protected void onMethodExit(int opcode) { if(opcode != Opcodes.ATHROW) { mv.visitVarInsn(Opcodes.ALOAD, 42); } } }