Skip to content

A Java library to parse JVM bytecode, simulate the stack and extract as much information as possible

License

Notifications You must be signed in to change notification settings

sgodbillon/BytecodeParser

Repository files navigation

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

About

A Java library to parse JVM bytecode, simulate the stack and extract as much information as possible

Resources

License

Stars

Watchers

Forks

Packages

No packages published