forked from gradlex-org/extra-java-module-info
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtraJavaModuleInfoTransform.java
More file actions
698 lines (636 loc) · 31.2 KB
/
ExtraJavaModuleInfoTransform.java
File metadata and controls
698 lines (636 loc) · 31.2 KB
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
689
690
691
692
693
694
695
696
697
698
// SPDX-License-Identifier: Apache-2.0
package org.gradlex.javamodule.moduleinfo;
import static org.gradlex.javamodule.moduleinfo.FilePathToModuleCoordinates.gaCoordinatesFromFilePathMatch;
import static org.gradlex.javamodule.moduleinfo.FilePathToModuleCoordinates.versionFromFilePath;
import static org.gradlex.javamodule.moduleinfo.ModuleNameUtil.automaticModulNameFromFileName;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import javax.annotation.Nullable;
import org.gradle.api.artifacts.transform.CacheableTransform;
import org.gradle.api.artifacts.transform.InputArtifact;
import org.gradle.api.artifacts.transform.TransformAction;
import org.gradle.api.artifacts.transform.TransformOutputs;
import org.gradle.api.artifacts.transform.TransformParameters;
import org.gradle.api.file.FileSystemLocation;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.Opcodes;
/**
* An artifact transform that applies additional information to Jars without module information.
* The transformation fails the build if a Jar does not contain information and no extra information
* was defined for it. This way we make sure that all Jars are turned into modules.
*/
@CacheableTransform
public abstract class ExtraJavaModuleInfoTransform implements TransformAction<ExtraJavaModuleInfoTransform.Parameter> {
private static final Pattern MODULE_INFO_CLASS_MRJAR_PATH =
Pattern.compile("META-INF/versions/\\d+/module-info.class");
private static final Pattern MRJAR_VERSIONS_PATH = Pattern.compile("META-INF/versions/\\d+/(.*)/.*");
private static final Pattern JAR_SIGNATURE_PATH = Pattern.compile("^META-INF/[^/]+\\.(SF|RSA|DSA|sf|rsa|dsa)$");
private static final String SERVICES_PREFIX = "META-INF/services/";
// See: org.gradle.api.internal.file.archive.ZipCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES
private static final long CONSTANT_TIME_FOR_ZIP_ENTRIES =
new GregorianCalendar(1980, Calendar.FEBRUARY, 1, 0, 0, 0).getTimeInMillis();
public interface Parameter extends TransformParameters {
@Input
MapProperty<String, ModuleSpec> getModuleSpecs();
@Input
Property<Boolean> getFailOnMissingModuleInfo();
@Input
Property<Boolean> getFailOnAutomaticModules();
@Input
Property<Boolean> getFailOnModifiedDerivedModuleNames();
@Input
Property<Boolean> getDeriveAutomaticModuleNamesFromFileNames();
@Input
ListProperty<String> getMergeJarIds();
@InputFiles
@Classpath
ListProperty<RegularFile> getMergeJars();
@Input
MapProperty<String, PublishedMetadata> getRequiresFromMetadata();
@Input
MapProperty<String, String> getAdditionalKnownModules();
}
@InputArtifact
@Classpath
protected abstract Provider<FileSystemLocation> getInputArtifact();
@Override
public void transform(TransformOutputs outputs) {
Parameter parameters = getParameters();
Map<String, ModuleSpec> moduleSpecs = parameters.getModuleSpecs().get();
File originalJar = getInputArtifact().get().getAsFile();
ModuleSpec moduleSpec = findModuleSpec(originalJar);
if (willBeMerged(originalJar, moduleSpecs.values())) { // No output if this Jar will be merged
return;
}
checkInputExists(originalJar);
// We return the original Jar without further analysis, if there is
// (1) no spec (2) no auto-module check (3) no missing module-info check (4) no auto-name derivation
if (moduleSpec == null
&& !getParameters().getFailOnAutomaticModules().get()
&& !getParameters().getFailOnMissingModuleInfo().get()
&& !getParameters().getDeriveAutomaticModuleNamesFromFileNames().get()) {
outputs.file(originalJar);
return;
}
boolean realModule = isModule(originalJar);
if (moduleSpec instanceof ModuleInfo) {
if (realModule && !((ModuleInfo) moduleSpec).patchRealModule) {
throw new RuntimeException(
"Patching of real modules must be explicitly enabled with 'patchRealModule()' or 'preserveExisting()'");
}
String definedName = moduleSpec.getModuleName();
String expectedName = autoModuleName(originalJar);
if (expectedName != null && !definedName.equals(expectedName) && !moduleSpec.overrideModuleName) {
throw new RuntimeException(
"The name '" + definedName + "' is different than the Automatic-Module-Name '" + expectedName
+ "'; explicitly allow override via 'overrideModuleName()'");
}
if (parameters.getFailOnModifiedDerivedModuleNames().get()
&& !realModule
&& expectedName == null
&& !moduleSpec.overrideModuleName) {
String expectedAutomaticNameFromFileName = automaticModulNameFromFileName(originalJar);
if (!definedName.equals(expectedAutomaticNameFromFileName)) {
throw new RuntimeException(
"The name '" + definedName + "' is different than the name derived from the Jar file name '"
+ expectedAutomaticNameFromFileName
+ "'; turn off 'failOnModifiedDerivedModuleNames' or explicitly allow override via 'overrideModuleName()'");
}
}
addModuleDescriptor(originalJar, getModuleJar(outputs, originalJar), (ModuleInfo) moduleSpec);
} else if (moduleSpec instanceof AutomaticModuleName) {
if (realModule) {
throw new RuntimeException(
"Patching of real modules must be explicitly enabled with 'patchRealModule()' and can only be done with 'module()'");
}
String definedName = moduleSpec.getModuleName();
String expectedName = autoModuleName(originalJar);
if (expectedName != null
&& (moduleSpec.getMergedJars().isEmpty() || !definedName.equals(expectedName))
&& !moduleSpec.overrideModuleName) {
throw new RuntimeException("'" + definedName + "' already has the Automatic-Module-Name '"
+ expectedName + "'; explicitly allow override via 'overrideModuleName()'");
}
if (parameters.getFailOnAutomaticModules().get()) {
throw new RuntimeException(
"Use of 'automaticModule()' is prohibited. Use 'module()' instead: " + originalJar.getName());
}
addAutomaticModuleName(originalJar, getModuleJar(outputs, originalJar), (AutomaticModuleName) moduleSpec);
} else if (realModule) {
outputs.file(originalJar);
} else if (autoModuleName(originalJar) != null) {
if (parameters.getFailOnAutomaticModules().get()) {
throw new RuntimeException("Found an automatic module: " + autoModuleName(originalJar) + " ("
+ originalJar.getName() + ")");
}
outputs.file(originalJar);
} else if (parameters.getDeriveAutomaticModuleNamesFromFileNames().get()) {
String automaticName = automaticModulNameFromFileName(originalJar);
addAutomaticModuleName(
originalJar,
getModuleJar(outputs, originalJar),
new AutomaticModuleName(originalJar.getName(), automaticName));
} else if (parameters.getFailOnMissingModuleInfo().get()) {
throw new RuntimeException("Not a module and no mapping defined: " + originalJar.getName());
} else {
outputs.file(originalJar);
}
}
private void checkInputExists(File jar) {
if (!jar.isFile()) {
// If the jar does not exist, it is most likely a locally-built Jar that does not yet exist because the
// transform was triggered at configuration time. See:
// - https://github.com/gradle/gradle/issues/26155
// - https://github.com/gradlex-org/extra-java-module-info/issues/15
// - https://github.com/gradlex-org/extra-java-module-info/issues/78
throw new RuntimeException("File does not exist: " + jar
+ "\n This is likely because a tool or another plugin performs early dependency resolution."
+ "\n You can prevent this error by setting 'skipLocalJars = true'.");
}
}
@Nullable
private ModuleSpec findModuleSpec(File originalJar) {
Map<String, ModuleSpec> moduleSpecs = getParameters().getModuleSpecs().get();
Optional<ModuleSpec> moduleSpec = moduleSpecs.values().stream()
.filter(spec -> gaCoordinatesFromFilePathMatch(originalJar.toPath(), spec.getIdentifier()))
.findFirst();
if (moduleSpec.isPresent()) {
String ga = moduleSpec.get().getIdentifier();
if (moduleSpecs.containsKey(ga)) {
return moduleSpecs.get(ga);
} else {
// maybe with classifier
Stream<String> idsWithClassifier = moduleSpecs.keySet().stream().filter(id -> id.startsWith(ga + "|"));
for (String idWithClassifier : idsWithClassifier.collect(Collectors.toList())) {
if (nameHasClassifier(originalJar, moduleSpecs.get(idWithClassifier))) {
return moduleSpecs.get(idWithClassifier);
}
}
}
return null;
}
String originalJarName = originalJar.getName();
if (moduleSpecs.containsKey(originalJarName)) {
return moduleSpecs.get(originalJarName);
}
return null;
}
private boolean willBeMerged(File originalJar, Collection<ModuleSpec> modules) {
return modules.stream().anyMatch(module -> module.getMergedJars().stream()
.anyMatch(toMerge -> gaCoordinatesFromFilePathMatch(originalJar.toPath(), toMerge)
|| toMerge.equals(originalJar.getName())));
}
private boolean isModule(File jar) {
try (JarInputStream inputStream = new JarInputStream(Files.newInputStream(jar.toPath()))) {
boolean isMultiReleaseJar = containsMultiReleaseJarEntry(inputStream);
ZipEntry next = inputStream.getNextEntry();
while (next != null) {
if ("module-info.class".equals(next.getName())) {
return true;
}
if (isMultiReleaseJar
&& MODULE_INFO_CLASS_MRJAR_PATH.matcher(next.getName()).matches()) {
return true;
}
next = inputStream.getNextEntry();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
private boolean containsMultiReleaseJarEntry(JarInputStream jarStream) {
Manifest manifest = jarStream.getManifest();
return manifest != null
&& Boolean.parseBoolean(manifest.getMainAttributes().getValue("Multi-Release"));
}
@Nullable
private String autoModuleName(File jar) {
try (JarInputStream inputStream = new JarInputStream(Files.newInputStream(jar.toPath()))) {
Manifest manifest = inputStream.getManifest();
return manifest != null ? manifest.getMainAttributes().getValue("Automatic-Module-Name") : null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private File getModuleJar(TransformOutputs outputs, File originalJar) {
return outputs.file(
originalJar.getName().substring(0, originalJar.getName().lastIndexOf('.')) + "-module.jar");
}
private void addAutomaticModuleName(File originalJar, File moduleJar, AutomaticModuleName automaticModule) {
try (JarInputStream inputStream = new JarInputStream(Files.newInputStream(originalJar.toPath()))) {
Manifest manifest = inputStream.getManifest();
if (manifest == null) {
manifest = new Manifest();
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
}
manifest.getMainAttributes().putValue("Automatic-Module-Name", automaticModule.getModuleName());
try (JarOutputStream outputStream =
newJarOutputStream(Files.newOutputStream(moduleJar.toPath()), manifest)) {
Map<String, List<String>> providers = new LinkedHashMap<>();
Set<String> packages = new TreeSet<>();
copyAndExtractProviders(
inputStream,
outputStream,
automaticModule.getRemovedPackages(),
!automaticModule.getMergedJars().isEmpty(),
providers,
packages);
mergeJars(automaticModule, outputStream, providers, packages);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void addModuleDescriptor(File originalJar, File moduleJar, ModuleInfo moduleInfo) {
try (JarInputStream inputStream = new JarInputStream(Files.newInputStream(originalJar.toPath()))) {
try (JarOutputStream outputStream =
newJarOutputStream(Files.newOutputStream(moduleJar.toPath()), inputStream.getManifest())) {
Map<String, List<String>> providers = new LinkedHashMap<>();
Set<String> packages = new TreeSet<>();
byte[] existingModuleInfo = copyAndExtractProviders(
inputStream,
outputStream,
moduleInfo.getRemovedPackages(),
!moduleInfo.getMergedJars().isEmpty(),
providers,
packages);
mergeJars(moduleInfo, outputStream, providers, packages);
outputStream.putNextEntry(newReproducibleEntry("module-info.class"));
if (moduleInfo.exportAllPackages) {
moduleInfo.exportAllPackagesExceptions.forEach(it -> packages.remove(packageToPath(it)));
}
outputStream.write(addModuleInfo(
moduleInfo,
providers,
versionFromFilePath(originalJar.toPath()),
moduleInfo.exportAllPackages ? packages : Collections.emptySet(),
existingModuleInfo));
outputStream.closeEntry();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private JarOutputStream newJarOutputStream(OutputStream out, @Nullable Manifest manifest) throws IOException {
JarOutputStream jar = new JarOutputStream(out);
if (manifest != null) {
ZipEntry e = newReproducibleEntry(JarFile.MANIFEST_NAME);
jar.putNextEntry(e);
manifest.write(new BufferedOutputStream(jar));
jar.closeEntry();
}
return jar;
}
@Nullable
private byte[] copyAndExtractProviders(
JarInputStream inputStream,
JarOutputStream outputStream,
List<String> removedPackages,
boolean willMergeJars,
Map<String, List<String>> providers,
Set<String> packages)
throws IOException {
JarEntry jarEntry = inputStream.getNextJarEntry();
byte[] existingModuleInfo = null;
while (jarEntry != null) {
byte[] content = readAllBytes(inputStream);
String entryName = jarEntry.getName();
boolean isFileInServicesFolder = entryName.startsWith(SERVICES_PREFIX)
&& !entryName.equals(SERVICES_PREFIX)
&& !entryName.substring(SERVICES_PREFIX.length()).contains("/"); // ignore files in sub-folders
if (isFileInServicesFolder) {
String key = entryName.substring(SERVICES_PREFIX.length());
if (!providers.containsKey(key)) {
providers.put(key, new ArrayList<>());
}
providers.get(key).addAll(extractImplementations(content));
}
if (isModuleInfoClass(entryName)) {
existingModuleInfo = content;
} else if (!JAR_SIGNATURE_PATH.matcher(entryName).matches() && !"META-INF/MANIFEST.MF".equals(entryName)) {
if (!willMergeJars || !isFileInServicesFolder) { // service provider files will be merged later
Matcher mrJarMatcher = MRJAR_VERSIONS_PATH.matcher(entryName);
int i = entryName.lastIndexOf("/");
String packagePath =
i > 0 ? mrJarMatcher.matches() ? mrJarMatcher.group(1) : entryName.substring(0, i) : "";
if (!removedPackages.contains(pathToPackage(packagePath))) {
if (entryName.endsWith(".class") && !packagePath.isEmpty()) {
packages.add(packagePath);
}
try {
jarEntry.setCompressedSize(-1);
outputStream.putNextEntry(jarEntry);
outputStream.write(content);
outputStream.closeEntry();
} catch (ZipException e) {
if (!e.getMessage().startsWith("duplicate entry:")) {
throw new RuntimeException(e);
}
}
}
}
}
jarEntry = inputStream.getNextJarEntry();
}
return existingModuleInfo;
}
private List<String> extractImplementations(byte[] content) {
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content), StandardCharsets.UTF_8))
.lines()
.map(String::trim)
.filter(line -> !line.isEmpty())
.filter(line -> !line.startsWith("#"))
.distinct()
.collect(Collectors.toList());
}
private byte[] addModuleInfo(
ModuleInfo moduleInfo,
Map<String, List<String>> providers,
@Nullable String version,
Set<String> autoExportedPackages,
@Nullable byte[] existingModuleInfo) {
ClassReader classReader =
moduleInfo.preserveExisting && existingModuleInfo != null ? new ClassReader(existingModuleInfo) : null;
ClassWriter classWriter = new ClassWriter(classReader, 0);
int openModule = moduleInfo.openModule ? Opcodes.ACC_OPEN : 0;
String moduleVersion = moduleInfo.getModuleVersion() == null ? version : moduleInfo.getModuleVersion();
if (classReader == null) {
classWriter.visit(Opcodes.V9, Opcodes.ACC_MODULE, "module-info", null, null, null);
ModuleVisitor moduleVisitor =
classWriter.visitModule(moduleInfo.getModuleName(), openModule, moduleVersion);
moduleVisitor.visitRequire("java.base", 0, null);
addModuleInfoEntires(moduleInfo, providers, autoExportedPackages, moduleVisitor);
moduleVisitor.visitEnd();
classWriter.visitEnd();
} else {
ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM9, classWriter) {
@Override
public ModuleVisitor visitModule(String name, int access, String version) {
ModuleVisitor moduleVisitor = super.visitModule(name, access, version);
return new ModuleVisitor(Opcodes.ASM9, moduleVisitor) {
@Override
public void visitEnd() {
addModuleInfoEntires(moduleInfo, Collections.emptyMap(), autoExportedPackages, this);
super.visitEnd();
}
};
}
};
classReader.accept(classVisitor, 0);
}
return classWriter.toByteArray();
}
/**
* Convert a Java package name to a path
* @param packageName The package name
* @return The package name converted to a path
*/
private static String packageToPath(String packageName) {
return packageName.replace('.', '/');
}
/**
* Convert a path to a Java package name
* @param path The path
* @return The path converted to a package name
*/
private static String pathToPackage(String path) {
return path.replace('/', '.');
}
private void addModuleInfoEntires(
ModuleInfo moduleInfo,
Map<String, List<String>> providers,
Set<String> autoExportedPackages,
ModuleVisitor moduleVisitor) {
for (String packageName : autoExportedPackages) {
moduleVisitor.visitExport(packageName, 0);
}
for (Map.Entry<String, Set<String>> entry : moduleInfo.exports.entrySet()) {
String packageName = entry.getKey();
Set<String> modules = entry.getValue();
moduleVisitor.visitExport(packageToPath(packageName), 0, modules.toArray(new String[0]));
}
for (Map.Entry<String, Set<String>> entry : moduleInfo.opens.entrySet()) {
String packageName = entry.getKey();
Set<String> modules = entry.getValue();
moduleVisitor.visitOpen(packageToPath(packageName), 0, modules.toArray(new String[0]));
}
if (moduleInfo.requireAllDefinedDependencies) {
String identifier = moduleInfo.getIdentifier();
PublishedMetadata requires =
getParameters().getRequiresFromMetadata().get().get(identifier);
if (requires == null) {
throw new RuntimeException("[requires directives from metadata] " + "Cannot find dependencies for '"
+ moduleInfo.getModuleName() + "'. " + "Are '"
+ moduleInfo.getIdentifier() + "' the correct component coordinates?");
}
if (requires.getErrorMessage() != null) {
throw new RuntimeException("[requires directives from metadata] " + "Cannot read metadata for '"
+ moduleInfo.getModuleName() + "': " + requires.getErrorMessage());
}
for (String ga : requires.getRequires()) {
String depModuleName = gaToModuleName(ga);
moduleVisitor.visitRequire(depModuleName, 0, null);
}
for (String ga : requires.getRequiresTransitive()) {
String depModuleName = gaToModuleName(ga);
moduleVisitor.visitRequire(depModuleName, Opcodes.ACC_TRANSITIVE, null);
}
for (String ga : requires.getRequiresStaticTransitive()) {
String depModuleName = gaToModuleName(ga);
moduleVisitor.visitRequire(depModuleName, Opcodes.ACC_STATIC_PHASE | Opcodes.ACC_TRANSITIVE, null);
}
}
for (String requireName : moduleInfo.requires) {
moduleVisitor.visitRequire(requireName, 0, null);
}
for (String requireName : moduleInfo.requiresTransitive) {
moduleVisitor.visitRequire(requireName, Opcodes.ACC_TRANSITIVE, null);
}
for (String requireName : moduleInfo.requiresStatic) {
moduleVisitor.visitRequire(requireName, Opcodes.ACC_STATIC_PHASE, null);
}
for (String requireName : moduleInfo.requiresStaticTransitive) {
moduleVisitor.visitRequire(requireName, Opcodes.ACC_STATIC_PHASE | Opcodes.ACC_TRANSITIVE, null);
}
for (String usesName : moduleInfo.uses) {
moduleVisitor.visitUse(packageToPath(usesName));
}
for (Map.Entry<String, List<String>> entry : providers.entrySet()) {
String name = entry.getKey();
Set<String> skipSet = moduleInfo.ignoreServiceProviders.get(name);
Set<String> implementations = new LinkedHashSet<>(entry.getValue());
if (skipSet != null) {
if (skipSet.isEmpty()) {
implementations.clear(); // Skip altogether
} else {
implementations.removeAll(skipSet); // Skip some
}
}
if (!implementations.isEmpty()) {
moduleVisitor.visitProvide(
packageToPath(name),
implementations.stream()
.map(ExtraJavaModuleInfoTransform::packageToPath)
.toArray(String[]::new));
}
}
}
private void mergeJars(
ModuleSpec moduleSpec,
JarOutputStream outputStream,
Map<String, List<String>> providers,
Set<String> packages)
throws IOException {
if (moduleSpec.getMergedJars().isEmpty()) {
return;
}
RegularFile mergeJarFile = null;
for (String identifier : moduleSpec.getMergedJars()) {
List<String> ids = getParameters().getMergeJarIds().get();
List<RegularFile> jarFiles = getParameters().getMergeJars().get();
for (int i = 0; i < ids.size(); i++) {
// referenced by 'group:version'
if (ids.get(i).equals(identifier)) {
mergeJarFile = jarFiles.get(i);
break;
}
// referenced by 'jar file name'
if (jarFiles.get(i).getAsFile().getName().equals(identifier)) {
mergeJarFile = jarFiles.get(i);
break;
}
}
if (mergeJarFile != null) {
try (JarInputStream toMergeInputStream = new JarInputStream(
Files.newInputStream(mergeJarFile.getAsFile().toPath()))) {
copyAndExtractProviders(
toMergeInputStream,
outputStream,
moduleSpec.getRemovedPackages(),
true,
providers,
packages);
}
} else {
throw new RuntimeException("Jar not found: " + identifier);
}
}
mergeServiceProviderFiles(outputStream, providers);
}
private void mergeServiceProviderFiles(JarOutputStream outputStream, Map<String, List<String>> providers)
throws IOException {
for (Map.Entry<String, List<String>> provider : providers.entrySet()) {
outputStream.putNextEntry(newReproducibleEntry(SERVICES_PREFIX + provider.getKey()));
for (String implementation : provider.getValue()) {
outputStream.write(implementation.getBytes());
outputStream.write("\n".getBytes());
}
outputStream.closeEntry();
}
}
private JarEntry newReproducibleEntry(String name) {
JarEntry jarEntry = new JarEntry(name);
jarEntry.setTime(CONSTANT_TIME_FOR_ZIP_ENTRIES);
return jarEntry;
}
private byte[] readAllBytes(InputStream inputStream) throws IOException {
final int bufLen = 4 * 0x400;
byte[] buf = new byte[bufLen];
int readLen;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1) {
outputStream.write(buf, 0, readLen);
}
return outputStream.toByteArray();
}
}
private String gaToModuleName(String ga) {
ModuleSpec moduleSpec = getParameters().getModuleSpecs().get().get(ga);
if (moduleSpec != null) {
return moduleSpec.getModuleName();
}
String moduleNameFromSharedMapping = moduleNameFromSharedMapping(ga);
if (moduleNameFromSharedMapping != null) {
return moduleNameFromSharedMapping;
}
throw new RuntimeException(
"[requires directives from metadata] " + "The module name of the following component is not known: "
+ ga + "\n - If it is already a module, make the module name known using 'knownModule(\""
+ ga + "\", \"<module name>\")'"
+ "\n - If it is not a module, patch it using 'module()' or 'automaticModule()'");
}
@Nullable
private String moduleNameFromSharedMapping(String ga) {
Optional<String> foundInCustom = getParameters().getAdditionalKnownModules().get().entrySet().stream()
.filter(e -> e.getValue().equals(ga))
.map(Map.Entry::getKey)
.findFirst();
if (foundInCustom.isPresent()) {
return foundInCustom.get();
}
try {
Class<?> sharedMappings = Class.forName("org.gradlex.javamodule.dependencies.SharedMappings");
@SuppressWarnings("unchecked")
Map<String, String> mappings = (Map<String, String>)
sharedMappings.getDeclaredField("mappings").get(null);
Optional<String> found = mappings.entrySet().stream()
.filter(e -> e.getValue().equals(ga))
.map(Map.Entry::getKey)
.findFirst();
return found.orElse(null);
} catch (ReflectiveOperationException ignored) {
}
return null;
}
private boolean nameHasClassifier(File jar, ModuleSpec spec) {
return jar.getName().endsWith("-" + spec.getClassifier() + ".jar");
}
private static boolean isModuleInfoClass(String jarEntryName) {
return "module-info.class".equals(jarEntryName)
|| MODULE_INFO_CLASS_MRJAR_PATH.matcher(jarEntryName).matches();
}
}