title: Find if the current statement in an If/Else block is the last statement of the THEN branch
body: <p>I am using <a href="https://javaparser.org/" rel="nofollow noreferrer">JavaParser</a> to parse through the contents of the following code and identify the sequence of statements:</p>
  <pre><code>class X {
     void x() {
       int x = 1;
       if (x&gt;4) {
          x=21;
       } else {
          if (x&gt;1) {
             x=3;
          } else {
             x=212;
          } 
       } 
       if (x==4) { 
          x=3;
       }
  }}
  </code></pre>
  <p>The code I am using to parse the above example:</p>
  <pre><code>private static void statements(BlockStmt block, State beforeState, State afterState) {
      List&lt;State&gt; states = new ArrayList&lt;&gt;();
  
      for (Statement statement : block.getStatements()) {
          states.add(new State(statement));
      }
  
      beforeState.nextStates.add(states.get(0));
  
      for (int i = 0; i &lt; states.size(); i++) {
          State currentState = states.get(i);
          
          State nextState;
          if (i == states.size() - 1) {
              nextState = afterState;
          } else {
              nextState = states.get(i + 1);
          }
  
          currentState.statement.accept(new VoidVisitorWithDefaults&lt;Void&gt;() {
              @Override
              public void visit(ExpressionStmt n, Void arg) {
                  currentState.nextStates.add(nextState);
              }
  
              @Override
              public void visit(IfStmt n, Void arg) {
                  statements(n.getThenStmt().asBlockStmt(), currentState, nextState);
                  n.getElseStmt().ifPresent(elseStmt -&gt;
                          statements(elseStmt.asBlockStmt(), currentState, nextState));
              }
          }, null);
   } }
  </code></pre>
  <p>What I would also like to do is to identify if the current statement is an expression statement (i.e., assignment), and if yes, find if it is the last statement contained in the THEN block of an If/Else statement. In the example above that would be statement <code>x=3;</code>. However, I am not sure how to achieve this.</p>
  <p>So far my thinking was along these lines:</p>
  <pre><code>ExpressionStmt exprStmt = (ExpressionStmt) currentState.statement;
  Node exprStmtParent = exprStmt.getParent();
  List&lt;Node&gt; exprStmtSibilings = exprStmtParent.getChildNodes();
  Node lastNode = exprStmtSiblings.get(exprStmtSiblings.size() - 1);
  boolean exprStmtIsLast = lastNode == ifStmt;
  </code></pre>
  <p>which works for if statements but not for assignments. Any recommendations on how to approach this?</p>