1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.util.graph.visitor;
20
21 import java.util.Collection;
22 import java.util.function.Consumer;
23 import java.util.function.Function;
24
25 import org.eclipse.aether.graph.DependencyNode;
26
27 /**
28 * A {@link DependencyGraphDumper} that is safe to use on a graph containing cycles: a dependency graph in
29 * {@code FULL} verbosity mode may contain nodes that (directly or transitively) depend on an ancestor of
30 * themselves, which makes the plain {@link DependencyGraphDumper} recurse forever. Wrapping it in
31 * {@link TreeDependencyVisitor} avoids the stack overflow but also hides every node that was already visited
32 * anywhere else in the graph, so a node reached a second time through a different path (a perfectly normal
33 * diamond dependency, not a cycle) is silently dropped from the output as well.
34 * <p>
35 * This dumper only checks the path from the root to the node currently being visited: a node is only treated as
36 * a cycle when it is already one of its own ancestors. Diamonds are still printed in full, and a true cycle is
37 * printed once, marked, with its subtree cut off instead of being recursed into.
38 *
39 * @since 2.0.25
40 */
41 public class CycleAwareDependencyGraphDumper extends DependencyGraphDumper {
42
43 /**
44 * Creates instance with given consumer.
45 *
46 * @param consumer the string consumer, must not be {@code null}
47 */
48 public CycleAwareDependencyGraphDumper(Consumer<String> consumer) {
49 super(consumer);
50 }
51
52 /**
53 * Creates instance with given consumer and decorators.
54 *
55 * @param consumer the string consumer, must not be {@code null}
56 * @param decorators the decorators to apply, must not be {@code null}
57 */
58 public CycleAwareDependencyGraphDumper(
59 Consumer<String> consumer, Collection<Function<DependencyNode, String>> decorators) {
60 super(consumer, decorators);
61 }
62
63 @Override
64 public boolean visitEnter(DependencyNode node) {
65 if (isOnPath(node)) {
66 nodes.push(node);
67 consumer.accept(formatLine(nodes) + " (cycle)");
68 return false;
69 }
70 return super.visitEnter(node);
71 }
72
73 /**
74 * Returns {@code true} if {@code node} is already one of its own ancestors on the path from the root to the
75 * node currently being visited, i.e. descending into it would recurse forever.
76 */
77 private boolean isOnPath(DependencyNode node) {
78 for (DependencyNode ancestor : nodes) {
79 if (ancestor == node) {
80 return true;
81 }
82 }
83 return false;
84 }
85 }