Skip to content
Advertisement

get line number of a MethodDeclaration

I’m trying to create a parser of Java files, but I can’t get the correct line number of each method. this is the code that I’ve right now:

JavaScript

imagine that we’re parsing the following class

JavaScript

the code

JavaScript

instead of returning 6, returns 3. apparently the eclipse.jdt API considers that javacode also belongs to the method. so, my question is, how to get the correct line number of method Main()?

EDIT_1

we can have access to the JavaDoc:

JavaScript

and count the number of lines

JavaScript

and in fact works for that case but not to all cases, like for example

JavaScript

JavaScript

JavaScript

JavaScript

apparently, toString method of Javadoc class ignores empty lines and considers label @param as two lines, etc.

Cheers 😉

Advertisement

Answer

Call the getName() of MethodDeclaration to get the ASTNode for the method name and get the line number of the name.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement