I have a line number of a Java source file and want to get the sourounding method for that line number programatically.
I looked into ANTLR which didn’t help me much.
Janino (http://www.janino.net) seems promising, I would scan and parse (and if necessary compile) the code. Then I could use JDI and
ReferenceType.locationsOfLine(int lineNumber)
Still I don’t know how to use JDI for doing this and didn’t find a tutorial that goes anywhere in this direction.
Maybe there is some other way that I am completely missing.
If you’re using Java 6, and if you don’t mind using Sun’s APIs, then you can use the javac API. You’ll need to add
tools.jarto your classpath.