Search in sources :

Example 6 with Configuration

use of java.lang.module.Configuration 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;
}
Also used : AccessControlContext(java.security.AccessControlContext) Enumeration(java.util.Enumeration) URL(java.net.URL) Spliterators(java.util.Spliterators) ModuleDescriptor(java.lang.module.ModuleDescriptor) ModuleReference(java.lang.module.ModuleReference) HashMap(java.util.HashMap) ModuleReader(java.lang.module.ModuleReader) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) CodeSigner(java.security.CodeSigner) ArrayList(java.util.ArrayList) ResolvedModule(java.lang.module.ResolvedModule) FilePermission(java.io.FilePermission) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) URI(java.net.URI) Resources(jdk.internal.module.Resources) SecureClassLoader(java.security.SecureClassLoader) PrivilegedActionException(java.security.PrivilegedActionException) Iterator(java.util.Iterator) SharedSecrets(jdk.internal.misc.SharedSecrets) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Configuration(java.lang.module.Configuration) PermissionCollection(java.security.PermissionCollection) PrivilegedAction(java.security.PrivilegedAction) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Permission(java.security.Permission) Optional(java.util.Optional) CodeSource(java.security.CodeSource) AccessController(java.security.AccessController) Collections(java.util.Collections) Spliterator(java.util.Spliterator) ModuleDescriptor(java.lang.module.ModuleDescriptor) ResolvedModule(java.lang.module.ResolvedModule) Optional(java.util.Optional) SecureClassLoader(java.security.SecureClassLoader)

Example 7 with Configuration

use of java.lang.module.Configuration in project Bytecoder by mirkosertic.

the class NamedPackage method location.

/**
 * Returns the location of the module if this named package is in
 * a named module; otherwise, returns null.
 */
URI location() {
    if (module.isNamed() && module.getLayer() != null) {
        Configuration cf = module.getLayer().configuration();
        ModuleReference mref = cf.findModule(module.getName()).get().reference();
        return mref.location().orElse(null);
    }
    return null;
}
Also used : Configuration(java.lang.module.Configuration) ModuleReference(java.lang.module.ModuleReference)

Example 8 with Configuration

use of java.lang.module.Configuration in project Bytecoder by mirkosertic.

the class ModuleBootstrap method createBootLayerForValidation.

/**
 * Create a boot module layer for validation that resolves all
 * system modules.
 */
private static ModuleLayer createBootLayerForValidation() {
    Set<String> allSystem = ModuleFinder.ofSystem().findAll().stream().map(ModuleReference::descriptor).map(ModuleDescriptor::name).collect(Collectors.toSet());
    Configuration cf = SharedSecrets.getJavaLangModuleAccess().resolveAndBind(ModuleFinder.ofSystem(), allSystem, null);
    Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
    return ModuleLayer.empty().defineModules(cf, clf);
}
Also used : Configuration(java.lang.module.Configuration) ModuleReference(java.lang.module.ModuleReference) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader)

Example 9 with Configuration

use of java.lang.module.Configuration in project junit5 by junit-team.

the class ModuleUtils method streamResolvedModules.

/**
 * Stream resolved modules from the supplied layer.
 */
private static Stream<ResolvedModule> streamResolvedModules(Predicate<String> moduleNamePredicate, ModuleLayer layer) {
    logger.debug(() -> "Streaming modules for layer @" + System.identityHashCode(layer) + ": " + layer);
    Configuration configuration = layer.configuration();
    logger.debug(() -> "Module layer configuration: " + configuration);
    Stream<ResolvedModule> stream = configuration.modules().stream();
    return stream.filter(module -> moduleNamePredicate.test(module.name()));
}
Also used : Configuration(java.lang.module.Configuration) ResolvedModule(java.lang.module.ResolvedModule)

Aggregations

Configuration (java.lang.module.Configuration)9 ModuleReference (java.lang.module.ModuleReference)7 ResolvedModule (java.lang.module.ResolvedModule)6 ModuleDescriptor (java.lang.module.ModuleDescriptor)4 URI (java.net.URI)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 BuiltinClassLoader (jdk.internal.loader.BuiltinClassLoader)4 ModuleFinder (java.lang.module.ModuleFinder)3 AccessController (java.security.AccessController)3 PrivilegedAction (java.security.PrivilegedAction)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3