Skip to content

Commit c057d0c

Browse files
committed
Use shared mappings from 'java-module-dependencies' for 'known modules'
1 parent 120812e commit c057d0c

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Extra Java Module Info Gradle Plugin - Changelog
22

3+
## Version 1.6.2
4+
* [New] - Use shared mappings from 'java-module-dependencies' for 'known modules' (if available)
5+
36
## Version 1.6.1
47
* [Fixed] [#89](https://github.com/gradlex-org/extra-java-module-info/issues/89) - Make Jar patching reproducible
58

src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoTransform.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,31 @@ private byte[] readAllBytes(InputStream inputStream) throws IOException {
494494

495495
private String gaToModuleName(String ga) {
496496
ModuleSpec moduleSpec = getParameters().getModuleSpecs().get().get(ga);
497-
if (moduleSpec == null) {
498-
throw new RuntimeException("[requires directives from metadata] " +
499-
"The module name of the following component is not known: " + ga +
500-
"\n - If it is already a module, make the module name known using 'knownModule(\"" + ga + "\", \"<module name>\")'" +
501-
"\n - If it is not a module, patch it using 'module()' or 'automaticModule()'");
497+
if (moduleSpec != null) {
498+
return moduleSpec.getModuleName();
502499
}
503-
return moduleSpec.getModuleName();
500+
String moduleNameFromSharedMapping = moduleNameFromSharedMapping(ga);
501+
if (moduleNameFromSharedMapping != null) {
502+
return moduleNameFromSharedMapping;
503+
}
504+
505+
throw new RuntimeException("[requires directives from metadata] " +
506+
"The module name of the following component is not known: " + ga +
507+
"\n - If it is already a module, make the module name known using 'knownModule(\"" + ga + "\", \"<module name>\")'" +
508+
"\n - If it is not a module, patch it using 'module()' or 'automaticModule()'");
509+
}
510+
511+
@Nullable
512+
private String moduleNameFromSharedMapping(String ga) {
513+
try {
514+
Class<?> sharedMappings = Class.forName("org.gradlex.javamodule.dependencies.SharedMappings");
515+
@SuppressWarnings("unchecked")
516+
Map<String, String> mappings = (Map<String, String>) sharedMappings.getDeclaredField("mappings").get(null);
517+
Optional<String> found = mappings.entrySet().stream().filter(
518+
e -> e.getValue().equals(ga)).map(Map.Entry::getKey).findFirst();
519+
return found.orElse(null);
520+
} catch (ReflectiveOperationException ignored) { }
521+
return null;
504522
}
505523

506524
private static boolean isModuleInfoClass(String jarEntryName) {

0 commit comments

Comments
 (0)