Method name from line number

送分小仙女□ 提交于 2019-12-05 12:39:00

(Java-specific)

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.

I don't know about Java, but .NET assemblies don't have line numbers stored in their metadata tables - you would need a PDB (Program Database) file for that kind of information.

Yes, as you said, using the right kind of AST. The DMS Software Reengineering Toolkit can build ASTs for Java and for C#. Each AST node is decorated with line number data. Each AST node corresponds to a grammar rule.

So the problem of determining the method name for a line number is pretty easy: find a node in the AST with corresponding line number, and climb the tree until an AST node is found that represents a method declaration; find the method-name subtree from that node and print it out.

This trick is heavily used in static analysis tools built using DMS, to report an offending method name for a problem diagnosed at a particular line number. DMS is designed to enable others to build such static analysis tools.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!