1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/source/util/TreePath.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,158 @@ 1.4 +/* 1.5 + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.source.util; 1.30 + 1.31 +import java.util.Iterator; 1.32 + 1.33 +import com.sun.source.tree.*; 1.34 + 1.35 +/** 1.36 + * A path of tree nodes, typically used to represent the sequence of ancestor 1.37 + * nodes of a tree node up to the top level CompilationUnitTree node. 1.38 + * 1.39 + * @author Jonathan Gibbons 1.40 + * @since 1.6 1.41 + */ 1.42 +@jdk.Exported 1.43 +public class TreePath implements Iterable<Tree> { 1.44 + /** 1.45 + * Gets a tree path for a tree node within a compilation unit. 1.46 + * @return null if the node is not found 1.47 + */ 1.48 + public static TreePath getPath(CompilationUnitTree unit, Tree target) { 1.49 + return getPath(new TreePath(unit), target); 1.50 + } 1.51 + 1.52 + /** 1.53 + * Gets a tree path for a tree node within a subtree identified by a TreePath object. 1.54 + * @return null if the node is not found 1.55 + */ 1.56 + public static TreePath getPath(TreePath path, Tree target) { 1.57 + path.getClass(); 1.58 + target.getClass(); 1.59 + 1.60 + class Result extends Error { 1.61 + static final long serialVersionUID = -5942088234594905625L; 1.62 + TreePath path; 1.63 + Result(TreePath path) { 1.64 + this.path = path; 1.65 + } 1.66 + } 1.67 + 1.68 + class PathFinder extends TreePathScanner<TreePath,Tree> { 1.69 + public TreePath scan(Tree tree, Tree target) { 1.70 + if (tree == target) { 1.71 + throw new Result(new TreePath(getCurrentPath(), target)); 1.72 + } 1.73 + return super.scan(tree, target); 1.74 + } 1.75 + } 1.76 + 1.77 + if (path.getLeaf() == target) { 1.78 + return path; 1.79 + } 1.80 + 1.81 + try { 1.82 + new PathFinder().scan(path, target); 1.83 + } catch (Result result) { 1.84 + return result.path; 1.85 + } 1.86 + return null; 1.87 + } 1.88 + 1.89 + /** 1.90 + * Creates a TreePath for a root node. 1.91 + */ 1.92 + public TreePath(CompilationUnitTree t) { 1.93 + this(null, t); 1.94 + } 1.95 + 1.96 + /** 1.97 + * Creates a TreePath for a child node. 1.98 + */ 1.99 + public TreePath(TreePath p, Tree t) { 1.100 + if (t.getKind() == Tree.Kind.COMPILATION_UNIT) { 1.101 + compilationUnit = (CompilationUnitTree) t; 1.102 + parent = null; 1.103 + } 1.104 + else { 1.105 + compilationUnit = p.compilationUnit; 1.106 + parent = p; 1.107 + } 1.108 + leaf = t; 1.109 + } 1.110 + /** 1.111 + * Get the compilation unit associated with this path. 1.112 + */ 1.113 + public CompilationUnitTree getCompilationUnit() { 1.114 + return compilationUnit; 1.115 + } 1.116 + 1.117 + /** 1.118 + * Get the leaf node for this path. 1.119 + */ 1.120 + public Tree getLeaf() { 1.121 + return leaf; 1.122 + } 1.123 + 1.124 + /** 1.125 + * Get the path for the enclosing node, or null if there is no enclosing node. 1.126 + */ 1.127 + public TreePath getParentPath() { 1.128 + return parent; 1.129 + } 1.130 + 1.131 + /** 1.132 + * Iterates from leaves to root. 1.133 + */ 1.134 + @Override 1.135 + public Iterator<Tree> iterator() { 1.136 + return new Iterator<Tree>() { 1.137 + @Override 1.138 + public boolean hasNext() { 1.139 + return next != null; 1.140 + } 1.141 + 1.142 + @Override 1.143 + public Tree next() { 1.144 + Tree t = next.leaf; 1.145 + next = next.parent; 1.146 + return t; 1.147 + } 1.148 + 1.149 + @Override 1.150 + public void remove() { 1.151 + throw new UnsupportedOperationException(); 1.152 + } 1.153 + 1.154 + private TreePath next = TreePath.this; 1.155 + }; 1.156 + } 1.157 + 1.158 + private CompilationUnitTree compilationUnit; 1.159 + private Tree leaf; 1.160 + private TreePath parent; 1.161 +}