fixed JPMS support
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package net.woggioni.envelope;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.lang.module.Configuration;
|
||||
import java.lang.module.ModuleFinder;
|
||||
import java.lang.module.ResolvedModule;
|
||||
import java.lang.module.ModuleReference;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
@@ -9,9 +13,11 @@ import java.util.Optional;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import net.woggioni.xclassloader.PathClassLoader;
|
||||
import net.woggioni.xclassloader.ModuleClassLoader;
|
||||
import net.woggioni.xclassloader.PathModuleFinder;
|
||||
|
||||
class MainClassLoader {
|
||||
|
||||
@SneakyThrows
|
||||
static Class<?> loadMainClass(Iterable<Path> roots, String mainModuleName, String mainClassName) {
|
||||
if (mainModuleName == null) {
|
||||
@@ -21,13 +27,22 @@ class MainClassLoader {
|
||||
ModuleLayer bootLayer = ModuleLayer.boot();
|
||||
Configuration bootConfiguration = bootLayer.configuration();
|
||||
Configuration cfg = bootConfiguration.resolve(new PathModuleFinder(roots), ModuleFinder.of(), Collections.singletonList(mainModuleName));
|
||||
ClassLoader pathClassLoader = new PathClassLoader(roots, cfg, null);
|
||||
Map<String, ClassLoader> packageMap = new TreeMap<>();
|
||||
ModuleLayer.Controller controller =
|
||||
ModuleLayer.defineModules(cfg, Collections.singletonList(ModuleLayer.boot()), moduleName -> pathClassLoader);
|
||||
ModuleLayer.defineModules(cfg, Collections.singletonList(ModuleLayer.boot()), moduleName -> {
|
||||
ModuleReference modRef = cfg.findModule(moduleName)
|
||||
.map(ResolvedModule::reference)
|
||||
.orElseThrow();
|
||||
ClassLoader cl = new ModuleClassLoader(
|
||||
Collections.unmodifiableMap(packageMap),
|
||||
modRef
|
||||
);
|
||||
for(String packageName : modRef.descriptor().packages()) {
|
||||
packageMap.put(packageName, cl);
|
||||
}
|
||||
return cl;
|
||||
});
|
||||
ModuleLayer layer = controller.layer();
|
||||
for(Module module : layer.modules()) {
|
||||
controller.addReads(module, pathClassLoader.getUnnamedModule());
|
||||
}
|
||||
Module mainModule = layer.findModule(mainModuleName).orElseThrow(
|
||||
() -> new IllegalStateException(String.format("Main module '%s' not found", mainModuleName)));
|
||||
return Optional.ofNullable(mainClassName)
|
||||
|
Reference in New Issue
Block a user