Skip to content

Commit 79e72a4

Browse files
committed
[MNG-8696] Hide the cache from DefaultDependencyResolverResult constructor (apache#2347)
Make package-private the `DefaultDependencyResolverResult` constructor having a `PathModularizationCache` argument, and add a public constructor without the cache argument for code in other packages that need to instantiate. The public constructor may be temporary, until we decide in the future where to store session-wide cache.
1 parent d74e4d6 commit 79e72a4

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ public Severity getSeverity() {
189189
public Optional<DependencyResolverResult> getDependencyResolverResult() {
190190
return Optional.ofNullable(res.getDependencyResolutionResult())
191191
.map(r -> new DefaultDependencyResolverResult(
192-
// TODO: this should not be null
193-
null, null, r.getCollectionErrors(), session.getNode(r.getDependencyGraph()), 0));
192+
null, r.getCollectionErrors(), session.getNode(r.getDependencyGraph()), 0));
194193
}
195194
};
196195
} catch (ProjectBuildingException e) {

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultDependencyResolver.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@
6868
@Singleton
6969
public class DefaultDependencyResolver implements DependencyResolver {
7070

71+
/**
72+
* Cache of information about the modules contained in a path element.
73+
*
74+
* <p><b>TODO:</b> This field should not be in this class, because the cache should be global to the session.
75+
* This field exists here only temporarily, until clarified where to store session-wide caches.</p>
76+
*/
77+
private final PathModularizationCache moduleCache;
78+
79+
/**
80+
* Creates an initially empty resolver.
81+
*/
82+
public DefaultDependencyResolver() {
83+
// TODO: the cache should not be instantiated here, but should rather be session-wide.
84+
moduleCache = new PathModularizationCache();
85+
}
86+
7187
@Nonnull
7288
@Override
7389
public DependencyResolverResult collect(@Nonnull DependencyResolverRequest request)
@@ -126,7 +142,11 @@ public DependencyResolverResult collect(@Nonnull DependencyResolverRequest reque
126142
final CollectResult result =
127143
session.getRepositorySystem().collectDependencies(systemSession, collectRequest);
128144
return new DefaultDependencyResolverResult(
129-
null, null, result.getExceptions(), session.getNode(result.getRoot(), request.getVerbose()), 0);
145+
null,
146+
moduleCache,
147+
result.getExceptions(),
148+
session.getNode(result.getRoot(), request.getVerbose()),
149+
0);
130150
} catch (DependencyCollectionException e) {
131151
throw new DependencyResolverException("Unable to collect dependencies", e);
132152
}
@@ -171,8 +191,8 @@ public DependencyResolverResult resolve(DependencyResolverRequest request)
171191
InternalSession session =
172192
InternalSession.from(requireNonNull(request, "request").getSession());
173193
RequestTraceHelper.ResolverTrace trace = RequestTraceHelper.enter(session, request);
194+
DependencyResolverResult result;
174195
try {
175-
DependencyResolverResult result;
176196
DependencyResolverResult collectorResult = collect(request);
177197
List<RemoteRepository> repositories = request.getRepositories() != null
178198
? request.getRepositories()
@@ -191,18 +211,13 @@ public DependencyResolverResult resolve(DependencyResolverRequest request)
191211
.map(Artifact::toCoordinates)
192212
.collect(Collectors.toList());
193213
Predicate<PathType> filter = request.getPathTypeFilter();
214+
DefaultDependencyResolverResult resolverResult = new DefaultDependencyResolverResult(
215+
null, moduleCache, collectorResult.getExceptions(), collectorResult.getRoot(), nodes.size());
194216
if (request.getRequestType() == DependencyResolverRequest.RequestType.FLATTEN) {
195-
DefaultDependencyResolverResult flattenResult = new DefaultDependencyResolverResult(
196-
null, null, collectorResult.getExceptions(), collectorResult.getRoot(), nodes.size());
197217
for (Node node : nodes) {
198-
flattenResult.addNode(node);
218+
resolverResult.addNode(node);
199219
}
200-
result = flattenResult;
201220
} else {
202-
PathModularizationCache cache =
203-
new PathModularizationCache(); // TODO: should be project-wide cache.
204-
DefaultDependencyResolverResult resolverResult = new DefaultDependencyResolverResult(
205-
null, cache, collectorResult.getExceptions(), collectorResult.getRoot(), nodes.size());
206221
ArtifactResolverResult artifactResolverResult =
207222
session.getService(ArtifactResolver.class).resolve(session, coordinates, repositories);
208223
for (Node node : nodes) {
@@ -217,13 +232,13 @@ public DependencyResolverResult resolve(DependencyResolverRequest request)
217232
throw cannotReadModuleInfo(path, e);
218233
}
219234
}
220-
result = resolverResult;
221235
}
236+
result = resolverResult;
222237
}
223-
return result;
224238
} finally {
225239
RequestTraceHelper.exit(trace);
226240
}
241+
return result;
227242
}
228243

