/
EdgeState.java
450 lines (399 loc) · 16.7 KB
/
EdgeState.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
440
441
442
443
444
445
446
447
448
449
450
/*
* 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 org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.component.ComponentSelector;
import org.gradle.api.artifacts.result.ComponentSelectionReason;
import org.gradle.api.artifacts.result.ResolvedVariantResult;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.ModuleExclusions;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeSpec;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.DependencyGraphEdge;
import org.gradle.api.internal.attributes.AttributeMergingException;
import org.gradle.api.internal.attributes.ImmutableAttributes;
import org.gradle.internal.component.local.model.DslOriginDependencyMetadata;
import org.gradle.internal.component.model.ComponentArtifactMetadata;
import org.gradle.internal.component.model.ComponentResolveMetadata;
import org.gradle.internal.component.model.ConfigurationMetadata;
import org.gradle.internal.component.model.DependencyMetadata;
import org.gradle.internal.component.model.ExcludeMetadata;
import org.gradle.internal.component.model.IvyArtifactName;
import org.gradle.internal.resolve.ModuleVersionResolveException;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* Represents the edges in the dependency graph.
*
* A dependency can have the following states:
* 1. Unattached: in this case the state of the dependency is tied to the state of it's associated {@link SelectorState}.
* 2. Attached: in this case the Edge has been connected to actual nodes in the target component. Only possible if the {@link SelectorState} did not fail to resolve.
*/
class EdgeState implements DependencyGraphEdge {
private final DependencyState dependencyState;
private final DependencyMetadata dependencyMetadata;
private final NodeState from;
private final ResolveState resolveState;
private final List<NodeState> targetNodes = Lists.newLinkedList();
private final boolean isTransitive;
private final boolean isConstraint;
private final int hashCode;
private SelectorState selector;
private ModuleVersionResolveException targetNodeSelectionFailure;
private ImmutableAttributes cachedAttributes;
private ExcludeSpec transitiveExclusions;
private ExcludeSpec cachedEdgeExclusions;
private ExcludeSpec cachedExclusions;
private ResolvedVariantResult resolvedVariant;
private boolean unattached;
EdgeState(NodeState from, DependencyState dependencyState, ExcludeSpec transitiveExclusions, ResolveState resolveState) {
this.from = from;
this.dependencyState = dependencyState;
this.dependencyMetadata = dependencyState.getDependency();
// The accumulated exclusions that apply to this edge based on the path from the root
this.transitiveExclusions = transitiveExclusions;
this.resolveState = resolveState;
this.isTransitive = from.isTransitive() && dependencyMetadata.isTransitive();
this.isConstraint = dependencyMetadata.isConstraint();
this.hashCode = computeHashCode();
}
private int computeHashCode() {
int hashCode = from.hashCode();
hashCode = 31 * hashCode + dependencyState.hashCode();
if (transitiveExclusions != null) {
hashCode = 31 * hashCode + transitiveExclusions.hashCode();
}
return hashCode;
}
void computeSelector() {
this.selector = resolveState.getSelector(dependencyState, from.versionProvidedByAncestors(dependencyState));
}
@Override
public String toString() {
return String.format("%s -> %s", from.toString(), dependencyMetadata);
}
@Override
public NodeState getFrom() {
return from;
}
DependencyMetadata getDependencyMetadata() {
return dependencyMetadata;
}
/**
* Returns the target component, if the edge has been successfully resolved.
* Returns null if the edge failed to resolve, or has not (yet) been successfully resolved to a target component.
*/
@Nullable
ComponentState getTargetComponent() {
if (!selector.isResolved() || selector.getFailure() != null) {
return null;
}
return getSelectedComponent();
}
@Override
public SelectorState getSelector() {
return selector;
}
public boolean isTransitive() {
return isTransitive;
}
void attachToTargetConfigurations() {
ComponentState targetComponent = getTargetComponent();
if (targetComponent == null) {
// The selector failed or the module has been deselected. Do not attach.
return;
}
if (isConstraint) {
// Need to double check that the target still has hard edges to it
ModuleResolveState module = targetComponent.getModule();
if (module.isPending()) {
selector.getTargetModule().removeUnattachedDependency(this);
from.makePending(this);
module.addPendingNode(from);
return;
}
}
calculateTargetConfigurations(targetComponent);
for (NodeState targetConfiguration : targetNodes) {
targetConfiguration.addIncomingEdge(this);
}
if (!targetNodes.isEmpty()) {
selector.getTargetModule().removeUnattachedDependency(this);
}
}
void cleanUpOnSourceChange(NodeState source) {
removeFromTargetConfigurations();
maybeDecreaseHardEdgeCount(source);
selector.getTargetModule().removeUnattachedDependency(this);
selector.release();
}
void removeFromTargetConfigurations() {
if (!targetNodes.isEmpty()) {
for (NodeState targetConfiguration : targetNodes) {
targetConfiguration.removeIncomingEdge(this);
}
targetNodes.clear();
}
targetNodeSelectionFailure = null;
}
/**
* Call this method to attach a failure late in the process. This is typically
* done when a failure is caused by graph validation. In that case we want to
* perform as much resolution as possible, still have a valid graph, but in the
* end fail resolution.
*/
void failWith(Throwable err) {
targetNodeSelectionFailure = new ModuleVersionResolveException(dependencyState.getRequested(), err);
}
public void restart(boolean checkUnattached) {
if (from.isSelected()) {
removeFromTargetConfigurations();
// We now have corner cases that can lead to this restart not succeeding
if (checkUnattached && !isUnattached()) {
selector.getTargetModule().addUnattachedDependency(this);
}
attachToTargetConfigurations();
}
}
@Override
public ImmutableAttributes getAttributes() {
assert cachedAttributes != null;
return cachedAttributes;
}
private ImmutableAttributes safeGetAttributes() throws AttributeMergingException {
ModuleResolveState module = selector.getTargetModule();
cachedAttributes = module.mergedConstraintsAttributes(dependencyState.getDependency().getSelector().getAttributes());
return cachedAttributes;
}
private void calculateTargetConfigurations(ComponentState targetComponent) {
ComponentResolveMetadata targetModuleVersion = targetComponent.getMetadata();
targetNodes.clear();
targetNodeSelectionFailure = null;
if (targetModuleVersion == null) {
targetComponent.getModule().getPlatformState().addOrphanEdge(this);
// Broken version
return;
}
if (isConstraint && !isVirtualDependency()) {
List<NodeState> nodes = targetComponent.getNodes();
for (NodeState node : nodes) {
if (node.isSelected()) {
targetNodes.add(node);
}
}
if (targetNodes.isEmpty()) {
// There is a chance we could not attach target configurations previously
List<EdgeState> unattachedDependencies = targetComponent.getModule().getUnattachedDependencies();
if (!unattachedDependencies.isEmpty()) {
for (EdgeState otherEdge : unattachedDependencies) {
if (otherEdge != this && !otherEdge.isConstraint()) {
otherEdge.attachToTargetConfigurations();
if (otherEdge.targetNodeSelectionFailure != null) {
// Copy selection failure
this.targetNodeSelectionFailure = otherEdge.targetNodeSelectionFailure;
return;
}
break;
}
}
}
for (NodeState node : nodes) {
if (node.isSelected()) {
targetNodes.add(node);
}
}
}
return;
}
List<ConfigurationMetadata> targetConfigurations;
try {
ImmutableAttributes attributes = resolveState.getRoot().getMetadata().getAttributes();
attributes = resolveState.getAttributesFactory().concat(attributes, safeGetAttributes());
targetConfigurations = dependencyMetadata.selectConfigurations(attributes, targetModuleVersion, resolveState.getAttributesSchema(), dependencyState.getDependency().getSelector().getRequestedCapabilities());
} catch (AttributeMergingException mergeError) {
targetNodeSelectionFailure = new ModuleVersionResolveException(dependencyState.getRequested(), () -> {
Attribute<?> attribute = mergeError.getAttribute();
Object constraintValue = mergeError.getLeftValue();
Object dependencyValue = mergeError.getRightValue();
return "Inconsistency between attributes of a constraint and a dependency, on attribute '" + attribute + "' : dependency requires '" + dependencyValue + "' while constraint required '" + constraintValue + "'";
});
return;
} catch (Exception t) {
// Failure to select the target variant/configurations from this component, given the dependency attributes/metadata.
targetNodeSelectionFailure = new ModuleVersionResolveException(dependencyState.getRequested(), t);
return;
}
for (ConfigurationMetadata targetConfiguration : targetConfigurations) {
NodeState targetNodeState = resolveState.getNode(targetComponent, targetConfiguration);
this.targetNodes.add(targetNodeState);
}
}
private boolean isVirtualDependency() {
return selector.getDependencyMetadata() instanceof LenientPlatformDependencyMetadata;
}
@Override
public ExcludeSpec getExclusions() {
if (cachedExclusions == null) {
computeExclusions();
}
return cachedExclusions;
}
private void computeExclusions() {
List<ExcludeMetadata> excludes = dependencyMetadata.getExcludes();
if (excludes.isEmpty()) {
cachedExclusions = transitiveExclusions;
} else {
computeExclusionsWhenExcludesPresent(excludes);
}
}
private void computeExclusionsWhenExcludesPresent(List<ExcludeMetadata> excludes) {
ModuleExclusions moduleExclusions = resolveState.getModuleExclusions();
ExcludeSpec edgeExclusions = moduleExclusions.excludeAny(excludes);
cachedExclusions = moduleExclusions.excludeAny(edgeExclusions, transitiveExclusions);
}
ExcludeSpec getEdgeExclusions() {
if (cachedEdgeExclusions == null) {
List<ExcludeMetadata> excludes = dependencyMetadata.getExcludes();
ModuleExclusions moduleExclusions = resolveState.getModuleExclusions();
if (excludes.isEmpty()) {
return moduleExclusions.nothing();
}
cachedEdgeExclusions = moduleExclusions.excludeAny(excludes);
}
return cachedEdgeExclusions;
}
@Override
public boolean contributesArtifacts() {
return !isConstraint;
}
@Override
public ComponentSelector getRequested() {
return resolveState.desugarSelector(dependencyState.getRequested());
}
@Override
public ModuleVersionResolveException getFailure() {
if (targetNodeSelectionFailure != null) {
return targetNodeSelectionFailure;
}
ModuleVersionResolveException selectorFailure = selector.getFailure();
if (selectorFailure != null) {
return selectorFailure;
}
return getSelectedComponent().getMetadataResolveFailure();
}
@Override
public Long getSelected() {
return getSelectedComponent().getResultId();
}
@Override
public boolean isTargetVirtualPlatform() {
ComponentState selectedComponent = getSelectedComponent();
return selectedComponent != null && selectedComponent.getModule().isVirtualPlatform();
}
@Override
@Nullable
public ResolvedVariantResult getSelectedVariant() {
if (resolvedVariant != null) {
return resolvedVariant;
}
List<NodeState> targetNodes = this.targetNodes;
if (targetNodes.isEmpty()) {
// happens for substituted dependencies
ComponentState targetComponent = getTargetComponent();
if (targetComponent != null) {
targetNodes = targetComponent.getNodes();
}
}
assert !targetNodes.isEmpty();
for (NodeState targetNode : targetNodes) {
if (targetNode.isSelected()) {
resolvedVariant = targetNode.getResolvedVariant();
return resolvedVariant;
}
}
return null;
}
@Override
public ComponentSelectionReason getReason() {
return selector.getSelectionReason();
}
@Override
public boolean isConstraint() {
return isConstraint;
}
@Override
public ResolvedVariantResult getFromVariant() {
return from.getResolvedVariant();
}
@Nullable
private ComponentState getSelectedComponent() {
return selector.getTargetModule().getSelected();
}
@Override
public Dependency getOriginalDependency() {
if (dependencyMetadata instanceof DslOriginDependencyMetadata) {
return ((DslOriginDependencyMetadata) dependencyMetadata).getSource();
}
return null;
}
@Override
public List<ComponentArtifactMetadata> getArtifacts(final ConfigurationMetadata targetConfiguration) {
List<IvyArtifactName> artifacts = dependencyMetadata.getArtifacts();
if (artifacts.isEmpty()) {
return Collections.emptyList();
}
return artifacts.stream().map(targetConfiguration::artifact).collect(Collectors.toList());
}
void maybeDecreaseHardEdgeCount(NodeState removalSource) {
if (!isConstraint) {
selector.getTargetModule().decreaseHardEdgeCount(removalSource);
}
}
@Override
public boolean equals(Object o) {
return this == o;
// Edge states are deduplicated, this is a performance optimization
}
@Override
public int hashCode() {
return hashCode;
}
DependencyState getDependencyState() {
return dependencyState;
}
public void updateTransitiveExcludes(ExcludeSpec newResolutionFilter) {
if (isConstraint) {
// Constraint do not carry excludes on a path
return;
}
transitiveExclusions = newResolutionFilter;
cachedExclusions = null;
for (NodeState targetNode : targetNodes) {
targetNode.updateTransitiveExcludes();
}
}
public void markUnattached() {
this.unattached = true;
}
public void markAttached() {
this.unattached = false;
}
public boolean isUnattached() {
return unattached;
}
}