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.
See the project’s website for detailed examples and more information. You can also browse the API.
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));
}
}
}