Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top