/
GradleModuleMetadataParser.java
688 lines (632 loc) · 28.7 KB
/
GradleModuleMetadataParser.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
* 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.ivyresolve.parser;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.artifacts.VersionConstraint;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.Category;
import org.gradle.api.capabilities.Capability;
import org.gradle.api.internal.artifacts.ImmutableModuleIdentifierFactory;
import org.gradle.api.internal.artifacts.ImmutableVersionConstraint;
import org.gradle.api.internal.artifacts.dependencies.DefaultImmutableVersionConstraint;
import org.gradle.api.internal.artifacts.ivyservice.moduleconverter.dependencies.DefaultExcludeRuleConverter;
import org.gradle.api.internal.artifacts.ivyservice.moduleconverter.dependencies.ExcludeRuleConverter;
import org.gradle.api.internal.artifacts.repositories.metadata.MavenImmutableAttributesFactory;
import org.gradle.api.internal.attributes.AttributeValue;
import org.gradle.api.internal.attributes.ImmutableAttributes;
import org.gradle.api.internal.attributes.ImmutableAttributesFactory;
import org.gradle.api.internal.model.NamedObjectInstantiator;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.internal.component.external.model.DefaultShadowedCapability;
import org.gradle.internal.component.external.model.ImmutableCapability;
import org.gradle.internal.component.external.model.MutableComponentVariant;
import org.gradle.internal.component.external.model.MutableModuleComponentResolveMetadata;
import org.gradle.internal.component.model.DefaultIvyArtifactName;
import org.gradle.internal.component.model.ExcludeMetadata;
import org.gradle.internal.component.model.IvyArtifactName;
import org.gradle.internal.resource.local.LocallyAvailableExternalResource;
import org.gradle.internal.snapshot.impl.CoercingStringValueSnapshot;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static com.google.gson.stream.JsonToken.BOOLEAN;
import static com.google.gson.stream.JsonToken.END_ARRAY;
import static com.google.gson.stream.JsonToken.END_OBJECT;
import static com.google.gson.stream.JsonToken.NULL;
import static com.google.gson.stream.JsonToken.NUMBER;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang.StringUtils.capitalize;
public class GradleModuleMetadataParser {
private final static Logger LOGGER = Logging.getLogger(GradleModuleMetadataParser.class);
public static final String FORMAT_VERSION = "1.1";
private final ImmutableAttributesFactory attributesFactory;
private final NamedObjectInstantiator instantiator;
private final ExcludeRuleConverter excludeRuleConverter;
public GradleModuleMetadataParser(ImmutableAttributesFactory attributesFactory, ImmutableModuleIdentifierFactory moduleIdentifierFactory, NamedObjectInstantiator instantiator) {
this.attributesFactory = attributesFactory;
this.instantiator = instantiator;
this.excludeRuleConverter = new DefaultExcludeRuleConverter(moduleIdentifierFactory);
}
public ImmutableAttributesFactory getAttributesFactory() {
return attributesFactory;
}
public NamedObjectInstantiator getInstantiator() {
return instantiator;
}
public void parse(final LocallyAvailableExternalResource resource, final MutableModuleComponentResolveMetadata metadata) {
resource.withContent(inputStream -> {
String version = null;
try {
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, UTF_8));
reader.beginObject();
if (reader.peek() != JsonToken.NAME) {
throw new RuntimeException("Module metadata should contain a 'formatVersion' value.");
}
String name = reader.nextName();
if (!name.equals("formatVersion")) {
throw new RuntimeException(String.format("The 'formatVersion' value should be the first value in a module metadata. Found '%s' instead.", name));
}
if (reader.peek() != JsonToken.STRING) {
throw new RuntimeException("The 'formatVersion' value should have a string value.");
}
version = reader.nextString();
consumeTopLevelElements(reader, metadata);
File file = resource.getFile();
if (!FORMAT_VERSION.equals(version)) {
LOGGER.debug("Unrecognized metadata format version '{}' found in '{}'. Parsing succeeded but it may lead to unexpected resolution results. Try upgrading to a newer version of Gradle", version, file);
}
return null;
} catch (Exception e) {
if (version != null && !FORMAT_VERSION.equals(version)) {
throw new MetaDataParseException("module metadata", resource, String.format("unsupported format version '%s' specified in module metadata. This version of Gradle supports format version %s.", version, FORMAT_VERSION), e);
}
throw new MetaDataParseException("module metadata", resource, e);
}
});
maybeAddEnforcedPlatformVariant(metadata);
}
private void maybeAddEnforcedPlatformVariant(MutableModuleComponentResolveMetadata metadata) {
List<? extends MutableComponentVariant> variants = metadata.getMutableVariants();
if (variants == null || variants.isEmpty()) {
return;
}
for (MutableComponentVariant variant : ImmutableList.copyOf(variants)) {
AttributeValue<String> entry = variant.getAttributes().findEntry(MavenImmutableAttributesFactory.CATEGORY_ATTRIBUTE);
if (entry.isPresent() && Category.REGULAR_PLATFORM.equals(entry.get()) && variant.getCapabilities().isEmpty()) {
// This generates a synthetic enforced platform variant with the same dependencies, similar to what the Maven variant derivation strategy does
ImmutableAttributes enforcedAttributes = attributesFactory.concat(variant.getAttributes(), MavenImmutableAttributesFactory.CATEGORY_ATTRIBUTE, new CoercingStringValueSnapshot(Category.ENFORCED_PLATFORM, instantiator));
Capability enforcedCapability = buildShadowPlatformCapability(metadata.getId());
metadata.addVariant(variant.copy("enforced" + capitalize(variant.getName()), enforcedAttributes, enforcedCapability));
}
}
}
private Capability buildShadowPlatformCapability(ModuleComponentIdentifier componentId) {
return new DefaultShadowedCapability(new ImmutableCapability(
componentId.getGroup(),
componentId.getModule(),
componentId.getVersion()
), "-derived-enforced-platform");
}
private void consumeTopLevelElements(JsonReader reader, MutableModuleComponentResolveMetadata metadata) throws IOException {
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "variants":
consumeVariants(reader, metadata);
break;
case "component":
consumeComponent(reader, metadata);
break;
default:
consumeAny(reader);
break;
}
}
}
private void consumeComponent(JsonReader reader, MutableModuleComponentResolveMetadata metadata) throws IOException {
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
//noinspection SwitchStatementWithTooFewBranches
switch (name) {
case "attributes":
metadata.setAttributes(consumeAttributes(reader));
break;
default:
consumeAny(reader);
break;
}
}
reader.endObject();
}
private void consumeVariants(JsonReader reader, MutableModuleComponentResolveMetadata metadata) throws IOException {
reader.beginArray();
while (reader.peek() != JsonToken.END_ARRAY) {
consumeVariant(reader, metadata);
}
reader.endArray();
}
private void consumeVariant(JsonReader reader, MutableModuleComponentResolveMetadata metadata) throws IOException {
String variantName = null;
ImmutableAttributes attributes = ImmutableAttributes.EMPTY;
List<ModuleFile> files = Collections.emptyList();
List<ModuleDependency> dependencies = Collections.emptyList();
List<ModuleDependencyConstraint> dependencyConstraints = Collections.emptyList();
List<VariantCapability> capabilities = Collections.emptyList();
boolean availableExternally = false;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "name":
variantName = reader.nextString();
break;
case "attributes":
attributes = consumeAttributes(reader);
break;
case "files":
files = consumeFiles(reader);
break;
case "dependencies":
dependencies = consumeDependencies(reader);
break;
case "dependencyConstraints":
dependencyConstraints = consumeDependencyConstraints(reader);
break;
case "capabilities":
capabilities = consumeCapabilities(reader, true);
break;
case "available-at":
availableExternally = true;
dependencies = consumeVariantLocation(reader);
break;
default:
consumeAny(reader);
break;
}
}
assertDefined(reader, "name", variantName);
reader.endObject();
MutableComponentVariant variant = metadata.addVariant(variantName, attributes);
variant.setAvailableExternally(availableExternally);
if (availableExternally) {
if (!dependencyConstraints.isEmpty()) {
throw new RuntimeException("A variant declared with available-at cannot declare dependency constraints");
}
if (!files.isEmpty()) {
throw new RuntimeException("A variant declared with available-at cannot declare files");
}
}
populateVariant(files, dependencies, dependencyConstraints, capabilities, variant);
}
private void populateVariant(List<ModuleFile> files, List<ModuleDependency> dependencies, List<ModuleDependencyConstraint> dependencyConstraints, List<VariantCapability> capabilities, MutableComponentVariant variant) {
for (ModuleFile file : files) {
variant.addFile(file.name, file.uri);
}
for (ModuleDependency dependency : dependencies) {
variant.addDependency(dependency.group, dependency.module, dependency.versionConstraint, dependency.excludes, dependency.reason, dependency.attributes, dependency.requestedCapabilities, dependency.endorsing, dependency.artifact);
}
for (ModuleDependencyConstraint dependencyConstraint : dependencyConstraints) {
variant.addDependencyConstraint(dependencyConstraint.group, dependencyConstraint.module, dependencyConstraint.versionConstraint, dependencyConstraint.reason, dependencyConstraint.attributes);
}
for (VariantCapability capability : capabilities) {
variant.addCapability(capability.group, capability.name, capability.version);
}
}
private List<ModuleDependency> consumeVariantLocation(JsonReader reader) throws IOException {
String url = null;
String group = null;
String module = null;
String version = null;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "url":
url = reader.nextString();
break;
case "group":
group = reader.nextString();
break;
case "module":
module = reader.nextString();
break;
case "version":
version = reader.nextString();
break;
default:
consumeAny(reader);
break;
}
}
assertDefined(reader, "url", url);
assertDefined(reader, "group", group);
assertDefined(reader, "module", module);
assertDefined(reader, "version", version);
reader.endObject();
return ImmutableList.of(new ModuleDependency(group, module, new DefaultImmutableVersionConstraint(version), ImmutableList.of(), null, ImmutableAttributes.EMPTY, Collections.emptyList(), false, null));
}
private List<ModuleDependency> consumeDependencies(JsonReader reader) throws IOException {
List<ModuleDependency> dependencies = new ArrayList<>();
reader.beginArray();
while (reader.peek() != END_ARRAY) {
String group = null;
String module = null;
String reason = null;
ImmutableAttributes attributes = ImmutableAttributes.EMPTY;
VersionConstraint version = DefaultImmutableVersionConstraint.of();
ImmutableList<ExcludeMetadata> excludes = ImmutableList.of();
List<VariantCapability> requestedCapabilities = ImmutableList.of();
IvyArtifactName artifactSelector = null;
boolean endorseStrictVersions = false;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "group":
group = reader.nextString();
break;
case "module":
module = reader.nextString();
break;
case "version":
version = consumeVersion(reader);
break;
case "excludes":
excludes = consumeExcludes(reader);
break;
case "reason":
reason = reader.nextString();
break;
case "attributes":
attributes = consumeAttributes(reader);
break;
case "requestedCapabilities":
requestedCapabilities = consumeCapabilities(reader, false);
break;
case "endorseStrictVersions":
endorseStrictVersions = reader.nextBoolean();
break;
case "thirdPartyCompatibility":
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String compatibilityFeatureName = reader.nextName();
if (compatibilityFeatureName.equals("artifactSelector")) {
artifactSelector = consumeArtifactSelector(reader);
} else {
consumeAny(reader);
}
}
reader.endObject();
break;
default:
consumeAny(reader);
break;
}
}
assertDefined(reader, "group", group);
assertDefined(reader, "module", module);
reader.endObject();
dependencies.add(new ModuleDependency(group, module, version, excludes, reason, attributes, requestedCapabilities, endorseStrictVersions, artifactSelector));
}
reader.endArray();
return dependencies;
}
private IvyArtifactName consumeArtifactSelector(JsonReader reader) throws IOException {
reader.beginObject();
String artifactName = null;
String type = null;
String extension = null;
String classifier = null;
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "name":
artifactName = reader.nextString();
break;
case "type":
type = reader.nextString();
break;
case "extension":
extension = reader.nextString();
break;
case "classifier":
classifier = reader.nextString();
break;
default:
consumeAny(reader);
break;
}
}
assertDefined(reader, "name", artifactName);
assertDefined(reader, "type", type);
reader.endObject();
return new DefaultIvyArtifactName(artifactName, type, extension, classifier);
}
private List<VariantCapability> consumeCapabilities(JsonReader reader, boolean versionRequired) throws IOException {
ImmutableList.Builder<VariantCapability> capabilities = ImmutableList.builder();
reader.beginArray();
while (reader.peek() != END_ARRAY) {
String group = null;
String name = null;
String version = null;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String val = reader.nextName();
switch (val) {
case "group":
group = reader.nextString();
break;
case "name":
name = reader.nextString();
break;
case "version":
if (reader.peek() == NULL) {
reader.nextNull();
} else {
version = reader.nextString();
}
break;
}
}
assertDefined(reader, "group", group);
assertDefined(reader, "name", name);
if (versionRequired) {
assertDefined(reader, "version", version);
}
reader.endObject();
capabilities.add(new VariantCapability(group, name, version));
}
reader.endArray();
return capabilities.build();
}
private List<ModuleDependencyConstraint> consumeDependencyConstraints(JsonReader reader) throws IOException {
List<ModuleDependencyConstraint> dependencies = new ArrayList<>();
reader.beginArray();
while (reader.peek() != END_ARRAY) {
String group = null;
String module = null;
String reason = null;
VersionConstraint version = DefaultImmutableVersionConstraint.of();
ImmutableAttributes attributes = ImmutableAttributes.EMPTY;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "group":
group = reader.nextString();
break;
case "module":
module = reader.nextString();
break;
case "version":
version = consumeVersion(reader);
break;
case "reason":
reason = reader.nextString();
break;
case "attributes":
attributes = consumeAttributes(reader);
break;
default:
consumeAny(reader);
break;
}
}
assertDefined(reader, "group", group);
assertDefined(reader, "module", module);
reader.endObject();
dependencies.add(new ModuleDependencyConstraint(group, module, version, reason, attributes));
}
reader.endArray();
return dependencies;
}
private ImmutableVersionConstraint consumeVersion(JsonReader reader) throws IOException {
String requiredVersion = "";
String preferredVersion = "";
String strictVersion = "";
List<String> rejects = Lists.newArrayList();
reader.beginObject();
while (reader.peek() != END_OBJECT) {
// At this stage, 'strictly' implies 'requires'.
String cst = reader.nextName();
switch (cst) {
case "prefers":
preferredVersion = reader.nextString();
break;
case "requires":
requiredVersion = reader.nextString();
break;
case "strictly":
strictVersion = reader.nextString();
break;
case "rejects":
reader.beginArray();
while (reader.peek() != END_ARRAY) {
rejects.add(reader.nextString());
}
reader.endArray();
break;
default:
consumeAny(reader);
break;
}
}
reader.endObject();
return DefaultImmutableVersionConstraint.of(preferredVersion, requiredVersion, strictVersion, rejects);
}
private ImmutableList<ExcludeMetadata> consumeExcludes(JsonReader reader) throws IOException {
ImmutableList.Builder<ExcludeMetadata> builder = new ImmutableList.Builder<>();
reader.beginArray();
while (reader.peek() != END_ARRAY) {
String group = null;
String module = null;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "group":
group = reader.nextString();
break;
case "module":
module = reader.nextString();
break;
default:
consumeAny(reader);
break;
}
}
reader.endObject();
ExcludeMetadata exclude = excludeRuleConverter.createExcludeRule(group, module);
builder.add(exclude);
}
reader.endArray();
return builder.build();
}
private List<ModuleFile> consumeFiles(JsonReader reader) throws IOException {
List<ModuleFile> files = new ArrayList<>();
reader.beginArray();
while (reader.peek() != END_ARRAY) {
String fileName = null;
String fileUrl = null;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "name":
fileName = reader.nextString();
break;
case "url":
fileUrl = reader.nextString();
break;
default:
consumeAny(reader);
break;
}
}
assertDefined(reader, "name", fileName);
assertDefined(reader, "url", fileUrl);
reader.endObject();
files.add(new ModuleFile(fileName, fileUrl));
}
reader.endArray();
return files;
}
private ImmutableAttributes consumeAttributes(JsonReader reader) throws IOException {
ImmutableAttributes attributes = ImmutableAttributes.EMPTY;
reader.beginObject();
while (reader.peek() != END_OBJECT) {
String attrName = reader.nextName();
if (reader.peek() == BOOLEAN) {
boolean attrValue = reader.nextBoolean();
attributes = attributesFactory.concat(attributes, Attribute.of(attrName, Boolean.class), attrValue);
} else if (reader.peek() == NUMBER) {
Integer attrValue = reader.nextInt();
attributes = attributesFactory.concat(attributes, Attribute.of(attrName, Integer.class), attrValue);
} else {
String attrValue = reader.nextString();
attributes = attributesFactory.concat(attributes, Attribute.of(attrName, String.class), new CoercingStringValueSnapshot(attrValue, instantiator));
}
}
reader.endObject();
return attributes;
}
private void consumeAny(JsonReader reader) throws IOException {
reader.skipValue();
}
private void assertDefined(JsonReader reader, String attribute, String value) {
if (StringUtils.isEmpty(value)) {
String path = reader.getPath();
// remove leading '$', remove last child segment, use '/' as separator
throw new RuntimeException("missing '" + attribute + "' at " + path.substring(1, path.lastIndexOf('.')).replace('.', '/'));
}
}
private static class ModuleFile {
final String name;
final String uri;
ModuleFile(String name, String uri) {
this.name = name;
this.uri = uri;
}
}
private static class ModuleDependency {
final String group;
final String module;
final VersionConstraint versionConstraint;
final ImmutableList<ExcludeMetadata> excludes;
final String reason;
final ImmutableAttributes attributes;
final List<? extends Capability> requestedCapabilities;
final boolean endorsing;
final IvyArtifactName artifact;
ModuleDependency(String group, String module, VersionConstraint versionConstraint, ImmutableList<ExcludeMetadata> excludes, String reason, ImmutableAttributes attributes, List<? extends Capability> requestedCapabilities, boolean endorsing, IvyArtifactName artifact) {
this.group = group;
this.module = module;
this.versionConstraint = versionConstraint;
this.excludes = excludes;
this.reason = reason;
this.attributes = attributes;
this.requestedCapabilities = requestedCapabilities;
this.endorsing = endorsing;
this.artifact = artifact;
}
}
private static class ModuleDependencyConstraint {
final String group;
final String module;
final VersionConstraint versionConstraint;
final String reason;
final ImmutableAttributes attributes;
ModuleDependencyConstraint(String group, String module, VersionConstraint versionConstraint, String reason, ImmutableAttributes attributes) {
this.group = group;
this.module = module;
this.versionConstraint = versionConstraint;
this.reason = reason;
this.attributes = attributes;
}
}
private static class VariantCapability implements Capability {
final String group;
final String name;
final String version;
private VariantCapability(String group, String name, String version) {
this.group = group;
this.name = name;
this.version = version;
}
@Override
public String getGroup() {
return group;
}
@Override
public String getName() {
return name;
}
@Override
public String getVersion() {
return version;
}
}
}