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.internal.impl.collect;
20
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.eclipse.aether.artifact.Artifact;
26 import org.eclipse.aether.graph.Dependency;
27 import org.eclipse.aether.graph.DependencyCycle;
28 import org.eclipse.aether.graph.DependencyNode;
29 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
30
31 /**
32 * Default implementation of {@link DependencyCycle}.
33 * Internal helper class for collector implementations.
34 */
35 public final class DefaultDependencyCycle implements DependencyCycle {
36 private final List<Dependency> dependencies;
37
38 private final int cycleEntry;
39
40 public DefaultDependencyCycle(List<DependencyNode> nodes, int cycleEntry, Dependency dependency) {
41 // skip root node unless it actually has a dependency or is considered the cycle entry (due to its label)
42 int offset = (cycleEntry > 0 && nodes.get(0).getDependency() == null) ? 1 : 0;
43 Dependency[] dependencies = new Dependency[nodes.size() - offset + 1];
44 for (int i = 0, n = dependencies.length - 1; i < n; i++) {
45 DependencyNode node = nodes.get(i + offset);
46 dependencies[i] = node.getDependency();
47 // when cycle starts at root artifact as opposed to root dependency, synthesize a dependency
48 if (dependencies[i] == null) {
49 dependencies[i] = new Dependency(node.getArtifact(), null);
50 }
51 }
52 dependencies[dependencies.length - 1] = dependency;
53 this.dependencies = Collections.unmodifiableList(Arrays.asList(dependencies));
54 this.cycleEntry = cycleEntry;
55 }
56
57 @Override
58 public List<Dependency> getPrecedingDependencies() {
59 return dependencies.subList(0, cycleEntry);
60 }
61
62 @Override
63 public List<Dependency> getCyclicDependencies() {
64 return dependencies.subList(cycleEntry, dependencies.size());
65 }
66
67 /**
68 * Searches for a node associated with the given artifact. A version of the artifact is not considered during the
69 * search.
70 *
71 * @param nodes a list representing single path in the dependency graph. First element is the root.
72 * @param artifact to find among the parent nodes.
73 * @return the index of the node furthest from the root and associated with the given artifact, or {@literal -1} if
74 * there is no such node.
75 */
76 public static int find(List<DependencyNode> nodes, Artifact artifact) {
77
78 for (int i = nodes.size() - 1; i >= 0; i--) {
79 DependencyNode node = nodes.get(i);
80
81 Artifact a = node.getArtifact();
82 if (a == null) {
83 break;
84 }
85
86 if (!a.getArtifactId().equals(artifact.getArtifactId())) {
87 continue;
88 }
89 if (!a.getGroupId().equals(artifact.getGroupId())) {
90 continue;
91 }
92 if (!a.getExtension().equals(artifact.getExtension())) {
93 continue;
94 }
95 if (!a.getClassifier().equals(artifact.getClassifier())) {
96 continue;
97 }
98 /*
99 * NOTE: While a:1 and a:2 are technically different artifacts, we want to consider the path a:2 -> b:2 ->
100 * a:1 a cycle in the current context. The artifacts themselves might not form a cycle but their producing
101 * projects surely do. Furthermore, conflict resolution will always have to consider a:1 a loser (otherwise
102 * its ancestor a:2 would get pruned and so would a:1) so there is no point in building the sub graph of
103 * a:1.
104 */
105
106 return i;
107 }
108
109 return -1;
110 }
111
112 @Override
113 public String toString() {
114 StringBuilder buffer = new StringBuilder(256);
115 int i = 0;
116 for (Dependency dependency : dependencies) {
117 if (i++ > 0) {
118 buffer.append(" -> ");
119 }
120 buffer.append(ArtifactIdUtils.toVersionlessId(dependency.getArtifact()));
121 }
122 return buffer.toString();
123 }
124 }