Reputation: 1091
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?
Upvotes: 2
Views: 1487
Reputation: 39451
That depends on whether the classfile is compiled with debugging information. Usually, the compiler will insert a LineNumberTable
attribute which gives the original source line numbers corresponding to each range of bytecode. However, the LineNumberTable
attribute 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: none
or 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 LineNumberTable
attributes. Of course if you're using a library, it will probably decode these for you already.
Upvotes: 3