code: private static Tree getNextNodeOrParent(Tree current, VisitorState state) {
      Tree predecessorNode = current;
      TreePath enclosingPath = state.getPath();
      while (enclosingPath != null
          && !(enclosingPath.getLeaf() instanceof BlockTree)
          && !(enclosingPath.getLeaf() instanceof ClassTree)) {
        predecessorNode = enclosingPath.getLeaf();
        enclosingPath = enclosingPath.getParentPath();
      }
  
      if (enclosingPath == null) {
        return state.getPath().getParentPath().getLeaf();
      }
  
      Tree parent = enclosingPath.getLeaf();
  
      if (parent instanceof BlockTree) {
        return after(predecessorNode, ((BlockTree) parent).getStatements(), parent);
      } else if (parent instanceof ClassTree) {
        return after(predecessorNode, ((ClassTree) parent).getMembers(), parent);
      }
      return parent;
    }
docs: Find the node which (approximately) follows this one in the tree. This works by walking upwards
  to find enclosing block (or class) and then looking for the node after the subtree we walked.
  If our subtree is the last of the block then we return the node for the block instead, if we
  can't find a suitable block we return the parent node.