Skip to content

Commit 1677bfc

Browse files
authored
Merge pull request #10 from iherasymenko/master
Polishing
2 parents 2e9d79d + 55c777c commit 1677bfc

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
id("de.jjohannes.extra-java-module-info") version "0.5"
1717
}
1818
19-
// add module information for all direct and transitive depencies that are not modules
19+
// add module information for all direct and transitive dependencies that are not modules
2020
extraJavaModuleInfo {
2121
// failOnMissingModuleInfo.set(false)
2222
module("commons-beanutils-1.9.4.jar", "org.apache.commons.beanutils", "1.9.4") {
@@ -80,3 +80,7 @@ configurations {
8080
}
8181
}
8282
```
83+
84+
## How do I add `provides ... with ...` declarations to the `module-info.class` descriptor?
85+
86+
The plugin will automatically retrofit all the available `META-INF/services/*` descriptors into `module-info.class` for you. The `META-INF/services/*` descriptors will be preserved so that a transformed JAR will continue to work if it is placed on the classpath.

src/main/java/de/jjohannes/gradle/javamodules/ExtraModuleInfoTransform.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@
3333
import java.io.*;
3434
import java.nio.charset.StandardCharsets;
3535
import java.util.LinkedHashMap;
36-
import java.util.LinkedHashSet;
3736
import java.util.Map;
38-
import java.util.Set;
3937
import java.util.jar.*;
4038
import java.util.regex.Pattern;
41-
import java.util.stream.Collectors;
4239
import java.util.zip.ZipEntry;
4340

4441
/**
@@ -144,7 +141,7 @@ private static void addAutomaticModuleName(File originalJar, File moduleJar, Str
144141
private static void addModuleDescriptor(File originalJar, File moduleJar, ModuleInfo moduleInfo) {
145142
try (JarInputStream inputStream = new JarInputStream(new FileInputStream(originalJar))) {
146143
try (JarOutputStream outputStream = newJarOutputStream(new FileOutputStream(moduleJar), inputStream.getManifest())) {
147-
Map<String, Set<String>> providers = copyAndExtractProviders(inputStream, outputStream);
144+
Map<String, String[]> providers = copyAndExtractProviders(inputStream, outputStream);
148145
outputStream.putNextEntry(new JarEntry("module-info.class"));
149146
outputStream.write(addModuleInfo(moduleInfo, providers));
150147
outputStream.closeEntry();
@@ -158,9 +155,9 @@ private static JarOutputStream newJarOutputStream(OutputStream out, Manifest man
158155
return manifest == null ? new JarOutputStream(out) : new JarOutputStream(out, manifest);
159156
}
160157

161-
private static Map<String, Set<String>> copyAndExtractProviders(JarInputStream inputStream, JarOutputStream outputStream) throws IOException {
158+
private static Map<String, String[]> copyAndExtractProviders(JarInputStream inputStream, JarOutputStream outputStream) throws IOException {
162159
JarEntry jarEntry = inputStream.getNextJarEntry();
163-
Map<String, Set<String>> providers = new LinkedHashMap<>();
160+
Map<String, String[]> providers = new LinkedHashMap<>();
164161
while (jarEntry != null) {
165162
byte[] content = inputStream.readAllBytes();
166163
String entryName = jarEntry.getName();
@@ -175,17 +172,18 @@ private static Map<String, Set<String>> copyAndExtractProviders(JarInputStream i
175172
return providers;
176173
}
177174

178-
private static Set<String> extractImplementations(byte[] content) {
175+
private static String[] extractImplementations(byte[] content) {
179176
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content), StandardCharsets.UTF_8))
180177
.lines()
181178
.map(String::trim)
182179
.filter(line -> !line.isEmpty())
183180
.filter(line -> !line.startsWith("#"))
184181
.map(line -> line.replace('.','/'))
185-
.collect(Collectors.toCollection(LinkedHashSet::new));
182+
.distinct()
183+
.toArray(String[]::new);
186184
}
187185

188-
private static byte[] addModuleInfo(ModuleInfo moduleInfo, Map<String, Set<String>> providers) {
186+
private static byte[] addModuleInfo(ModuleInfo moduleInfo, Map<String, String[]> providers) {
189187
ClassWriter classWriter = new ClassWriter(0);
190188
classWriter.visit(Opcodes.V9, Opcodes.ACC_MODULE, "module-info", null, null, null);
191189
ModuleVisitor moduleVisitor = classWriter.visitModule(moduleInfo.getModuleName(), Opcodes.ACC_OPEN, moduleInfo.getModuleVersion());
@@ -202,11 +200,8 @@ private static byte[] addModuleInfo(ModuleInfo moduleInfo, Map<String, Set<Strin
202200
for (String requireName : moduleInfo.getRequiresStatic()) {
203201
moduleVisitor.visitRequire(requireName, Opcodes.ACC_STATIC_PHASE, null);
204202
}
205-
for (Map.Entry<String, Set<String>> entry : providers.entrySet()) {
206-
String provider = entry.getKey();
207-
String[] implementations = entry.getValue()
208-
.toArray(new String[0]);
209-
moduleVisitor.visitProvide(provider, implementations);
203+
for (Map.Entry<String, String[]> entry : providers.entrySet()) {
204+
moduleVisitor.visitProvide(entry.getKey(), entry.getValue());
210205
}
211206
moduleVisitor.visitEnd();
212207
classWriter.visitEnd();

0 commit comments

Comments
 (0)