1

For example, I can get the begin line and end line, but how do I get the source code between begin line and end line. for the example code here.

String[] cmds =new String[2];
String OS = System.getProperty("os.name");
if (OS.startsWith("Windows")) {
    cmds[0]="cmd";
    cmds[1]="/c";
}
else {
    cmds[0]="/bin/sh";
    cmds[1]="-c";
}

try {
    Process p = Runtime.getRuntime().exec(cmds);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I want to get the code below, that is the definition related to cmds.

String[] cmds =new String[2];
String OS = System.getProperty("os.name");
if (OS.startsWith("Windows")) {
     cmds[0]="cmd";
     cmds[1]="/c";
}
else {
     cmds[0]="/bin/sh";
     cmds[1]="-c";
}
Lindstorm
  • 890
  • 2
  • 7
  • 22
heshuguo
  • 21
  • 4
  • Do you want to find the largest `Node` within a given two lines, or any source code within two lines? For example, if you have a for loop that goes from lines 1 to 3, and your inputs are 2 and 4, should the output be half a for loop or just the largest node within lines 2-4? If it just half a for loop, then I think it would be between to use regular expressions than JavaParser. – Lindstorm Oct 28 '19 at 18:55

1 Answers1

0

You could create a visitor class like

 public class RangeFinder extends VoidVisitorWithDefaults<Range> {
    List<Node> nodesFound = new ArrayList<>();


    public void defaultAction(Node n, Range givenRange) {
        //Range of element in your code
        Range rangeOfNode = n.getRange().get();

        //If your given two lines contain this node, add it 
        if (givenRange.contains(rangeOfNode))
            nodesFound.add(n);
        //Else, if it overlaps with the node, check the children of the node
        else if (givenRange.overlapsWith(rangeOfNode)) {
            n.getChildNodes().forEach(child -> child.accept(this, givenRange));
        }
    }
}

And then use the method

 public static String findSrc(CompilationUnit cu, int beginRow, int endRow) {
      //Parse code for all nodes contained within two rows
      RangeFinder rangeFinder = new RangeFinder();
      cu.accept(rangeFinder, Range.range(beginRow, 0, endRow, 0));

      //Build string
      StringBuilder codeBuilder = new StringBuilder();
      rangeFinder.nodesFound.forEach(node -> codeBuilder.append(node.toString()).append("\n"));
      return codeBuilder.toString();

}
Lindstorm
  • 890
  • 2
  • 7
  • 22
  • Thank you, @Lindstorm. By the way, is there any other ways to get all the codes related to the variables including definition and operation? – heshuguo Oct 29 '19 at 07:45
  • @heshuguo Yes, you create a visitor (there are many types of visitors), and you override the `visit` methods for `AssignExpr`, `VariableDeclarator`, `VariableDeclarationExpr`, or `FieldDeclaraton` depending on why type of variable data you want. If this is what you were trying to do in the first place, I would not reccomend using the solution in this answer but rather just create a visitor and check the range in the visit method itself – Lindstorm Oct 29 '19 at 21:01
  • Even if you were only considering the lines of code, I would think regular expressions would be a better solution due to it being way less code. To use regular expressions, you would just count the number of `\n` until you are between two lines – Lindstorm Oct 29 '19 at 21:13