The solution proposed by Dennis Shirazi is not working properly. Based on his solution, we solved this issue with this :
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 = currentNode.getFirstChild(); AST nextSibling = currentNode.getNextSibling(); if (firstChild != null) { nodeDeque.push(firstChild); } if (nextSibling != null) { nodeDeque.push(nextSibling); } } return found; }
Hope this can help…