gpt4 book ai didi

java - StackTraceElement类中的getLineNumber()方法如何查找java源文件的实际行号

转载 作者:行者123 更新时间:2023-11-30 03:37:06 25 4
gpt4 key购买 nike

我最近在 StackTraceElement 类中使用 getLineNumber() 方法在执行时查找 java 源文件的行号(用于调试目的)。这个方法非常有效。但我想知道,它是如何找到实际的行号的。 java源文件行和java类文件行之间是否存在一对一的映射?

最佳答案

存在一对一映射是没有意义的,因为 .java 文件可以在同一行上有多个方法调用。对于 .class 文件来说,情况并非如此。有关 .java 文件的元数据存储在 .class 文件中,与泛型的情况一样。

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.

关于java - StackTraceElement类中的getLineNumber()方法如何查找java源文件的实际行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27603915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com