title: Is there a way to get all the statements in a method without manually traversing the AST
body: <p>just wondering if there is a way to get all the statements in a method without manually traversing the AST of a method.</p>
  
  <p>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.</p>
  
  <pre class="lang-java prettyprint-override"><code>public void visit(MethodDeclaration n, Object arg) {
          int total = 0;
          NodeList&lt;Statement&gt; 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);
      }
  </code></pre>
  
  <p>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.</p>