Search code examples
c#javadiffline-numbersabstract-syntax-tree

Method name from line number


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)

(This would be useful in limiting the output of checkstyle to just the method touched).

I'm assuming you'd have to use an Abstract Syntax Tree to do Line#->MethodName.


Solution

  • (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.