FileDocCategorySizeDatePackage
TreePath.javaAPI DocJava SE 6 API3127Tue Jun 10 00:23:26 BST 2008com.sun.source.util

TreePath

public class TreePath extends Object implements Iterable
A path of tree nodes, typically used to represent the sequence of ancestor nodes of a tree node up to the top level CompilationUnitTree node.
author
Jonathan Gibbons
since
1.6

Fields Summary
private CompilationUnitTree
compilationUnit
private Tree
leaf
private TreePath
parent
Constructors Summary
public TreePath(CompilationUnitTree t)
Creates a TreePath for a root node.

        this(null, t);
    
public TreePath(TreePath p, Tree t)
Creates a TreePath for a child node.

        if (t.getKind() == Tree.Kind.COMPILATION_UNIT) {
            compilationUnit = (CompilationUnitTree) t;
            parent = null; 
        }
        else {
            compilationUnit = p.compilationUnit;
            parent = p;
        }
        leaf = t;
    
Methods Summary
public CompilationUnitTreegetCompilationUnit()
Get the compilation unit associated with this path.

	return compilationUnit;
    
public TreegetLeaf()
Get the leaf node for this path.

	return leaf;
    
public com.sun.source.util.TreePathgetParentPath()
Get the path for the enclosing node, or null if there is no enclosing node.

	return parent;
    
public static com.sun.source.util.TreePathgetPath(CompilationUnitTree unit, Tree target)
Gets a tree path for a tree node within a compilation unit.

return
null if the node is not found

	return getPath(new TreePath(unit), target);
    
public static com.sun.source.util.TreePathgetPath(com.sun.source.util.TreePath path, Tree target)
Gets a tree path for a tree node within a subtree identified by a TreePath object.

return
null if the node is not found

	path.getClass();
	target.getClass();
	
	class Result extends Error {
	    static final long serialVersionUID = -5942088234594905625L;
	    TreePath path;
	    Result(TreePath path) {
		this.path = path;
	    }
	}
	class PathFinder extends TreePathScanner<TreePath,Tree> {
	    public TreePath scan(Tree tree, Tree target) {
		if (tree == target)
		    throw new Result(new TreePath(getCurrentPath(), target));
		return super.scan(tree, target);
	    }
	}
	
	try {
	    new PathFinder().scan(path, target);
	} catch (Result result) {
	    return result.path;
	}
        return null;
    
public java.util.Iteratoriterator()

	return new Iterator<Tree>() {
	    public boolean hasNext() {
		return curr.parent != null;
	    }
	    
	    public Tree next() {
		curr = curr.parent;
		return curr.leaf;
	    }
	    
	    public void remove() {
		throw new UnsupportedOperationException();
	    }
	    
	    private TreePath curr;
	};