/
AbstractRealisedModuleComponentResolveMetadata.java
238 lines (203 loc) · 9.21 KB
/
AbstractRealisedModuleComponentResolveMetadata.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
/*
* Copyright 2018 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.internal.component.external.model;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.capabilities.CapabilitiesMetadata;
import org.gradle.api.internal.attributes.AttributeContainerInternal;
import org.gradle.api.internal.attributes.ImmutableAttributes;
import org.gradle.internal.Describables;
import org.gradle.internal.DisplayName;
import org.gradle.internal.component.model.ComponentArtifactMetadata;
import org.gradle.internal.component.model.ConfigurationMetadata;
import org.gradle.internal.component.model.ModuleSources;
import org.gradle.internal.component.model.VariantResolveMetadata;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Common base class for the realised versions of {@link ModuleComponentResolveMetadata} implementations.
*
* The realised part is about the application of {@link VariantMetadataRules} which are applied eagerly
* to configuration or variant data.
*
* This type hierarchy is used whenever the {@code ModuleComponentResolveMetadata} needs to outlive
* the build execution.
*/
public abstract class AbstractRealisedModuleComponentResolveMetadata extends AbstractModuleComponentResolveMetadata {
private Optional<ImmutableList<? extends ConfigurationMetadata>> graphVariants;
private final ImmutableMap<String, ConfigurationMetadata> configurations;
public AbstractRealisedModuleComponentResolveMetadata(AbstractRealisedModuleComponentResolveMetadata metadata) {
super(metadata);
this.configurations = metadata.configurations;
}
public AbstractRealisedModuleComponentResolveMetadata(AbstractRealisedModuleComponentResolveMetadata metadata, ModuleSources sources, VariantDerivationStrategy derivationStrategy) {
super(metadata, sources, derivationStrategy);
this.configurations = metadata.configurations;
}
public AbstractRealisedModuleComponentResolveMetadata(AbstractModuleComponentResolveMetadata mutableMetadata, ImmutableList<? extends ComponentVariant> variants,
Map<String, ConfigurationMetadata> configurations) {
super(mutableMetadata, variants);
this.configurations = ImmutableMap.<String, ConfigurationMetadata>builder().putAll(configurations).build();
}
@Override
public VariantMetadataRules getVariantMetadataRules() {
return VariantMetadataRules.noOp();
}
@Override
public Set<String> getConfigurationNames() {
return configurations.keySet();
}
@Nullable
@Override
public ConfigurationMetadata getConfiguration(String name) {
return configurations.get(name);
}
@Override
public Optional<ImmutableList<? extends ConfigurationMetadata>> getVariantsForGraphTraversal() {
if (graphVariants == null) {
graphVariants = buildVariantsForGraphTraversal(getVariants());
}
return graphVariants;
}
private Optional<ImmutableList<? extends ConfigurationMetadata>> buildVariantsForGraphTraversal(List<? extends ComponentVariant> variants) {
if (variants.isEmpty()) {
return maybeDeriveVariants();
}
ImmutableList.Builder<ConfigurationMetadata> configurations = new ImmutableList.Builder<>();
for (ComponentVariant variant : variants) {
configurations.add(new RealisedVariantBackedConfigurationMetadata(getId(), variant, getAttributes(), getAttributesFactory()));
}
return Optional.of(configurations.build());
}
protected static class NameOnlyVariantResolveMetadata implements VariantResolveMetadata {
private final String name;
public NameOnlyVariantResolveMetadata(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public DisplayName asDescribable() {
throw new UnsupportedOperationException("NameOnlyVariantResolveMetadata cannot be used that way");
}
@Override
public AttributeContainerInternal getAttributes() {
throw new UnsupportedOperationException("NameOnlyVariantResolveMetadata cannot be used that way");
}
@Override
public ImmutableList<? extends ComponentArtifactMetadata> getArtifacts() {
throw new UnsupportedOperationException("NameOnlyVariantResolveMetadata cannot be used that way");
}
@Override
public CapabilitiesMetadata getCapabilities() {
throw new UnsupportedOperationException("NameOnlyVariantResolveMetadata cannot be used that way");
}
}
public static class ImmutableRealisedVariantImpl implements ComponentVariant, VariantResolveMetadata {
private final ModuleComponentIdentifier componentId;
private final String name;
private final ImmutableAttributes attributes;
private final ImmutableList<? extends Dependency> dependencies;
private final ImmutableList<? extends DependencyConstraint> dependencyConstraints;
private final ImmutableList<? extends File> files;
private final ImmutableCapabilities capabilities;
private final ImmutableList<GradleDependencyMetadata> dependencyMetadata;
public ImmutableRealisedVariantImpl(ModuleComponentIdentifier componentId, String name, ImmutableAttributes attributes,
ImmutableList<? extends Dependency> dependencies, ImmutableList<? extends DependencyConstraint> dependencyConstraints,
ImmutableList<? extends File> files, ImmutableCapabilities capabilities,
List<GradleDependencyMetadata> dependencyMetadata) {
this.componentId = componentId;
this.name = name;
this.attributes = attributes;
this.dependencies = dependencies;
this.dependencyConstraints = dependencyConstraints;
this.files = files;
this.capabilities = capabilities;
this.dependencyMetadata = ImmutableList.copyOf(dependencyMetadata);
}
@Override
public String getName() {
return name;
}
@Override
public DisplayName asDescribable() {
return Describables.of(componentId, "variant", name);
}
@Override
public ImmutableAttributes getAttributes() {
return attributes;
}
@Override
public ImmutableList<? extends Dependency> getDependencies() {
return dependencies;
}
@Override
public ImmutableList<? extends DependencyConstraint> getDependencyConstraints() {
return dependencyConstraints;
}
public ImmutableList<GradleDependencyMetadata> getDependencyMetadata() {
return dependencyMetadata;
}
@Override
public ImmutableList<? extends File> getFiles() {
return files;
}
@Override
public CapabilitiesMetadata getCapabilities() {
return capabilities;
}
@Override
public ImmutableList<? extends ComponentArtifactMetadata> getArtifacts() {
ImmutableList.Builder<ComponentArtifactMetadata> artifacts = new ImmutableList.Builder<>();
for (ComponentVariant.File file : files) {
artifacts.add(new UrlBackedArtifactMetadata(componentId, file.getName(), file.getUri()));
}
return artifacts.build();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ImmutableRealisedVariantImpl that = (ImmutableRealisedVariantImpl) o;
return Objects.equal(componentId, that.componentId)
&& Objects.equal(name, that.name)
&& Objects.equal(attributes, that.attributes)
&& Objects.equal(dependencies, that.dependencies)
&& Objects.equal(dependencyConstraints, that.dependencyConstraints)
&& Objects.equal(files, that.files);
}
@Override
public int hashCode() {
return Objects.hashCode(componentId,
name,
attributes,
dependencies,
dependencyConstraints,
files);
}
}
}