問題描述
ASM 使用代碼查找偏移量 (ASM find offset with code)
I would like to find the offsets of different methods in test cases I work with. I can find where methods start and end, I look for opcodes RETURN and ARETURN (Im doing so in a class that extends a methodVisitor in the method visitInsn()), but I have not been able to find the offset where those happen in the bytecode. Any ideas?
Just in case: I am working with the core API of ASM, I am aware that the tree API could help me find what I need, but the tree API makes everything go slow and use memory and I can not afford any of those, so I work with the core API
參考解法
方法 1:
I found a way to get the offsets per instruction, it is not a common way but it works for me. ASM does not give you the offset per instruction because usually you wont need it, but you can still get it, I had to modify classes within ASM to make my idea work.
I modified the Label class and the ClassReader class, in the second one, there is a part that creates and adds labels to the labels array only in special occasions, so I just told it to add a label per instruction, then I just call the getOffset inside a visitInsn inside a methodVisitor and, the label will be resolved and have the offset.
(by lesolorzanov、lesolorzanov)