Question

Presumably the entire method body gets processed, and converted in the InsnList which contains a bunch of AbstractInsnNodes, along with some non-bytecode nodes (like FrameNode, LineNumberNode, etc.). The XXXNode classes javadoc generally contains some useful information (e.g. which opcodes map to this XXXNode) but the mapping doesn't seem to be total, so I'm not entirely sure about what happens in all cases:

  • What happens to the WIDE bytecode? Does it get automatically folded into the next instruction? Presumably it does, but I can't find anything which explicitly states that
  • Do all the Ldc instructions (LDC, LDC_2, LDC_2W) all map to LdcInsnNodes? Presumably they do, but the documentation doesn't say

In general, is there any convenient mapping of java bytecode -> ASM class? I'm doing some processing on java bytecode sequences (i.e. method bodies) and would like to check that all the bytecodes that I may receive (all ~200 of them or so) map to ASM data structures that I am properly handling.

Was it helpful?

Solution

Yes, you are right for the LDC instructions and WIDE.

ASM does not map the opcodes in Java bytecode to the API as mentioned in the desciption of the org.objectweb.asm.Opcodes Interface's Java-doc (http://asm.ow2.org/asm40/javadoc/user/index.html)

public interface Opcodes

Defines the JVM opcodes, access flags and array type codes. This interface does not define all the JVM opcodes because some opcodes are automatically handled. For example, the xLOAD and xSTORE opcodes are automatically replaced by xLOAD_n and xSTORE_n opcodes when possible. The xLOAD_n and xSTORE_n opcodes are therefore not defined in this interface. Likewise for LDC, automatically replaced by LDC_W or LDC2_W when necessary, WIDE, GOTO_W and JSR_W.

And this translates to the other APIs in ASM.

This can also be confirmed here:

First of all please note that ASM does not provide exact one-to-one mappings from original bytecode ... - http://osdir.com/ml/java.objectweb.asm/2008-03/msg00031.html

For the second part of your question you could try using the ASMifer as mentioned in the FAQ: http://asm.ow2.org/doc/faq.html#Q10. It will basically list out the Java instructions you will have to call using ASM to create a given sequence of ByteCode instructions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top