diff --git a/domino/app/src/main/java/io/quarkus/domino/cli/Quarkus.java b/domino/app/src/main/java/io/quarkus/domino/cli/Quarkus.java index e95d24d5..201ccb1f 100644 --- a/domino/app/src/main/java/io/quarkus/domino/cli/Quarkus.java +++ b/domino/app/src/main/java/io/quarkus/domino/cli/Quarkus.java @@ -20,8 +20,10 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; +import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -116,38 +118,43 @@ public Integer call() throws Exception { } final Map> rootsToMembers = tracePattern == null ? Map.of() : new HashMap<>(); - var treeVisitor = new DependencyTreeVisitor>() { + var treeVisitor = new DependencyTreeVisitor() { @Override - public void visitTree(DependencyTreeVisit> ctx) { + public void visitTree(DependencyTreeVisit ctx) { if (tracePattern != null) { - visitNode(ctx, ctx.getRoot(), new ArrayList<>()); + visitNode(ctx, ctx.getRoot(), new DependencyStack()); } } - private void visitNode(DependencyTreeVisit> ctx, DependencyNode node, List branch) { + private void visitNode(DependencyTreeVisit ctx, DependencyNode node, DependencyStack stack) { var a = node.getArtifact(); - branch.add(a); + stack.push(a); if (tracePattern.contains(a.getGroupId(), a.getArtifactId(), a.getClassifier(), a.getExtension(), a.getVersion())) { - ctx.pushEvent(branch); - branch.remove(branch.size() - 1); + stack.eventPushScheduled = true; + } else if (stack.eventPushScheduled) { + stack.pop(); return; } for (var child : node.getChildren()) { - visitNode(ctx, child, branch); + visitNode(ctx, child, stack); } - branch.remove(branch.size() - 1); + if (stack.eventPushScheduled) { + ctx.pushEvent(stack); + stack.eventPushScheduled = false; + } + stack.pop(); } @Override - public void onEvent(List result, MessageWriter log) { + public void onEvent(DependencyStack stack, MessageWriter log) { if (!rootsToMembers.isEmpty()) { - var a = result.get(0); + var a = stack.peek(); var reports = rootsToMembers.get(ArtifactCoords.of(a.getGroupId(), a.getArtifactId(), a.getClassifier(), a.getExtension(), a.getVersion())); if (reports != null) { - var copy = List.copyOf(result); + var copy = stack.toList(); for (var report : reports) { report.addTracedExtensionDependency(copy); } @@ -463,4 +470,25 @@ boolean hasTraces() { return !tracedExtensionDeps.isEmpty() || !tracedBomConstraints.isEmpty(); } } + + private static class DependencyStack { + final Deque chain = new ArrayDeque<>(); + boolean eventPushScheduled; + + void push(Artifact a) { + chain.addLast(a); + } + + void pop() { + chain.removeLast(); + } + + Artifact peek() { + return chain.peekFirst(); + } + + List toList() { + return List.copyOf(chain); + } + } }