How to get ClassLoader of newly created JPMS layer?

Pavel_K :

Maybe it is simple question but still can find how to do it. Let's suppose I use the following code to create layer

ModuleFinder finder = ModuleFinder.of(moduleCPath);
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of("module-c"));
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer myLayer = parent.defineModulesWithOneLoader(cf, scl);

As I understand for myLayer one classloader was created. How can I get reference to this classloader having reference to myLayer and without knowing what classes can be inside myLayer modules?

Slaw :

When a ModuleLayer is created each Module gets mapped to a ClassLoader.

ModuleLayer

A layer is created from a graph of modules in a Configuration and a function that maps each module to a ClassLoader.

A ModuleLayer has no concept of a ClassLoader because there is no guarantee that every Module will have the same ClassLoader. Instead, the ClassLoader is associated with each individual Module, which you can get via Module.getClassLoader().

Since you are using ModuleLayer.defineModulesWithOneLoader(Configuration,ClassLoader)1, however, there is a guarantee that each Module will have the same ClassLoader. This means you can get the ClassLoader from any Module in the ModuleLayer and consider it as the "ClassLoader of the ModuleLayer".

ModuleLayer layer = parent.defineModulesWithOneLoader(...);
ClassLoader loader = layer.modules().iterator().next().getClassLoader();

Note that this won't work as intended if:

  • The ModuleLayer is empty (has no Modules).
    • iterator().next() will throw a NoSuchElementException.
  • You create the ModuleLayer via defineModulesWithManyLoaders(Configuration,ClassLoader)1
    • Every Module will have its own ClassLoader.
  • You create the ModuleLayer via defineModules(Configuration,Function) and the Function doesn't return the same ClassLoader every time.
    • Allows any combination of Module to ClassLoader. Also, each ClassLoader can potentially have different parents. This method provides the most flexibility when defining ModuleLayers.

1. The methods defineModulesWithOneLoader and defineModulesWithManyLoaders are, in effect, convenience methods for defineModules for two pre-definable use cases; each Module has the same ClassLoader and each Module has their own ClassLoader, respectively.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get a reference to a newly created Android activity

How to get ID of newly created object in ActiveAdmin?

GIT - How to get all newly created branches

Chrome extension: how to get newly created tab url

Laravel 5.2 - How to get id of newly created user and pass it to a controller

How to automatically get write access to newly created directories?

How to get newly created machine details in Vagrant-Openstack plugin

How would I get the total of month in my newly created file?

How to get the user id of the newly created user in firebase

SwiftUI - how to get newly created object to navigation link

How long to wait before a get succeeds on a newly-created object?

How to get declared symbol from SemanticModel for newly created class?

How to get the ID of a newly created Firestore document in Vue.js?

How to get newly created _id from Meteor.loginWithFacebook() method?

How to get the id of a newly-created value with Diesel and SQLite?

SwiftUI - how to get the exactly center coordinate/position of newly created Button

ReactJS/Firestore: how to get the id of a newly created document

How to get the ID from a newly created database entry for a redirect - Mongoose

How can I get newly created entities with their proxy?

How to get the arn of a newly created aws_cloudformation_stack terraform

How to react on newly created Appointments?

How to localize a newly created storyboard?

How to get Spring boot project work with JPMS?

Reload Kubernetes ReplicationController to get newly created Service

Python: Get the name of a newly created directory?

Get the newly created element as a jQuery object

Laravel get ID of newly created resource

Laravel, get id from newly created object

How to get classpath from classloader?