1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl.scope;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28
29 import org.eclipse.aether.impl.scope.InternalScopeManager;
30 import org.eclipse.aether.scope.DependencyScope;
31 import org.eclipse.aether.scope.ScopeManager;
32 import org.eclipse.aether.util.graph.transformer.ConflictResolver;
33 import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictContext;
34 import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictItem;
35 import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeSelector;
36
37 import static java.util.Objects.requireNonNull;
38
39
40
41
42
43
44
45
46 public final class ManagedScopeSelector extends ScopeSelector {
47 private final DependencyScope systemScope;
48 private final List<DependencyScope> dependencyScopesByWidthDescending;
49
50 public ManagedScopeSelector(InternalScopeManager scopeManager) {
51 requireNonNull(scopeManager, "scopeManager");
52 this.systemScope = scopeManager.getSystemDependencyScope().orElse(null);
53 this.dependencyScopesByWidthDescending =
54 Collections.unmodifiableList(scopeManager.getDependencyScopeUniverse().stream()
55 .sorted(Comparator.comparing(scopeManager::getDependencyScopeWidth)
56 .reversed())
57 .collect(Collectors.toList()));
58 }
59
60 @Override
61 public void selectScope(ConflictContext context) {
62 String scope = context.getWinner().getDependency().getScope();
63 if (systemScope == null || !systemScope.getId().equals(scope)) {
64 scope = chooseEffectiveScope(context.getItems());
65 }
66 context.setScope(scope);
67 }
68
69 private String chooseEffectiveScope(Collection<ConflictItem> items) {
70 Set<String> scopes = new HashSet<>();
71 for (ConflictItem item : items) {
72 if (item.getDepth() <= 1) {
73 return item.getDependency().getScope();
74 }
75 scopes.addAll(item.getScopes());
76 }
77 return chooseEffectiveScope(scopes);
78 }
79
80
81
82
83
84 public String chooseEffectiveScope(Set<String> scopes) {
85 if (scopes.size() > 1 && systemScope != null) {
86 scopes.remove(systemScope.getId());
87 }
88
89 String effectiveScope = "";
90
91 if (scopes.size() == 1) {
92 effectiveScope = scopes.iterator().next();
93 } else {
94 for (DependencyScope dependencyScope : dependencyScopesByWidthDescending) {
95 if (scopes.contains(dependencyScope.getId())) {
96 effectiveScope = dependencyScope.getId();
97 break;
98 }
99 }
100 }
101
102 return effectiveScope;
103 }
104 }