just wondering if there is a way to get all the statements in a method without manually traversing the AST of a method.
So far I've tried to get all of the statements by manually traversing through the tree which is generally a bad idea and tricky to do as well.
public void visit(MethodDeclaration n, Object arg) {
int total = 0;
NodeList<Statement> statements = n.getBody().get().getStatements();
for(Node node: statements) {
//System.out.println(node.getClass());
if(node instanceof ExpressionStmt) {
if(node.toString().contains("=")) {
total++;
}
}
}
super.visit(n, arg);
System.out.println(total);
}
The code above works but It gets only the statements at the same depth level of the tree rather than all of statements of all the depth levels of the AST If anyone could help it would be much appreciated.