A Java library to parse JVM bytecode, simulate the stack and extract as much information as possible
Java CSS
Latest commit b24c5c5 Jan 6, 2014 @sgodbillon Merge pull request #3 from deanhiller/master
this file is backwards and incorrectly swaps top two elements.
Permalink
Failed to load latest commit information.
documentation/api
lib add some log May 4, 2011
src this file is backwards and incorrectly swaps top two elements. This f… Dec 4, 2013
.gitignore Add target/ directory to .gitignore Jun 23, 2013
LICENSE.txt License notice and README.txt file Apr 25, 2011
README.textile Update README.textile with new API and jar URLs. Jan 23, 2012
README.txt
build.xml
gpl-3.0.txt License notice and README.txt file Apr 25, 2011
lgpl-3.0.txt
log4j.properties.test.jar add some log May 4, 2011
pom.xml Added maven pom.xml Jun 23, 2013

README.textile

BytecodeParser

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.

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.

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.

BytecodeParser is released under the LGPL, version 3. It is actively developped. You can download the latest version (v0.3), explore its capabilities and extend it if you need to.

How to use it

See the project’s website for detailed examples and more information. You can also browse the API.

A small example

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));
        }
    }
}