来自行号的方法名称 [英] Method name from line number
问题描述
给定特定类源代码 (Java/C#) 的行号 - 是否有一种简单的方法可以获取它所属的方法的名称?(如果它在 1 之内)(大概是使用抽象语法树)
Given a line number of a particular class source code (Java/C#) - is there an easy way to get the name of the method it falls within? (If it falls within one) (Presumably using an Abstract Syntax Tree)
(这将有助于将 checkstyle 的输出限制为仅触及的方法).
(This would be useful in limiting the output of checkstyle to just the method touched).
我假设您必须使用抽象语法树来执行 Line#->MethodName.
I'm assuming you'd have to use an Abstract Syntax Tree to do Line#->MethodName.
推荐答案
(Java-specific)
(Java-specific)
如果类文件是用调试信息编译的,那么行号表将包含代码<->行号的映射.我不认为有一个内置的 API 可以在运行时得到这个,尽管我相信你可能可以使用一些字节码工程库,比如 ASM 或 BCEL.
If the class file was compiled with debug info then the line number table will contain a mapping of code<->line number. I don't think there's a built-in API for getting at this at runtime though I'm sure you can probably do with with some of the bytecode engineering libs out there such as ASM or BCEL.
这篇关于来自行号的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!