Search code examples
javabytecodejava-bytecode-asm

ASM helper class to cache all instructions


So I am trying to implement a stateful transformation of a method code, it starts from my MethodVisitor sense a special instruction A, and then all the following instructions needs to be cached until the visitor encountered a special invocation instruction to a method that returns a certain result, then I need to inject some code before instruction A, and then output all instructions cached in the instruction cache. Is there a help class help me listening all instruction visits and can apply them once needed?


Solution

  • In case when you are using only core API, you must implement caching yourself (AFAIK). For your case is better to use Tree API (see in manual), where you have access to list with all instruction from given method. See http://www.geekyarticles.com/2011/10/manipulating-java-class-files-with-asm_13.html for example how to use Tree API.