TreePath (Compiler Tree API )
Keywords: com.sun.source.util.TreePath class
Date: 2026-04-17

com.sun.source.util

## Class TreePath

- java.lang.Object
- - com.sun.source.util.TreePath

- All Implemented Interfaces:
    Iterable<Tree>

      @Exported
      public class TreePath
      extends Object
      implements Iterable<Tree>

  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.
  Since:
    1.6

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TreePath(CompilationUnitTree t)` Creates a TreePath for a root node.
    `TreePath(TreePath p, Tree t)` Creates a TreePath for a child node.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `CompilationUnitTree` | `getCompilationUnit()` Get the compilation unit associated with this path.
    `Tree` | `getLeaf()` Get the leaf node for this path.
    `TreePath` | `getParentPath()` Get the path for the enclosing node, or null if there is no enclosing node.
    `static TreePath` | `getPath(CompilationUnitTree unit, Tree target)` Gets a tree path for a tree node within a compilation unit.
    `static TreePath` | `getPath(TreePath path, Tree target)` Gets a tree path for a tree node within a subtree identified by a TreePath object.
    `Iterator<Tree>` | `iterator()` Iterates from leaves to root.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.lang.Iterable

      `forEach, spliterator`

- - ### Constructor Detail

    - #### TreePath

          public TreePath(CompilationUnitTree t)

      Creates a TreePath for a root node.

    - #### TreePath

          public TreePath(TreePath p,
                          Tree t)

      Creates a TreePath for a child node.

  - ### Method Detail

    - #### getPath

          public static TreePath getPath(CompilationUnitTree unit,
                                         Tree target)

      Gets a tree path for a tree node within a compilation unit.
      Returns:
        null if the node is not found

    - #### getPath

          public static TreePath getPath(TreePath path,
                                         Tree target)

      Gets a tree path for a tree node within a subtree identified by a TreePath object.
      Returns:
        null if the node is not found

    - #### getCompilationUnit

          public CompilationUnitTree getCompilationUnit()

      Get the compilation unit associated with this path.

    - #### getLeaf

          public Tree getLeaf()

      Get the leaf node for this path.

    - #### getParentPath

          public TreePath getParentPath()

      Get the path for the enclosing node, or null if there is no enclosing node.

    - #### iterator

          public Iterator<Tree> iterator()

      Iterates from leaves to root.
      Specified by:
        `iterator` in interface `Iterable<Tree>`
      Returns:
        an Iterator.

Copyright © 2005, 2026, Oracle and/or its affiliates. All rights reserved.