use of java.lang.module.ModuleReference in project junit5 by junit-team.
the class ModuleUtils method scan.
/**
* Scan for classes using the supplied set of module references, class
* filter, and loader.
*/
private static List<Class<?>> scan(Set<ModuleReference> references, ClassFilter filter, ClassLoader loader) {
logger.debug(() -> "Scanning " + references.size() + " module references: " + references);
ModuleReferenceScanner scanner = new ModuleReferenceScanner(filter, loader);
List<Class<?>> classes = new ArrayList<>();
for (ModuleReference reference : references) {
classes.addAll(scanner.scan(reference));
}
logger.debug(() -> "Found " + classes.size() + " classes: " + classes);
return Collections.unmodifiableList(classes);
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class Module method defineModules.
// -- creating Module objects --
/**
* Defines all module in a configuration to the runtime.
*
* @return a map of module name to runtime {@code Module}
*
* @throws IllegalArgumentException
* If defining any of the modules to the VM fails
*/
static Map<String, Module> defineModules(Configuration cf, Function<String, ClassLoader> clf, ModuleLayer layer) {
boolean isBootLayer = (ModuleLayer.boot() == null);
int cap = (int) (cf.modules().size() / 0.75f + 1.0f);
Map<String, Module> nameToModule = new HashMap<>(cap);
Map<String, ClassLoader> nameToLoader = new HashMap<>(cap);
Set<ClassLoader> loaders = new HashSet<>();
boolean hasPlatformModules = false;
// map each module to a class loader
for (ResolvedModule resolvedModule : cf.modules()) {
String name = resolvedModule.name();
ClassLoader loader = clf.apply(name);
nameToLoader.put(name, loader);
if (loader == null || loader == ClassLoaders.platformClassLoader()) {
if (!(clf instanceof ModuleLoaderMap.Mapper)) {
throw new IllegalArgumentException("loader can't be 'null'" + " or the platform class loader");
}
hasPlatformModules = true;
} else {
loaders.add(loader);
}
}
// define each module in the configuration to the VM
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
String name = descriptor.name();
ClassLoader loader = nameToLoader.get(name);
Module m;
if (loader == null && name.equals("java.base")) {
// java.base is already defined to the VM
m = Object.class.getModule();
} else {
URI uri = mref.location().orElse(null);
m = new Module(layer, loader, descriptor, uri);
}
nameToModule.put(name, m);
}
// setup readability and exports/opens
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
String mn = descriptor.name();
Module m = nameToModule.get(mn);
assert m != null;
// reads
Set<Module> reads = new HashSet<>();
// name -> source Module when in parent layer
Map<String, Module> nameToSource = Collections.emptyMap();
for (ResolvedModule other : resolvedModule.reads()) {
Module m2 = null;
if (other.configuration() == cf) {
// this configuration
m2 = nameToModule.get(other.name());
assert m2 != null;
} else {
// parent layer
for (ModuleLayer parent : layer.parents()) {
m2 = findModule(parent, other);
if (m2 != null)
break;
}
assert m2 != null;
if (nameToSource.isEmpty())
nameToSource = new HashMap<>();
nameToSource.put(other.name(), m2);
}
reads.add(m2);
// update VM view
addReads0(m, m2);
}
m.reads = reads;
// automatic modules read all unnamed modules
if (descriptor.isAutomatic()) {
m.implAddReads(ALL_UNNAMED_MODULE, true);
}
// exports and opens, skipped for open and automatic
if (!descriptor.isOpen() && !descriptor.isAutomatic()) {
if (isBootLayer && descriptor.opens().isEmpty()) {
// no open packages, no qualified exports to modules in parent layers
initExports(m, nameToModule);
} else {
initExportsAndOpens(m, nameToSource, nameToModule, layer.parents());
}
}
}
// then register the modules in the class loader's services catalog
if (hasPlatformModules) {
ClassLoader pcl = ClassLoaders.platformClassLoader();
ServicesCatalog bootCatalog = BootLoader.getServicesCatalog();
ServicesCatalog pclCatalog = ServicesCatalog.getServicesCatalog(pcl);
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
if (!descriptor.provides().isEmpty()) {
String name = descriptor.name();
Module m = nameToModule.get(name);
ClassLoader loader = nameToLoader.get(name);
if (loader == null) {
bootCatalog.register(m);
} else if (loader == pcl) {
pclCatalog.register(m);
}
}
}
}
// record that there is a layer with modules defined to the class loader
for (ClassLoader loader : loaders) {
layer.bindToLoader(loader);
}
return nameToModule;
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class ModuleBootstrap method limitFinder.
/**
* Returns a ModuleFinder that limits observability to the given root
* modules, their transitive dependences, plus a set of other modules.
*/
private static ModuleFinder limitFinder(ModuleFinder finder, Set<String> roots, Set<String> otherMods) {
// resolve all root modules
Configuration cf = Configuration.empty().resolve(finder, ModuleFinder.of(), roots);
// module name -> reference
Map<String, ModuleReference> map = new HashMap<>();
// root modules and their transitive dependences
cf.modules().stream().map(ResolvedModule::reference).forEach(mref -> map.put(mref.descriptor().name(), mref));
// additional modules
otherMods.stream().map(finder::find).flatMap(Optional::stream).forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
// set of modules that are observable
Set<ModuleReference> mrefs = new HashSet<>(map.values());
return new ModuleFinder() {
@Override
public Optional<ModuleReference> find(String name) {
return Optional.ofNullable(map.get(name));
}
@Override
public Set<ModuleReference> findAll() {
return mrefs;
}
};
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class ModulePath method scanNextEntry.
/**
* Scans the next entry on the module path. A no-op if all entries have
* already been scanned.
*
* @throws FindException if an error occurs scanning the next entry
*/
private void scanNextEntry() {
if (hasNextEntry()) {
long t0 = System.nanoTime();
Path entry = entries[next];
Map<String, ModuleReference> modules = scan(entry);
next++;
// update cache, ignoring duplicates
int initialSize = cachedModules.size();
for (Map.Entry<String, ModuleReference> e : modules.entrySet()) {
cachedModules.putIfAbsent(e.getKey(), e.getValue());
}
// update counters
int added = cachedModules.size() - initialSize;
moduleCount.add(added);
scanTime.addElapsedTimeFrom(t0);
}
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class ModulePath method scan.
/**
* Scan the given module path entry. If the entry is a directory then it is
* a directory of modules or an exploded module. If the entry is a regular
* file then it is assumed to be a packaged module.
*
* @throws FindException if an error occurs scanning the entry
*/
private Map<String, ModuleReference> scan(Path entry) {
BasicFileAttributes attrs;
try {
attrs = Files.readAttributes(entry, BasicFileAttributes.class);
} catch (NoSuchFileException e) {
return Collections.emptyMap();
} catch (IOException ioe) {
throw new FindException(ioe);
}
try {
if (attrs.isDirectory()) {
Path mi = entry.resolve(MODULE_INFO);
if (!Files.exists(mi)) {
// assume a directory of modules
return scanDirectory(entry);
}
}
// packaged or exploded module
ModuleReference mref = readModule(entry, attrs);
if (mref != null) {
String name = mref.descriptor().name();
return Collections.singletonMap(name, mref);
}
// not recognized
String msg;
if (!isLinkPhase && entry.toString().endsWith(".jmod")) {
msg = "JMOD format not supported at execution time";
} else {
msg = "Module format not recognized";
}
throw new FindException(msg + ": " + entry);
} catch (IOException ioe) {
throw new FindException(ioe);
}
}
Aggregations