I am manipulating a .class file. I am using the InstrutionHandle package to get the instructions one at a time. I have the byte offset of the instruction via getPosition() method
, can i get the source line number from the byte offset?
I am manipulating a .class file. I am using the InstrutionHandle package to get
Share
That depends on whether the classfile is compiled with debugging information. Usually, the compiler will insert a
LineNumberTableattribute which gives the original source line numbers corresponding to each range of bytecode. However, theLineNumberTableattribute is just metadata, so the author could put anything they want in there with minor constraints or just omit it entirely. (Typically done by compiling with-g: noneor by running an obfuscator on it)Anyway, the format of the attribute is number of entires (2 bytes) followed by (start pc, line number) pairs (both 2 bytes). You can also have multiple
LineNumberTableattributes. Of course if you’re using a library, it will probably decode these for you already.