use of java.lang.module.ModuleDescriptor.Opens in project Bytecoder by mirkosertic.
the class Module method initExportsAndOpens.
/**
* Initialize/setup a module's exports.
*
* @param m the module
* @param nameToSource map of module name to Module for modules that m reads
* @param nameToModule map of module name to Module for modules in the layer
* under construction
* @param parents the parent layers
*/
private static void initExportsAndOpens(Module m, Map<String, Module> nameToSource, Map<String, Module> nameToModule, List<ModuleLayer> parents) {
ModuleDescriptor descriptor = m.getDescriptor();
Map<String, Set<Module>> openPackages = new HashMap<>();
Map<String, Set<Module>> exportedPackages = new HashMap<>();
// process the open packages first
for (Opens opens : descriptor.opens()) {
String source = opens.source();
if (opens.isQualified()) {
// qualified opens
Set<Module> targets = new HashSet<>();
for (String target : opens.targets()) {
Module m2 = findModule(target, nameToSource, nameToModule, parents);
if (m2 != null) {
addExports0(m, source, m2);
targets.add(m2);
}
}
if (!targets.isEmpty()) {
openPackages.put(source, targets);
}
} else {
// unqualified opens
addExportsToAll0(m, source);
openPackages.put(source, EVERYONE_SET);
}
}
// next the exports, skipping exports when the package is open
for (Exports exports : descriptor.exports()) {
String source = exports.source();
// skip export if package is already open to everyone
Set<Module> openToTargets = openPackages.get(source);
if (openToTargets != null && openToTargets.contains(EVERYONE_MODULE))
continue;
if (exports.isQualified()) {
// qualified exports
Set<Module> targets = new HashSet<>();
for (String target : exports.targets()) {
Module m2 = findModule(target, nameToSource, nameToModule, parents);
if (m2 != null) {
// skip qualified export if already open to m2
if (openToTargets == null || !openToTargets.contains(m2)) {
addExports0(m, source, m2);
targets.add(m2);
}
}
}
if (!targets.isEmpty()) {
exportedPackages.put(source, targets);
}
} else {
// unqualified exports
addExportsToAll0(m, source);
exportedPackages.put(source, EVERYONE_SET);
}
}
if (!openPackages.isEmpty())
m.openPackages = openPackages;
if (!exportedPackages.isEmpty())
m.exportedPackages = exportedPackages;
}
use of java.lang.module.ModuleDescriptor.Opens 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));
}
Aggregations