1

I recently used getLineNumber() method in StackTraceElement class to find line number of a java source file at execution time(for debug purpose). This method worked perfectly. But i like to know, how it is finding the actual line number. Is there a one-to-one mapping between java source file line and java class file line?

1 Answer 1

2

It does not makes sense that there would be a one to one mapping as a .java file can have multiple method calls on the same line. This is not true for a .class file. Meta data about the .java file is stored in the .class file, as is the case with generics.

Extract from the VM spec:

The LineNumberTable attribute is an optional variable-length attribute in the attributes table of a Code attribute. It may be used by debuggers to determine which part of the Java virtual machine code array corresponds to a given line number in the original source file. If LineNumberTable attributes are present in the attributes table of a given Code attribute, then they may appear in any order. Furthermore, multiple LineNumberTable attributes may together represent a given line of a source file; that is, LineNumberTable attributes need not be one-to-one with source lines.

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

2 Comments

thanks, for mentioning vm spec, i had no idea to search there.
Can you please provide a link to the spec?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.