user14272
user14272

Reputation: 119

How get line from parsed java source from Eclipse JDT Parser?

In my hard drive I have follow source:

package DAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CountDownLatch;


public abstract class A {
public class A4{
    public String teste4(int i){
        return (new String("teste"));
    }
}
public class A3{
    public String teste3(int i){
        return (new String("teste"));
    }
    public A4 teste33(int i){
        return (new A4());
    }       
}
public class A2{
    public A3 teste2(int i){
        return (new A3());
    }
}
public class A1{
    public A2 teste1(int i){
        return (new A2());
    }
}


  public int[] toIntArray(List<Integer> list) {
     A1 q=new A1();

     Integer t=q.teste1(
                (new A1()).
                teste1(0).
                teste2(0).
                teste33(0).
                teste4(0).
                length() ).
            teste2(0).
            teste3(0).
            length();
  } 
}

Note that in this file (A.java) we have many blank lines and one command not necessarily was written in the same line, in others words, it's maybe spread in many lines (See last line of method "toIntArray"). When I parse this source code with AST, the "toString" of "CompilationUnit"show the follow struct (AST Struct):

package DAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public abstract class A {
public class A4 {
    public String teste4(    int i){
      return (new String("teste"));
    }
  }
public class A3 {
    public String teste3(    int i){
      return (new String("teste"));
    }
    public A4 teste33(    int i){
      return (new A4());
    }
  }
public class A2 {
    public A3 teste2(    int i){
      return (new A3());
    }
  }
public class A1 {
    public A2 teste1(    int i){
      return (new A2());
    }
  }
  public int[] toIntArray(  List<Integer> list){
    A1 q=new A1();
    Integer t=q.teste1((new A1()).teste1(0).teste2(0).teste33(0).teste4(0).length()).teste2(0).teste3(0).length();
  }
}

Note that in this case, all blank lines was removed and the command "Integer t=q.teste1...." was write in the same line. It's fine to me. Thus, in visit "MethodInvocation", I want to get line number of these invocations. To do this, I make follow source in my astVisitors:

public boolean visit(final CompilationUnit node) {
this.CompilationUnit=node;
}
public boolean visit(final MethodInvocation node) {
Integer LineInFile=this.CompilationUnit.getLineNumber(node.getStartPosition());
}

But the command "getLineNumber" return Line Number in source file in my hard drive, not in AST Struct. In this case, to the line "q.teste1((new.....", the command "getLineNumber" return the lines 44 and 45, but want that this return only the line 44. So, How get de line number in "AST Struct"?

Upvotes: 0

Views: 441

Answers (2)

Stephan Herrmann
Stephan Herrmann

Reputation: 8178

We may start by expanding AST to "abstract syntax tree", which emphasizes that we have no (direct) connection to the source level (=concrete) syntax. You may call it a "model", which captures the semantically relevant aspects, while omitting accidental details like white space. A language could even rename its keywords without any changes to the abstract syntax etc.

It's a convenience for users, that a CompilationUnit additionally stores the positions of line ends, so it can map an AST node back to its original location in the source code.

Upvotes: 1

greg-449
greg-449

Reputation: 111142

The CompilationUnit.toString method is just doing a rough format of the internal AST using the NaiveASTFlattener class.

The actual internal structure of the AST is just a large number of class instances of things like Block, Comment, Statement, ... As such the AST itself does not have line numbers. The only line numbers the AST knows about are the lines in the original source code.

Upvotes: 1

Related Questions