| To fix the error I came up with this solution using a java.util.ArrayDeque:
private <T extends AST> List<T> findAllNodes(AST node, Class<T> clazz) {
ArrayList<T> found = new ArrayList<>();
Deque<AST> nodeDeque = new ArrayDeque<>();
nodeDeque.push( node );
while ( !nodeDeque.isEmpty() ) {
AST currentNode = nodeDeque.pop();
if ( clazz.isAssignableFrom( currentNode.getClass() ) ) {
found.add( (T) currentNode );
}
AST firstChild = node.getFirstChild();
AST nextSibling = node.getNextSibling();
if ( firstChild != null ) {
nodeDeque.push( firstChild );
}
if ( nextSibling != null ) {
nodeDeque.push( nextSibling );
}
}
return found;
}
I still have to confirm it works as intended but wanted to give a short status update that I’m working on this. Would love to get some feedback on my current solution and if there are any flaws that I’m missing. |