/
ModuleResolveState.java
439 lines (386 loc) · 16.2 KB
/
ModuleResolveState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Copyright 2017 the original author or authors.
*
* Licensed 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.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.builder;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ComponentSelector;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.Version;
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionParser;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.conflicts.CandidateModule;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.selectors.SelectorStateResolver;
import org.gradle.api.internal.attributes.AttributeContainerInternal;
import org.gradle.api.internal.attributes.AttributeMergingException;
import org.gradle.api.internal.attributes.ImmutableAttributes;
import org.gradle.api.internal.attributes.ImmutableAttributesFactory;
import org.gradle.internal.component.model.DependencyMetadata;
import org.gradle.internal.component.model.ForcingDependencyMetadata;
import org.gradle.internal.id.IdGenerator;
import org.gradle.internal.resolve.resolver.ComponentMetaDataResolver;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Resolution state for a given module.
*/
class ModuleResolveState implements CandidateModule {
private final ComponentMetaDataResolver metaDataResolver;
private final IdGenerator<Long> idGenerator;
private final ModuleIdentifier id;
private final List<EdgeState> unattachedDependencies = new LinkedList<>();
private final Map<ModuleVersionIdentifier, ComponentState> versions = new LinkedHashMap<>();
private final ModuleSelectors<SelectorState> selectors;
private final ImmutableAttributesFactory attributesFactory;
private final Comparator<Version> versionComparator;
private final VersionParser versionParser;
final ResolveOptimizations resolveOptimizations;
private SelectorStateResolver<ComponentState> selectorStateResolver;
private final PendingDependencies pendingDependencies;
private ComponentState selected;
private ImmutableAttributes mergedConstraintAttributes = ImmutableAttributes.EMPTY;
private AttributeMergingException attributeMergingError;
private VirtualPlatformState platformState;
private boolean overriddenSelection;
private Set<VirtualPlatformState> platformOwners;
private boolean replaced = false;
private boolean changingSelection;
ModuleResolveState(IdGenerator<Long> idGenerator,
ModuleIdentifier id,
ComponentMetaDataResolver metaDataResolver,
ImmutableAttributesFactory attributesFactory,
Comparator<Version> versionComparator,
VersionParser versionParser,
SelectorStateResolver<ComponentState> selectorStateResolver,
ResolveOptimizations resolveOptimizations) {
this.idGenerator = idGenerator;
this.id = id;
this.metaDataResolver = metaDataResolver;
this.attributesFactory = attributesFactory;
this.versionComparator = versionComparator;
this.versionParser = versionParser;
this.resolveOptimizations = resolveOptimizations;
this.pendingDependencies = new PendingDependencies(id);
this.selectorStateResolver = selectorStateResolver;
selectors = new ModuleSelectors<SelectorState>(versionComparator);
}
void setSelectorStateResolver(SelectorStateResolver<ComponentState> selectorStateResolver) {
this.selectorStateResolver = selectorStateResolver;
}
void registerPlatformOwner(VirtualPlatformState owner) {
if (platformOwners == null) {
platformOwners = Sets.newHashSetWithExpectedSize(1);
}
platformOwners.add(owner);
}
public Set<VirtualPlatformState> getPlatformOwners() {
return platformOwners == null ? Collections.emptySet() : platformOwners;
}
@Override
public String toString() {
return id.toString();
}
@Override
public ModuleIdentifier getId() {
return id;
}
@Override
public Collection<ComponentState> getVersions() {
if (this.versions.isEmpty()) {
return Collections.emptyList();
}
Collection<ComponentState> values = this.versions.values();
if (areAllCandidatesForSelection(values)) {
return values;
}
List<ComponentState> versions = Lists.newArrayListWithCapacity(values.size());
for (ComponentState componentState : values) {
if (componentState.isCandidateForConflictResolution()) {
versions.add(componentState);
}
}
return versions;
}
public Collection<ComponentState> getAllVersions() {
return this.versions.values();
}
private static boolean areAllCandidatesForSelection(Collection<ComponentState> values) {
boolean allCandidates = true;
for (ComponentState value : values) {
if (!value.isCandidateForConflictResolution()) {
allCandidates = false;
break;
}
}
return allCandidates;
}
@Nullable
public ComponentState getSelected() {
return selected;
}
/**
* Selects the target component for this module for the first time.
* Any existing versions will be evicted.
*/
public void select(ComponentState selected) {
assert this.selected == null;
this.selected = selected;
this.replaced = false;
selectComponentAndEvictOthers(selected);
}
private void selectComponentAndEvictOthers(ComponentState selected) {
for (ComponentState version : versions.values()) {
version.evict();
}
selected.select();
}
public boolean isChangingSelection() {
return changingSelection;
}
/**
* Changes the selected target component for this module.
*/
private void changeSelection(ComponentState newSelection) {
assert this.selected != null;
assert newSelection != null;
assert this.selected != newSelection;
assert newSelection.getModule() == this;
changingSelection = true;
// Remove any outgoing edges for the current selection
selected.removeOutgoingEdges();
this.selected = newSelection;
this.replaced = false;
doRestart(newSelection);
changingSelection = false;
}
/**
* Clears the current selection for the module, to prepare for conflict resolution.
* - For the current selection, disconnect and remove any outgoing dependencies.
* - Make all 'selected' component versions selectable.
*/
public void clearSelection() {
if (selected != null) {
selected.removeOutgoingEdges();
}
for (ComponentState version : versions.values()) {
if (version.isSelected()) {
version.makeSelectable();
}
}
selected = null;
replaced = false;
}
/**
* Overrides the component selection for this module, when this module has been replaced by another.
*/
@Override
public void replaceWith(ComponentState selected) {
if (this.selected != null) {
clearSelection();
}
assert this.selected == null;
assert selected != null;
if (!selected.getId().getModule().equals(getId())) {
this.overriddenSelection = true;
}
this.selected = selected;
this.replaced = computeReplaced(selected);
doRestart(selected);
}
private boolean computeReplaced(ComponentState selected) {
// This module might be resolved to a different module, through replacedBy
return !selected.getId().getModule().equals(getId());
}
private void doRestart(ComponentState selected) {
selectComponentAndEvictOthers(selected);
for (ComponentState version : versions.values()) {
version.restartIncomingEdges(selected);
}
for (SelectorState selector : selectors) {
selector.overrideSelection(selected);
}
if (!unattachedDependencies.isEmpty()) {
restartUnattachedDependencies();
}
}
private void restartUnattachedDependencies() {
if (unattachedDependencies.size() == 1) {
EdgeState singleDependency = unattachedDependencies.get(0);
singleDependency.restart(false);
} else {
for (EdgeState dependency : new ArrayList<>(unattachedDependencies)) {
dependency.restart(false);
}
}
}
public void addUnattachedDependency(EdgeState edge) {
unattachedDependencies.add(edge);
edge.markUnattached();
}
public void removeUnattachedDependency(EdgeState edge) {
if (unattachedDependencies.remove(edge)) {
edge.markAttached();
}
}
public ComponentState getVersion(ModuleVersionIdentifier id, ComponentIdentifier componentIdentifier) {
ComponentState moduleRevision = versions.get(id);
if (moduleRevision == null) {
moduleRevision = new ComponentState(idGenerator.generateId(), this, id, componentIdentifier, metaDataResolver);
versions.put(id, moduleRevision);
}
return moduleRevision;
}
void addSelector(SelectorState selector, boolean deferSelection) {
selectors.add(selector, deferSelection);
mergedConstraintAttributes = appendAttributes(mergedConstraintAttributes, selector);
if (overriddenSelection) {
assert selected != null : "An overridden module cannot have selected == null";
selector.overrideSelection(selected);
}
}
void removeSelector(SelectorState selector) {
selectors.remove(selector);
boolean alreadyReused = selector.markForReuse();
mergedConstraintAttributes = ImmutableAttributes.EMPTY;
for (SelectorState selectorState : selectors) {
mergedConstraintAttributes = appendAttributes(mergedConstraintAttributes, selectorState);
}
if (!alreadyReused && selectors.size() != 0) {
maybeUpdateSelection();
}
}
public ModuleSelectors<SelectorState> getSelectors() {
return selectors;
}
List<EdgeState> getUnattachedDependencies() {
return unattachedDependencies;
}
ImmutableAttributes mergedConstraintsAttributes(AttributeContainer append) throws AttributeMergingException {
if (attributeMergingError != null) {
throw new IllegalStateException(IncompatibleDependencyAttributesMessageBuilder.buildMergeErrorMessage(this, attributeMergingError));
}
ImmutableAttributes attributes = ((AttributeContainerInternal) append).asImmutable();
if (mergedConstraintAttributes.isEmpty()) {
return attributes;
}
return attributesFactory.safeConcat(mergedConstraintAttributes.asImmutable(), attributes);
}
private ImmutableAttributes appendAttributes(ImmutableAttributes dependencyAttributes, SelectorState selectorState) {
try {
DependencyMetadata dependencyMetadata = selectorState.getDependencyMetadata();
boolean constraint = dependencyMetadata.isConstraint();
if (constraint) {
ComponentSelector selector = dependencyMetadata.getSelector();
ImmutableAttributes attributes = ((AttributeContainerInternal) selector.getAttributes()).asImmutable();
dependencyAttributes = attributesFactory.safeConcat(attributes, dependencyAttributes);
}
} catch (AttributeMergingException e) {
attributeMergingError = e;
}
return dependencyAttributes;
}
Set<EdgeState> getIncomingEdges() {
Set<EdgeState> incoming = Sets.newLinkedHashSet();
if (selected != null) {
for (NodeState nodeState : selected.getNodes()) {
incoming.addAll(nodeState.getIncomingEdges());
}
}
return incoming;
}
VirtualPlatformState getPlatformState() {
if (platformState == null) {
platformState = new VirtualPlatformState(versionComparator, versionParser, this, resolveOptimizations);
}
return platformState;
}
boolean isVirtualPlatform() {
return platformState != null && !platformState.getParticipatingModules().isEmpty();
}
void decreaseHardEdgeCount(NodeState removalSource) {
pendingDependencies.decreaseHardEdgeCount();
if (pendingDependencies.isPending()) {
// Back to being a pending dependency
// Clear remaining incoming edges, as they must be all from constraints
if (selected != null) {
for (NodeState node : selected.getNodes()) {
node.clearConstraintEdges(pendingDependencies, removalSource);
}
}
}
}
boolean isPending() {
return pendingDependencies.isPending();
}
PendingDependencies getPendingDependencies() {
return pendingDependencies;
}
void addPendingNode(NodeState node) {
pendingDependencies.addNode(node);
}
public void maybeUpdateSelection() {
if (replaced) {
// Never update selection for a replaced module
return;
}
if (selectors.checkDeferSelection()) {
// Selection deferred as we know another selector will be added soon
return;
}
ComponentState newSelected = selectorStateResolver.selectBest(getId(), selectors);
newSelected.setSelectors(selectors);
if (selected == null) {
select(newSelected);
} else if (newSelected != selected) {
changeSelection(newSelected);
}
}
void maybeCreateVirtualMetadata(ResolveState resolveState) {
for (ComponentState componentState : versions.values()) {
if (componentState.getMetadata() == null) {
// TODO LJA Using the root as the NodeState here is a bit of a cheat, investigate if we can track the proper NodeState
componentState.setMetadata(new LenientPlatformResolveMetadata((ModuleComponentIdentifier) componentState.getComponentId(), componentState.getId(), platformState, resolveState.getRoot(), resolveState));
}
}
}
@Nullable
String maybeFindForcedPlatformVersion() {
ComponentState selected = getSelected();
for (NodeState node : selected.getNodes()) {
if (node.isSelected()) {
for (EdgeState incomingEdge : node.getIncomingEdges()) {
DependencyMetadata dependencyMetadata = incomingEdge.getDependencyMetadata();
if (!(dependencyMetadata instanceof LenientPlatformDependencyMetadata) && dependencyMetadata instanceof ForcingDependencyMetadata) {
if (((ForcingDependencyMetadata) dependencyMetadata).isForce()) {
return selected.getVersion();
}
}
}
}
}
return null;
}
}