Passing through the AST node

I want to find out the line number the call refers to using the AST api in the package. How can i do this?

+3
source share
2 answers

You have an illustration on how to search in the CompilationUnit method in this papercut article :

for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
IType[] types = unit.getTypes();
for (int i = 0; i < types.length; i++) {
  IType type = types[i];
  IMethod[] methods = type.getMethods();
  • If the method is ASTNode , you can use the function ASTNode.getStartPosition().
  • If the compilation unit of this IMember is CompilationUnit, you can use it inCompilationUnit.getLineNumber(position)
+2
source

CompilationUnit.getLineNumber (int position)

in relation to the CompilationUnit object

Documentation:

, . 1, . . , A\n {\n} 3 [0,7], [8,9] [10,10]. -1 -2, .

+2

Source: https://habr.com/ru/post/1731001/


All Articles