Skip to content

Commit

Permalink
Add system modules implicitly required by the application to the imag…
Browse files Browse the repository at this point in the history
…e builder module graph
  • Loading branch information
ivan-ristovic committed Nov 14, 2024
1 parent 100855c commit 3492831
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.module.Configuration;
import java.lang.module.FindException;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.lang.module.ResolvedModule;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -60,6 +63,7 @@
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -1626,6 +1630,15 @@ protected int buildImage(List<String> javaArgs, LinkedHashSet<Path> cp, LinkedHa
}
List<Path> finalImageModulePath = applicationModules.values().stream().toList();

/*
* Make sure to add all system modules required by the application that might not be part of
* the boot module layer of image builder. If we do not do this, the image builder will fail
* to create the image-build module layer, as it will attempt to define system modules to
* the host VM.
*/
Set<String> implicitlyRequiredSystemModules = getImplicitlyRequiredSystemModules(finalImageModulePath);
addModules.addAll(implicitlyRequiredSystemModules);

if (!addModules.isEmpty()) {

arguments.add("-D" + ModuleSupport.PROPERTY_IMAGE_EXPLICITLY_ADDED_MODULES + "=" +
Expand Down Expand Up @@ -1825,6 +1838,36 @@ private Map<String, Path> getModulesFromPath(Collection<Path> modulePath) {
return mrefs;
}

private Set<String> getImplicitlyRequiredSystemModules(Collection<Path> modulePath) {
if (!config.modulePathBuild || modulePath.isEmpty()) {
return Set.of();
}

ModuleFinder finder = ModuleFinder.of(modulePath.toArray(Path[]::new));
Set<ModuleReference> modules = finder.findAll();

Set<String> systemModules = getBuiltInModules();
Set<String> applicationModulePathRequiredModules = new HashSet<>();

int priorSize;
do {
priorSize = applicationModulePathRequiredModules.size();
for (ModuleReference mref : modules) {
Set<String> requiredModules = getRequiredModules(mref);
applicationModulePathRequiredModules.addAll(requiredModules);
}
} while (priorSize < applicationModulePathRequiredModules.size());

applicationModulePathRequiredModules.retainAll(systemModules);
return applicationModulePathRequiredModules;
}

private Set<String> getRequiredModules(ModuleReference mref) {
return mref.descriptor().requires().stream()
.map(ModuleDescriptor.Requires::name)
.collect(Collectors.toSet());
}

boolean useBundle() {
return bundleSupport != null;
}
Expand Down

0 comments on commit 3492831

Please sign in to comment.