229244
private static DependencyResolverException cannotReadModuleInfo(final Path path, final IOException cause) {

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultDependencyResolverResult.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.LinkedHashMap;
2828
import java.util.List;
2929
import java.util.Map;
30+
import java.util.Objects;
3031
import java.util.Optional;
3132
import java.util.Set;
3233
import java.util.function.Predicate;
@@ -56,6 +57,7 @@ public class DefaultDependencyResolverResult implements DependencyResolverResult
5657
* The corresponding request.
5758
*/
5859
private final DependencyResolverRequest request;
60+
5961
/**
6062
* The exceptions that occurred while building the dependency graph.
6163
*/
@@ -97,6 +99,24 @@ public class DefaultDependencyResolverResult implements DependencyResolverResult
9799
*/
98100
private final PathModularizationCache cache;
99101

102+
/**
103+
* Creates an initially empty result with a temporary cache.
104+
* Callers should add path elements by calls to {@link #addDependency(Node, Dependency, Predicate, Path)}.
105+
*
106+
* <p><b>WARNING: this constructor may be removed in a future Maven release.</b>
107+
* The reason is because {@code DefaultDependencyResolverResult} needs a cache, which should
108+
* preferably be session-wide. How to manage such caches has not yet been clarified.</p>
109+
*
110+
* @param request the corresponding request
111+
* @param exceptions the exceptions that occurred while building the dependency graph
112+
* @param root the root node of the dependency graph
113+
* @param count estimated number of dependencies
114+
*/
115+
public DefaultDependencyResolverResult(
116+
DependencyResolverRequest request, List<Exception> exceptions, Node root, int count) {
117+
this(request, new PathModularizationCache(), exceptions, root, count);
118+
}
119+
100120
/**
101121
* Creates an initially empty result. Callers should add path elements by calls
102122
* to {@link #addDependency(Node, Dependency, Predicate, Path)}.
@@ -107,14 +127,14 @@ public class DefaultDependencyResolverResult implements DependencyResolverResult
107127
* @param root the root node of the dependency graph
108128
* @param count estimated number of dependencies
109129
*/
110-
public DefaultDependencyResolverResult(
130+
DefaultDependencyResolverResult(
111131
DependencyResolverRequest request,
112132
PathModularizationCache cache,
113133
List<Exception> exceptions,
114134
Node root,
115135
int count) {
116136
this.request = request;
117-
this.cache = cache;
137+
this.cache = Objects.requireNonNull(cache);
118138
this.exceptions = exceptions;
119139
this.root = root;
120140
nodes = new ArrayList<>(count);
@@ -350,7 +370,7 @@ public DependencyResolverRequest getRequest() {
350370

351371
@Override
352372
public List<Exception> getExceptions() {
353-
return exceptions;
373+
return Collections.unmodifiableList(exceptions);
354374
}
355375

356376
@Override
@@ -360,22 +380,22 @@ public Node getRoot() {
360380

361381
@Override
362382
public List<Node> getNodes() {
363-
return nodes;
383+
return Collections.unmodifiableList(nodes);
364384
}
365385

366386
@Override
367387
public List<Path> getPaths() {
368-
return paths;
388+
return Collections.unmodifiableList(paths);
369389
}
370390

371391
@Override
372392
public Map<PathType, List<Path>> getDispatchedPaths() {
373-
return dispatchedPaths;
393+
return Collections.unmodifiableMap(dispatchedPaths);
374394
}
375395

376396
@Override
377397
public Map<Dependency, Path> getDependencies() {
378-
return dependencies;
398+
return Collections.unmodifiableMap(dependencies);
379399
}
380400

381401
@Override

0 commit comments

Comments
 (0)