| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.ast.visitor; |
| 23 | |
| 24 | import com.github.javaparser.ast.Node; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.LinkedList; |
| 28 | import java.util.Queue; |
| 29 | |
| 30 | /** |
| 31 | * Iterate over all the nodes in (a part of) the AST. In contrast to the visit methods in Node, these methods are |
| 32 | * implemented in a simple recursive way which should be more efficient. A disadvantage is that they cannot be quit in |
| 33 | * the middle of their traversal. |
| 34 | */ |
| 35 | public abstract class TreeVisitor { |
| 36 | |
| 37 | public void visitLeavesFirst(Node node) { |
| 38 | for (Node child : node.getChildNodes()) { |
| 39 | visitLeavesFirst(child); |
| 40 | } |
| 41 | process(node); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Performs a pre-order node traversal starting with a given node. When each node is visited, {@link #process(Node)} |
| 46 | * is called for further processing. |
| 47 | * |
| 48 | * @param node The node at which the traversal begins. |
| 49 | * @see <a href="https://en.wikipedia.org/wiki/Depth-first_search#Vertex_orderings">Pre-order traversal</a> |
| 50 | */ |
| 51 | public void visitPreOrder(Node node) { |
| 52 | process(node); |
| 53 | new ArrayList<>(node.getChildNodes()).forEach(this::visitPreOrder); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Performs a post-order node traversal starting with a given node. When each node is visited, {@link |
| 58 | * #process(Node)} is called for further processing. |
| 59 | * |
| 60 | * @param node The node at which the traversal begins. |
| 61 | * @see <a href="https://en.wikipedia.org/wiki/Depth-first_search#Vertex_orderings">Post-order traversal</a> |
| 62 | */ |
| 63 | public void visitPostOrder(Node node) { |
| 64 | new ArrayList<>(node.getChildNodes()).forEach(this::visitPostOrder); |
| 65 | process(node); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * https://en.wikipedia.org/wiki/Breadth-first_search |
| 70 | * |
| 71 | * @param node the start node, and the first one that is passed to process(node). |
| 72 | */ |
| 73 | public void visitBreadthFirst(Node node) { |
| 74 | final Queue<Node> queue = new LinkedList<>(); |
| 75 | queue.offer(node); |
| 76 | while (queue.size() > 0) { |
| 77 | final Node head = queue.peek(); |
| 78 | for (Node child : head.getChildNodes()) { |
| 79 | queue.offer(child); |
| 80 | } |
| 81 | process(queue.poll()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Process the given node. |
| 87 | * |
| 88 | * @param node The current node to process. |
| 89 | */ |
| 90 | public abstract void process(Node node); |
| 91 | |
| 92 | /** |
| 93 | * Performs a simple traversal over all nodes that have the passed node as their parent. |
| 94 | */ |
| 95 | public void visitDirectChildren(Node node) { |
| 96 | new ArrayList<>(node.getChildNodes()).forEach(this::process); |
| 97 | } |
| 98 | } |
| 99 |
Members