I have a line number of a Java source file and want to get the surrounding method for that line number programmatically.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Use something like JavaParser. From what I can see, the Node class has references to begin and end column and row indexes. MethodDeclaration is a subclass of Node, so parse the source file and search for the MethodDeclaration that 'contains' your line number.
Sample code
You would make sure the
src
file points to your own source. Here, I just use the source of the sample itself.Sample output
For the new version of JavaParser: