Class TreePathCacher

java.lang.Object
com.sun.source.util.TreeScanner<TreePath,Tree>
org.checkerframework.framework.util.TreePathCacher
All Implemented Interfaces:
TreeVisitor<TreePath,Tree>

public class TreePathCacher extends TreeScanner<TreePath,Tree>
TreePathCacher is a TreeScanner that creates and caches a TreePath for a target Tree.

This class replicates some logic from com.sun.source.util.TreePath.getPath but caches results so that lookups with overlapping paths reuse each other's work.

The search is lazy about allocation: scan(com.sun.source.tree.Tree, com.sun.source.tree.Tree) only pushes and pops the trees it visits on currentStack and allocates no TreePath during traversal. Only when the target is reached does buildPathForStack() materialize (and cache) the TreePath nodes along the root-to-target path. Off-path trees visited during a search are therefore not cached, which keeps foundPaths far smaller than caching every visited node would. See the "TreePathCacher" entries in docs/developer/performance-notes.md for the allocation measurements that motivated this design.

  • Constructor Details

    • TreePathCacher

      public TreePathCacher()
      Construct a TreePathCacher.
  • Method Details

    • useUnit

      public void useUnit(CompilationUnitTree newUnit)
      Prepares the cache for the given compilation unit: clears it, unless it is already for that unit. The checkers of a compound checker share one cache, and each of them calls this when it starts a compilation unit.

      Clearing the cache when a later checker starts the same unit would lose the paths of the artificial trees that a CFG builder registered. A scan cannot find an artificial tree, so the registered path is the only source of its path.

      Parameters:
      newUnit - the compilation unit that is about to be checked
    • isCached

      public boolean isCached(@FindDistinct Tree target)
      Returns true if the tree is cached.
      Parameters:
      target - the tree to search for
      Returns:
      true if the tree is cached
    • getCachedPath

      public @Nullable TreePath getCachedPath(@FindDistinct Tree target)
      Returns the cached path for the given tree, without scanning. Performs a single map lookup.

      Returns null in two distinct cases that this method does not distinguish: (a) target is not cached at all, or (b) target was previously searched for via getPath(com.sun.source.tree.CompilationUnitTree, com.sun.source.tree.Tree) and not found in the compilation unit (in which case getPath(com.sun.source.tree.CompilationUnitTree, com.sun.source.tree.Tree) cached null to avoid re-scanning). Callers that need to distinguish these cases must follow up with isCached(com.sun.source.tree.Tree).

      Parameters:
      target - the tree to look up
      Returns:
      the cached path for target, or null if target is not cached or is cached as not-in-unit
    • addPath

      public void addPath(Tree target, TreePath path)
      Adds the given key and value to the cache.
      Parameters:
      target - the tree to add
      path - the path to cache
    • addArtificialPath

      public void addArtificialPath(Tree tree, TreePath path)
      Adds the path of an artificial tree to the cache and records that the tree is artificial.

      An artificial tree is one that the CFG builder created, such as the binary operation of a compound assignment. It is not in the compilation unit, so a scan cannot find it, and the path registered here is the only source of its path.

      Parameters:
      tree - the artificial tree
      path - the path of tree
    • isArtificial

      public boolean isArtificial(@FindDistinct Tree tree)
      Returns true if tree is an artificial tree, whose path was registered with addArtificialPath(com.sun.source.tree.Tree, com.sun.source.util.TreePath) for the current compilation unit.
      Parameters:
      tree - a tree
      Returns:
      true if tree is an artificial tree
    • getPath

      public @Nullable TreePath getPath(CompilationUnitTree root, @FindDistinct Tree target)
      Returns the TreePath for a Tree.
      Parameters:
      root - the compilation unit to search in
      target - the target tree to look for
      Returns:
      the TreePath corresponding to target, or null if target is not found in the compilation root
    • getPath

      public @Nullable TreePath getPath(TreePath rootPath, @FindDistinct Tree target)
      Return the TreePath for target, searching the compilation unit that rootPath belongs to, but starting the search at rootPath's leaf and expanding outward.

      This searches the whole compilation unit, not just rootPath's subtree (despite taking a path argument, it does not have the subtree-only semantics of com.sun.source.util.TreePath.getPath(TreePath, Tree)). The reason to pass rootPath rather than just the compilation unit is locality: callers pass the path of the tree they are currently visiting, and the target is almost always inside that subtree, so starting the scan there finds it without rescanning the whole unit. On a class with very many top-level members this turns an O(members) rescan per lookup into a local one.

      Parameters:
      rootPath - a path within the compilation unit to search; its leaf's subtree is searched first, and its node chain seeds the path prefix (see the body)
      target - the target tree to look for
      Returns:
      the TreePath corresponding to target, or null if target is not in the compilation unit
    • clear

      public void clear()
      Clear the foundPaths and re-initialize with a new IdentityHashMap, and drop the subtree index and the set of artificial trees. The cache is then for no compilation unit, so the next useUnit(com.sun.source.tree.CompilationUnitTree) clears it again.
    • scan

      public TreePath scan(Tree tree, Tree target)
      Visit one tree as part of a search. Pushes tree onto currentStack for the duration of the visit and allocates no TreePath here; if tree is the target, buildPathForStack() materializes the path and it is thrown out via TreePathCacher.Result. This is the lazy counterpart of eagerly extending a TreePath field on every node.
      Overrides:
      scan in class TreeScanner<TreePath,Tree>