Class DependencyTreeWalker

java.lang.Object
org.openrewrite.maven.graph.DependencyTreeWalker

public class DependencyTreeWalker extends Object
Walks a dependency tree lazily, finding dependencies that match a given matcher. Uses depth-first pre-order traversal with cycle detection to avoid infinite loops. Pre-order means parent dependencies are visited before their children.

This class encapsulates the common pattern used by dependency insight recipes to traverse dependency trees without creating intermediate objects for non-matching dependencies, reducing GC pressure on large dependency graphs.

  • Constructor Details

    • DependencyTreeWalker

      public DependencyTreeWalker()
  • Method Details

    • walk

      public static void walk(List<ResolvedDependency> roots, @Nullable org.openrewrite.semver.DependencyMatcher matcher, DependencyTreeWalker.Callback callback)
      Walks the dependency tree starting from the given roots, calling the callback for each dependency that matches the matcher (or all dependencies if matcher is null). Uses caching to avoid re-traversing duplicate dependencies.
      Parameters:
      roots - the direct dependencies to start traversal from
      matcher - the matcher to test dependencies against, or null to visit all
      callback - called for each matching dependency with its path
    • walk

      public static void walk(ResolvedDependency root, @Nullable org.openrewrite.semver.DependencyMatcher matcher, DependencyTreeWalker.Callback callback)
      Walks a single dependency tree, calling the callback for each match.
      Parameters:
      root - the direct dependency to start traversal from
      matcher - the matcher to test dependencies against, or null to visit all
      callback - called for each matching dependency with its path
    • renderPath

      public static String renderPath(String scopeOrConfig, Deque<ResolvedDependency> dependencyPath)
      Renders a dependency path as an inverse tree string for display. Shows the matched dependency at the top, with parents indented below, ending with the scope/configuration.

      Example output for a transitive dependency:

       matched:artifact:1.0
       \--- parent:artifact:2.0
            \--- direct:artifact:3.0
                 \--- compile
       
      Parameters:
      scopeOrConfig - the scope (Maven) or configuration name (Gradle)
      dependencyPath - the path in leaf-to-root order (matched dependency first, direct dependency last)
      Returns:
      formatted inverse dependency tree string