sgodbillon / BytecodeParser

A Java library to parse JVM bytecode, simulate the stack and extract as much information as possible
http://sgodbillon.github.com/BytecodeParser/
Other
41 stars 10 forks source link

h1. BytecodeParser

p. BytecodeParser is a java library that can help you statically to parse java bytecode by extracting as much information as possible. It is based upon "javassist":http://www.csg.is.titech.ac.jp/~chiba/javassist/.

p. It can also statically analyze each method's frame, for particular purposes like retrieving the names of the local variable names given as parameters of a method call (its original purpose), check if all the frames are reachable, etc.

p. BytecodeParser simulates the stack operations made by the opcodes in the CodeAttribute. It can give you the state of the stack before and after the frame is run.

p. BytecodeParser is released under the "LGPL, version 3":http://www.gnu.org/licenses/lgpl.html. It is actively developped. You can download the latest version ("v0.3":http://sgodbillon.github.com/BytecodeParser/releases/0.3/bytecodeparser-0.3.jar), explore its capabilities and extend it if you need to.

h2. How to use it

p. See the "project's website":http://sgodbillon.github.com/BytecodeParser/ for detailed examples and more information. You can also "browse the API":http://sgodbillon.github.com/BytecodeParser/releases/0.3/api/index.html.

h2. A small example

bc. ClassPool cp = new ClassPool(); CtClass ctClass = cp.getCtClass("org.myapp.MyClass"); for(CtMethod method: ctClass.getMethods()) { StackAnalyzer stackAnalyzer = new StackAnalyzer(method); for(Frame frame: stackAnalyzer.analyze()) { // you can get the state of the stack before or after this frame with frame.stackBefore and frame.stackAfter System.out.println(frame.stackBefore + " => " + frame.stackAfter); // you can also get some extended information about this frame with frame.decodedOp if(frame.decodedOp instanceof DecodedMethodInvocationOp) { DecodedMethodInvocationOp dmio = (DecodedMethodInvocationOp) frame.decodedOp; MethodParams methodParams = DecodedMethodInvocationOp.resolveParameters(frame); MethodParam[] params = methodParams.merge(); System.out.println("method '" + dmio.getName() + "' has been called with the following arguments: " + java.util.Arrays.toString(params)); } } }