duke@1: /* jjg@1357: * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.source.util; duke@1: jjg@1357: import java.util.Iterator; jjg@1357: duke@1: import com.sun.source.tree.*; duke@1: duke@1: /** duke@1: * A path of tree nodes, typically used to represent the sequence of ancestor duke@1: * nodes of a tree node up to the top level CompilationUnitTree node. duke@1: * duke@1: * @author Jonathan Gibbons duke@1: * @since 1.6 duke@1: */ duke@1: public class TreePath implements Iterable { duke@1: /** duke@1: * Gets a tree path for a tree node within a compilation unit. duke@1: * @return null if the node is not found duke@1: */ duke@1: public static TreePath getPath(CompilationUnitTree unit, Tree target) { duke@1: return getPath(new TreePath(unit), target); duke@1: } duke@1: duke@1: /** duke@1: * Gets a tree path for a tree node within a subtree identified by a TreePath object. duke@1: * @return null if the node is not found duke@1: */ duke@1: public static TreePath getPath(TreePath path, Tree target) { duke@1: path.getClass(); duke@1: target.getClass(); duke@1: duke@1: class Result extends Error { duke@1: static final long serialVersionUID = -5942088234594905625L; duke@1: TreePath path; duke@1: Result(TreePath path) { duke@1: this.path = path; duke@1: } duke@1: } jjg@1455: duke@1: class PathFinder extends TreePathScanner { duke@1: public TreePath scan(Tree tree, Tree target) { jjg@1455: if (tree == target) { duke@1: throw new Result(new TreePath(getCurrentPath(), target)); jjg@1455: } duke@1: return super.scan(tree, target); duke@1: } duke@1: } duke@1: jjg@1455: if (path.getLeaf() == target) { jjg@1455: return path; jjg@1455: } jjg@1455: duke@1: try { duke@1: new PathFinder().scan(path, target); duke@1: } catch (Result result) { duke@1: return result.path; duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** duke@1: * Creates a TreePath for a root node. duke@1: */ duke@1: public TreePath(CompilationUnitTree t) { duke@1: this(null, t); duke@1: } duke@1: duke@1: /** duke@1: * Creates a TreePath for a child node. duke@1: */ duke@1: public TreePath(TreePath p, Tree t) { duke@1: if (t.getKind() == Tree.Kind.COMPILATION_UNIT) { duke@1: compilationUnit = (CompilationUnitTree) t; duke@1: parent = null; duke@1: } duke@1: else { duke@1: compilationUnit = p.compilationUnit; duke@1: parent = p; duke@1: } duke@1: leaf = t; duke@1: } duke@1: /** duke@1: * Get the compilation unit associated with this path. duke@1: */ duke@1: public CompilationUnitTree getCompilationUnit() { duke@1: return compilationUnit; duke@1: } duke@1: duke@1: /** duke@1: * Get the leaf node for this path. duke@1: */ duke@1: public Tree getLeaf() { duke@1: return leaf; duke@1: } duke@1: duke@1: /** duke@1: * Get the path for the enclosing node, or null if there is no enclosing node. duke@1: */ duke@1: public TreePath getParentPath() { duke@1: return parent; duke@1: } duke@1: duke@1: public Iterator iterator() { duke@1: return new Iterator() { duke@1: public boolean hasNext() { jjg@308: return next != null; duke@1: } duke@1: duke@1: public Tree next() { jjg@308: Tree t = next.leaf; jjg@308: next = next.parent; jjg@308: return t; duke@1: } duke@1: duke@1: public void remove() { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: jjg@308: private TreePath next = TreePath.this; duke@1: }; duke@1: } duke@1: duke@1: private CompilationUnitTree compilationUnit; duke@1: private Tree leaf; duke@1: private TreePath parent; duke@1: }