1 /*
   2  * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.source.util;
  27 
  28 import java.util.Iterator;
  29 
  30 import com.sun.source.tree.*;
  31 
  32 /**
  33  * A path of tree nodes, typically used to represent the sequence of ancestor
  34  * nodes of a tree node up to the top level CompilationUnitTree node.
  35  *
  36  * @author Jonathan Gibbons
  37  * @since 1.6
  38  */
  39 @jdk.Exported
  40 public class TreePath implements Iterable<Tree> {
  41     /**
  42      * Gets a tree path for a tree node within a compilation unit.
  43      * @return null if the node is not found
  44      */
  45     public static TreePath getPath(CompilationUnitTree unit, Tree target) {
  46         return getPath(new TreePath(unit), target);
  47     }
  48 
  49     /**
  50      * Gets a tree path for a tree node within a subtree identified by a TreePath object.
  51      * @return null if the node is not found
  52      */
  53     public static TreePath getPath(TreePath path, Tree target) {
  54         path.getClass();
  55         target.getClass();
  56 
  57         class Result extends Error {
  58             static final long serialVersionUID = -5942088234594905625L;
  59             TreePath path;
  60             Result(TreePath path) {
  61                 this.path = path;
  62             }
  63         }
  64 
  65         class PathFinder extends TreePathScanner<TreePath,Tree> {
  66             public TreePath scan(Tree tree, Tree target) {
  67                 if (tree == target) {
  68                     throw new Result(new TreePath(getCurrentPath(), target));
  69                 }
  70                 return super.scan(tree, target);
  71             }
  72         }
  73 
  74         if (path.getLeaf() == target) {
  75             return path;
  76         }
  77 
  78         try {
  79             new PathFinder().scan(path, target);
  80         } catch (Result result) {
  81             return result.path;
  82         }
  83         return null;
  84     }
  85 
  86     /**
  87      * Creates a TreePath for a root node.
  88      */
  89     public TreePath(CompilationUnitTree t) {
  90         this(null, t);
  91     }
  92 
  93     /**
  94      * Creates a TreePath for a child node.
  95      */
  96     public TreePath(TreePath p, Tree t) {
  97         if (t.getKind() == Tree.Kind.COMPILATION_UNIT) {
  98             compilationUnit = (CompilationUnitTree) t;
  99             parent = null;
 100         }
 101         else {
 102             compilationUnit = p.compilationUnit;
 103             parent = p;
 104         }
 105         leaf = t;
 106     }
 107     /**
 108      * Get the compilation unit associated with this path.
 109      */
 110     public CompilationUnitTree getCompilationUnit() {
 111         return compilationUnit;
 112     }
 113 
 114     /**
 115      * Get the leaf node for this path.
 116      */
 117     public Tree getLeaf() {
 118         return leaf;
 119     }
 120 
 121     /**
 122      * Get the path for the enclosing node, or null if there is no enclosing node.
 123      */
 124     public TreePath getParentPath() {
 125         return parent;
 126     }
 127 
 128     /**
 129      *  Iterates from leaves to root.
 130      */
 131     @Override
 132     public Iterator<Tree> iterator() {
 133         return new Iterator<Tree>() {
 134             @Override
 135             public boolean hasNext() {
 136                 return next != null;
 137             }
 138 
 139             @Override
 140             public Tree next() {
 141                 Tree t = next.leaf;
 142                 next = next.parent;
 143                 return t;
 144             }
 145 
 146             @Override
 147             public void remove() {
 148                 throw new UnsupportedOperationException();
 149             }
 150 
 151             private TreePath next = TreePath.this;
 152         };
 153     }
 154 
 155     private CompilationUnitTree compilationUnit;
 156     private Tree leaf;
 157     private TreePath parent;
 158 }