Skip to content

Instantly share code, notes, and snippets.

@VijayKrishna
Created March 17, 2013 05:29
Show Gist options
  • Save VijayKrishna/5180270 to your computer and use it in GitHub Desktop.
Save VijayKrishna/5180270 to your computer and use it in GitHub Desktop.
Frames Stack information
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.AnalyzerAdapter;
public class Driver {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String classFile, className;
classFile = args[0];//full path of the class file
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);
ClassAdapter tagAd = new ClassAdapter(cw, className);
cr.accept(tagAd, 0);
}
}
class ClassAdapter extends ClassVisitor {
private String className;
public ClassAdapter(ClassVisitor cv, String className) {
super(Opcodes.ASM4, cv);
this.className = className;
}
@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 MethodAdapter(Opcodes.ASM4, className, access, name, desc, mv);
return mv;
}
}
class MethodAdapter extends AnalyzerAdapter {
public MethodAdapter(
int api,
String owner,
int access,
String name,
String desc,
MethodVisitor mv) {
super(Opcodes.ASM4, owner, access, name, desc, mv);
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
System.out.println("method name::" + name);
for(Object item : stack) {
System.out.println(item);
}
mv.visitMethodInsn(opcode, owner, name, desc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment