1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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/Pre-order">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/Post-order">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 * Performs a pre-order node traversal starting with a given node. When each node is visited, {@link #process(Node)} 70 * is called for further processing. 71 * 72 * @param node The node at which the traversal begins. 73 * @see <a href="https://en.wikipedia.org/wiki/Pre-order">Pre-order traversal</a> 74 * @deprecated As of release 3.1.0, replaced by {@link #visitPreOrder(Node)} 75 */ 76 @Deprecated 77 public void visitDepthFirst(Node node) { 78 visitPreOrder(node); 79 } 80 81 /** 82 * https://en.wikipedia.org/wiki/Breadth-first_search 83 * 84 * @param node the start node, and the first one that is passed to process(node). 85 */ 86 public void visitBreadthFirst(Node node) { 87 final Queue<Node> queue = new LinkedList<>(); 88 queue.offer(node); 89 while (queue.size() > 0) { 90 final Node head = queue.peek(); 91 for (Node child : head.getChildNodes()) { 92 queue.offer(child); 93 } 94 process(queue.poll()); 95 } 96 } 97 98 /** 99 * Process the given node. 100 * 101 * @param node The current node to process. 102 */ 103 public abstract void process(Node node); 104 105 /** 106 * Performs a simple traversal over all nodes that have the passed node as their parent. 107 */ 108 public void visitDirectChildren(Node node) { 109 new ArrayList<>(node.getChildNodes()).forEach(this::process); 110 } 111 } 112