How can I construct a org.objectweb.asm.tree.analysis.Frame
for each instruction in a method using only FrameNodes
and LocalVariableNodes
from the MethodNode
?
Context
While instrumenting some code I need all the locals and stack types for some of the original instructions.
Currently I get this by doing:
Analyzer analyzer = new Analyzer(new MyBasicInterpreter());
Frame[] frames = analyzer.analyze(ownerClass, methodNode);
This gives me a Frame
for each instruction in the methodNode
.
However, to have the exact types means properly implementing BasicInterpreter.merge
and that requires resolving the common super class for arbitrary types. This escalates quickly to having to know super classes and interfaces for a bunch of classes (i.e. having to read more information from the classloader).
So I am wondering if I can avoid using an Analyzer
and just use the original frame information to reconstruct the data I need.
The original classes are always jdk 1.8.0 classes and have frame information. And I really need to know the types in the stack.
You can’t avoid the analyzation entirely as the
StackMapTable
contains only information about branch targets and merge points. However, for the linear flow, theBasicInterpreter
already contains the necessary operations and you indeed don’t need to implement a merge operation if you extract the resulting information from theStackMapTable
.The only drawback is that you have to re-implement the construction of the method entry’s initial frame as that operation is buried inside the
Analyzer
and ASM doesn’t seem to provide a node for the implicit stack map frame that formally exists at the beginning of a method.Here is the full code: