use of java.lang.module.ModuleDescriptor in project Bytecoder by mirkosertic.
the class Loader method initRemotePackageMap.
/**
* Completes initialization of this Loader. This method populates
* remotePackageToLoader with the packages of the remote modules, where
* "remote modules" are the modules read by modules defined to this loader.
*
* @param cf the Configuration containing at least modules to be defined to
* this class loader
*
* @param parentModuleLayers the parent ModuleLayers
*/
public Loader initRemotePackageMap(Configuration cf, List<ModuleLayer> parentModuleLayers) {
for (String name : nameToModule.keySet()) {
ResolvedModule resolvedModule = cf.findModule(name).get();
assert resolvedModule.configuration() == cf;
for (ResolvedModule other : resolvedModule.reads()) {
String mn = other.name();
ClassLoader loader;
if (other.configuration() == cf) {
// loader then the packages are local.
if (pool == null) {
assert nameToModule.containsKey(mn);
continue;
}
loader = pool.loaderFor(mn);
assert loader != null;
} else {
// find the layer for the target module
ModuleLayer layer = parentModuleLayers.stream().map(parent -> findModuleLayer(parent, other.configuration())).flatMap(Optional::stream).findAny().orElseThrow(() -> new InternalError("Unable to find parent layer"));
// boot loader
assert layer.findModule(mn).isPresent();
loader = layer.findLoader(mn);
if (loader == null)
loader = ClassLoaders.platformClassLoader();
}
// find the packages that are exported to the target module
String target = resolvedModule.name();
ModuleDescriptor descriptor = other.reference().descriptor();
for (ModuleDescriptor.Exports e : descriptor.exports()) {
boolean delegate;
if (e.isQualified()) {
// qualified export in same configuration
delegate = (other.configuration() == cf) && e.targets().contains(target);
} else {
// unqualified
delegate = true;
}
if (delegate) {
String pn = e.source();
ClassLoader l = remotePackageToLoader.putIfAbsent(pn, loader);
if (l != null && l != loader) {
throw new IllegalArgumentException("Package " + pn + " cannot be imported from multiple loaders");
}
}
}
}
}
return this;
}
use of java.lang.module.ModuleDescriptor in project Bytecoder by mirkosertic.
the class LauncherHelper method describeModule.
/**
* Called by the launcher to describe a module
*/
static void describeModule(String moduleName) {
initOutput(System.out);
ModuleFinder finder = ModuleBootstrap.limitedFinder();
ModuleReference mref = finder.find(moduleName).orElse(null);
if (mref == null) {
abort(null, "java.launcher.module.error4", moduleName);
}
ModuleDescriptor md = mref.descriptor();
// one-line summary
showModule(mref);
// unqualified exports (sorted by package)
md.exports().stream().filter(e -> !e.isQualified()).sorted(Comparator.comparing(Exports::source)).map(e -> Stream.concat(Stream.of(e.source()), toStringStream(e.modifiers())).collect(Collectors.joining(" "))).forEach(sourceAndMods -> ostream.format("exports %s%n", sourceAndMods));
// dependences
for (Requires r : md.requires()) {
String nameAndMods = Stream.concat(Stream.of(r.name()), toStringStream(r.modifiers())).collect(Collectors.joining(" "));
ostream.format("requires %s", nameAndMods);
finder.find(r.name()).map(ModuleReference::descriptor).filter(ModuleDescriptor::isAutomatic).ifPresent(any -> ostream.print(" automatic"));
ostream.println();
}
// service use and provides
for (String s : md.uses()) {
ostream.format("uses %s%n", s);
}
for (Provides ps : md.provides()) {
String names = ps.providers().stream().collect(Collectors.joining(" "));
ostream.format("provides %s with %s%n", ps.service(), names);
}
// qualified exports
for (Exports e : md.exports()) {
if (e.isQualified()) {
String who = e.targets().stream().collect(Collectors.joining(" "));
ostream.format("qualified exports %s to %s%n", e.source(), who);
}
}
// open packages
for (Opens opens : md.opens()) {
if (opens.isQualified())
ostream.print("qualified ");
String sourceAndMods = Stream.concat(Stream.of(opens.source()), toStringStream(opens.modifiers())).collect(Collectors.joining(" "));
ostream.format("opens %s", sourceAndMods);
if (opens.isQualified()) {
String who = opens.targets().stream().collect(Collectors.joining(" "));
ostream.format(" to %s", who);
}
ostream.println();
}
// non-exported/non-open packages
Set<String> concealed = new TreeSet<>(md.packages());
md.exports().stream().map(Exports::source).forEach(concealed::remove);
md.opens().stream().map(Opens::source).forEach(concealed::remove);
concealed.forEach(p -> ostream.format("contains %s%n", p));
}
use of java.lang.module.ModuleDescriptor in project Bytecoder by mirkosertic.
the class LauncherHelper method showModule.
/**
* Prints a single line with the module name, version and modifiers
*/
private static void showModule(ModuleReference mref) {
ModuleDescriptor md = mref.descriptor();
ostream.print(md.toNameAndVersion());
mref.location().filter(uri -> !isJrt(uri)).ifPresent(uri -> ostream.format(" %s", uri));
if (md.isOpen())
ostream.print(" open");
if (md.isAutomatic())
ostream.print(" automatic");
ostream.println();
}
use of java.lang.module.ModuleDescriptor in project Bytecoder by mirkosertic.
the class ModulePatcher method patchIfNeeded.
/**
* Returns a module reference that interposes on the given module if
* needed. If there are no patches for the given module then the module
* reference is simply returned. Otherwise the patches for the module
* are scanned (to find any new packages) and a new module reference is
* returned.
*
* @throws UncheckedIOException if an I/O error is detected
*/
public ModuleReference patchIfNeeded(ModuleReference mref) {
// if there are no patches for the module then nothing to do
ModuleDescriptor descriptor = mref.descriptor();
String mn = descriptor.name();
List<Path> paths = map.get(mn);
if (paths == null)
return mref;
// Scan the JAR file or directory tree to get the set of packages.
// For automatic modules then packages that do not contain class files
// must be ignored.
Set<String> packages = new HashSet<>();
boolean isAutomatic = descriptor.isAutomatic();
try {
for (Path file : paths) {
if (Files.isRegularFile(file)) {
// is not supported by the boot class loader
try (JarFile jf = new JarFile(file.toString())) {
jf.stream().filter(e -> !e.isDirectory() && (!isAutomatic || e.getName().endsWith(".class"))).map(e -> toPackageName(file, e)).filter(Checks::isPackageName).forEach(packages::add);
}
} else if (Files.isDirectory(file)) {
// exploded directory without following sym links
Path top = file;
Files.find(top, Integer.MAX_VALUE, ((path, attrs) -> attrs.isRegularFile())).filter(path -> (!isAutomatic || path.toString().endsWith(".class")) && !isHidden(path)).map(path -> toPackageName(top, path)).filter(Checks::isPackageName).forEach(packages::add);
}
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
// if there are new packages then we need a new ModuleDescriptor
packages.removeAll(descriptor.packages());
if (!packages.isEmpty()) {
Builder builder = JLMA.newModuleBuilder(descriptor.name(), /*strict*/
false, descriptor.modifiers());
if (!descriptor.isAutomatic()) {
descriptor.requires().forEach(builder::requires);
descriptor.exports().forEach(builder::exports);
descriptor.opens().forEach(builder::opens);
descriptor.uses().forEach(builder::uses);
}
descriptor.provides().forEach(builder::provides);
descriptor.version().ifPresent(builder::version);
descriptor.mainClass().ifPresent(builder::mainClass);
// original + new packages
builder.packages(descriptor.packages());
builder.packages(packages);
descriptor = builder.build();
}
// return a module reference to the patched module
URI location = mref.location().orElse(null);
ModuleTarget target = null;
ModuleHashes recordedHashes = null;
ModuleHashes.HashSupplier hasher = null;
ModuleResolution mres = null;
if (mref instanceof ModuleReferenceImpl) {
ModuleReferenceImpl impl = (ModuleReferenceImpl) mref;
target = impl.moduleTarget();
recordedHashes = impl.recordedHashes();
hasher = impl.hasher();
mres = impl.moduleResolution();
}
return new ModuleReferenceImpl(descriptor, location, () -> new PatchedModuleReader(paths, mref), this, target, recordedHashes, hasher, mres);
}
use of java.lang.module.ModuleDescriptor in project Bytecoder by mirkosertic.
the class ServicesCatalog method register.
/**
* Registers the providers in the given module in this services catalog.
*/
public void register(Module module) {
ModuleDescriptor descriptor = module.getDescriptor();
for (Provides provides : descriptor.provides()) {
String service = provides.service();
List<String> providerNames = provides.providers();
int count = providerNames.size();
if (count == 1) {
String pn = providerNames.get(0);
providers(service).add(new ServiceProvider(module, pn));
} else {
List<ServiceProvider> list = new ArrayList<>(count);
for (String pn : providerNames) {
list.add(new ServiceProvider(module, pn));
}
providers(service).addAll(list);
}
}
}
Aggregations