2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.