This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch stateless-serializing-dependency-node-visitor
in repository https://gitbox.apache.org/repos/asf/maven-dependency-tree.git

commit 99feeaa74440f962a4b545e8b9d113191030de50
Author: Elliotte Rusty Harold <[email protected]>
AuthorDate: Thu Nov 19 12:37:36 2026 +0000

    Make SerializingDependencyNodeVisitor stateless
---
 .../SerializingDependencyNodeVisitor.java          | 49 ++++---------
 .../SerializingDependencyNodeVisitorTest.java      | 81 ++++++++++++++++++++++
 2 files changed, 95 insertions(+), 35 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java
 
b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java
index 9fbb828..d3955f0 100644
--- 
a/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java
+++ 
b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java
@@ -20,6 +20,7 @@ package org.apache.maven.shared.dependency.graph.traversal;
 
 import java.io.PrintWriter;
 import java.io.Writer;
+import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.maven.shared.dependency.graph.DependencyNode;
@@ -90,11 +91,6 @@ public class SerializingDependencyNodeVisitor implements 
DependencyNodeVisitor {
      */
     private final GraphTokens tokens;
 
-    /**
-     * The depth of the currently visited dependency node.
-     */
-    private int depth;
-
     // constructors -----------------------------------------------------------
 
     /**
@@ -121,8 +117,6 @@ public class SerializingDependencyNodeVisitor implements 
DependencyNodeVisitor {
         }
 
         this.tokens = tokens;
-
-        depth = 0;
     }
 
     // DependencyNodeVisitor methods ------------------------------------------
@@ -132,11 +126,11 @@ public class SerializingDependencyNodeVisitor implements 
DependencyNodeVisitor {
      */
     @Override
     public boolean visit(DependencyNode node) {
-        indent(node);
-
-        writer.println(node.toNodeString());
+        synchronized (writer) {
+            indent(node);
 
-        depth++;
+            writer.println(node.toNodeString());
+        }
 
         return true;
     }
@@ -146,8 +140,6 @@ public class SerializingDependencyNodeVisitor implements 
DependencyNodeVisitor {
      */
     @Override
     public boolean endVisit(DependencyNode node) {
-        depth--;
-
         return true;
     }
 
@@ -159,11 +151,17 @@ public class SerializingDependencyNodeVisitor implements 
DependencyNodeVisitor {
      * @param node the dependency node to indent
      */
     private void indent(DependencyNode node) {
-        for (int i = 1; i < depth; i++) {
-            writer.write(tokens.getFillIndent(isLast(node, i)));
+        List<DependencyNode> ancestors = new ArrayList<>();
+
+        for (DependencyNode parent = node.getParent(); parent != null; parent 
= parent.getParent()) {
+            ancestors.add(parent);
+        }
+
+        for (int i = ancestors.size() - 2; i >= 0; i--) {
+            writer.write(tokens.getFillIndent(isLast(ancestors.get(i))));
         }
 
-        if (depth > 0) {
+        if (!ancestors.isEmpty()) {
             writer.write(tokens.getNodeIndent(isLast(node)));
         }
     }
@@ -191,23 +189,4 @@ public class SerializingDependencyNodeVisitor implements 
DependencyNodeVisitor {
 
         return last;
     }
-
-    /**
-     * Gets whether the specified dependency node ancestor is the last of its 
siblings.
-     *
-     * @param node the dependency node whose ancestor to check
-     * @param ancestorDepth the depth of the ancestor of the specified 
dependency node to check
-     * @return <code>true</code> if the specified dependency node ancestor is 
the last of its siblings
-     */
-    private boolean isLast(DependencyNode node, int ancestorDepth) {
-        // TODO: remove node argument and calculate from visitor calls only
-
-        int distance = depth - ancestorDepth;
-
-        while (distance-- > 0) {
-            node = node.getParent();
-        }
-
-        return isLast(node);
-    }
 }
diff --git 
a/src/test/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitorTest.java
 
b/src/test/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitorTest.java
new file mode 100644
index 0000000..f2f16de
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitorTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.shared.dependency.graph.traversal;
+
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyNode;
+import org.junit.jupiter.api.Test;
+
+import static 
org.apache.maven.shared.dependency.graph.traversal.SerializingDependencyNodeVisitor.STANDARD_TOKENS;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class SerializingDependencyNodeVisitorTest {
+
+    @Test
+    void serializesTreeWithExpectedIndentation() {
+        DefaultDependencyNode root = node("root", null);
+        DefaultDependencyNode firstChild = node("first-child", root);
+        DefaultDependencyNode grandchild = node("grandchild", firstChild);
+        DefaultDependencyNode secondChild = node("second-child", root);
+        root.setChildren(Arrays.asList(firstChild, secondChild));
+        firstChild.setChildren(Collections.singletonList(grandchild));
+        grandchild.setChildren(Collections.emptyList());
+        secondChild.setChildren(Collections.emptyList());
+
+        StringWriter output = new StringWriter();
+        root.accept(new SerializingDependencyNodeVisitor(output, 
STANDARD_TOKENS));
+
+        assertEquals(
+                lines(
+                        "group:root:jar:1.0:compile",
+                        "+- group:first-child:jar:1.0:compile",
+                        "|  \\- group:grandchild:jar:1.0:compile",
+                        "\\- group:second-child:jar:1.0:compile"),
+                output.toString());
+    }
+
+    @Test
+    void interleavedTraversalsDoNotShareDepth() {
+        DefaultDependencyNode firstRoot = node("first", null);
+        DefaultDependencyNode secondRoot = node("second", null);
+        StringWriter output = new StringWriter();
+        SerializingDependencyNodeVisitor visitor = new 
SerializingDependencyNodeVisitor(output, STANDARD_TOKENS);
+
+        visitor.visit(firstRoot);
+        visitor.visit(secondRoot);
+        visitor.endVisit(secondRoot);
+        visitor.endVisit(firstRoot);
+
+        assertEquals(lines("group:first:jar:1.0:compile", 
"group:second:jar:1.0:compile"), output.toString());
+    }
+
+    private static DefaultDependencyNode node(String artifactId, 
DefaultDependencyNode parent) {
+        Artifact artifact = new DefaultArtifact("group", artifactId, "1.0", 
"compile", "jar", "", null);
+        return new DefaultDependencyNode(parent, artifact, null, null, null);
+    }
+
+    private static String lines(String... lines) {
+        return String.join(System.lineSeparator(), lines) + 
System.lineSeparator();
+    }
+}

Reply via email to