Package org.openrewrite.maven.graph
Class DependencyTreeWalker
java.lang.Object
org.openrewrite.maven.graph.DependencyTreeWalker
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface for handling dependencies during tree traversal.static classCollects dependency match information while walking the dependency tree. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringrenderPath(String scopeOrConfig, Deque<ResolvedDependency> dependencyPath) Renders a dependency path as an inverse tree string for display.static voidwalk(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).static voidwalk(ResolvedDependency root, @Nullable org.openrewrite.semver.DependencyMatcher matcher, DependencyTreeWalker.Callback callback) Walks a single dependency tree, calling the callback for each match.
-
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 frommatcher- the matcher to test dependencies against, or null to visit allcallback- 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 frommatcher- the matcher to test dependencies against, or null to visit allcallback- called for each matching dependency with its path
-
renderPath
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
-