pub struct ModuleSystem { /* private fields */ }Expand description
Complete module system combining static configuration and dynamic runtime state.
This structure provides:
- Static configuration (
ResolvedConfiguration): Module graph resolved at startup - Dynamic state: Runtime modifications via
Module.addExports0(), etc.
Access checking combines both sources - static configuration is checked first, then dynamic modifications are checked if the static check fails.
Implementations§
Source§impl ModuleSystem
impl ModuleSystem
Sourcepub async fn new(
configuration: &Configuration,
java_home: &Path,
java_major_version: u16,
) -> Result<Self>
pub async fn new( configuration: &Configuration, java_home: &Path, java_major_version: u16, ) -> Result<Self>
Creates a new module system initialized with command-line options and resolved modules.
This function performs full JPMS module resolution:
- Loads system modules from the jimage file (for Java 9+)
- Loads modules from the module path (if specified)
- Resolves the module graph starting from root modules
- Applies command-line overrides (–add-reads, –add-exports, –add-opens)
For simple classpath-based applications (no module path, no main module), this skips expensive module resolution and uses a fast path.
§Errors
Returns an error if module resolution fails.
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates a new empty module system with no static configuration.
This is primarily useful for testing or when creating a module system that will only use dynamic modifications.
Sourcepub fn resolved_configuration(&self) -> &ResolvedConfiguration
pub fn resolved_configuration(&self) -> &ResolvedConfiguration
Returns the resolved configuration.
Sourcepub fn check_access(
&self,
from_module: Option<&str>,
to_module: Option<&str>,
to_class_name: &str,
) -> AccessCheckResult
pub fn check_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> AccessCheckResult
Checks if access from one module to a class in another module is allowed.
This implements JPMS access checking by combining:
- Static configuration: Module descriptors and command-line options
- Dynamic state: Runtime modifications via
Module.addExports0(), etc.
§Arguments
from_module- The module requesting access (useALL_UNNAMEDfor classpath code)to_module- The module containing the target typeto_class_name- The fully qualified class name (in internal format)
§Returns
An AccessCheckResult indicating if access is allowed.
Sourcepub fn check_reflection_access(
&self,
from_module: Option<&str>,
to_module: Option<&str>,
to_class_name: &str,
) -> AccessCheckResult
pub fn check_reflection_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> AccessCheckResult
Checks if reflection access from one module to a class in another module is allowed.
This implements JPMS reflection access checking by combining:
- Static configuration: Module descriptors and command-line options
- Dynamic state: Runtime modifications via
Module.addOpens0(), etc.
§Arguments
from_module- The module requesting access (useALL_UNNAMEDfor classpath code)to_module- The module containing the target typeto_class_name- The fully qualified class name (in internal format)
§Returns
An AccessCheckResult indicating if reflection access is allowed.
Sourcepub fn add_export(
&self,
source_module: &str,
package: &str,
target_module: Option<&str>,
)
pub fn add_export( &self, source_module: &str, package: &str, target_module: Option<&str>, )
Adds an export from source_module of package to target_module.
This is called by Module.addExports0(Module, String, Module).
If target_module is None, the package is exported to all modules.
Sourcepub fn add_export_to_all(&self, source_module: &str, package: &str)
pub fn add_export_to_all(&self, source_module: &str, package: &str)
Adds an unqualified export from source_module of package to all modules.
This is called by Module.addExportsToAll0(Module, String).
Sourcepub fn add_export_to_all_unnamed(&self, source_module: &str, package: &str)
pub fn add_export_to_all_unnamed(&self, source_module: &str, package: &str)
Adds an export from source_module of package to all unnamed modules.
This is called by Module.addExportsToAllUnnamed0(Module, String).
Sourcepub fn is_exported(
&self,
source_module: &str,
package: &str,
target_module: &str,
) -> bool
pub fn is_exported( &self, source_module: &str, package: &str, target_module: &str, ) -> bool
Checks if package in source_module is exported to target_module.
Sourcepub fn add_opens(
&self,
source_module: &str,
package: &str,
target_module: Option<&str>,
)
pub fn add_opens( &self, source_module: &str, package: &str, target_module: Option<&str>, )
Adds an opens from source_module of package to target_module.
This is called by Module.addOpens0(Module, String, Module).
If target_module is None, the package is opened to all modules.
Sourcepub fn add_opens_to_all(&self, source_module: &str, package: &str)
pub fn add_opens_to_all(&self, source_module: &str, package: &str)
Adds an unqualified opens from source_module of package to all modules.
Sourcepub fn add_opens_to_all_unnamed(&self, source_module: &str, package: &str)
pub fn add_opens_to_all_unnamed(&self, source_module: &str, package: &str)
Adds an opens from source_module of package to all unnamed modules.
Sourcepub fn is_opened(
&self,
source_module: &str,
package: &str,
target_module: &str,
) -> bool
pub fn is_opened( &self, source_module: &str, package: &str, target_module: &str, ) -> bool
Checks if package in source_module is opened to target_module.
Sourcepub fn add_read(&self, source_module: &str, target_module: &str)
pub fn add_read(&self, source_module: &str, target_module: &str)
Adds a read edge from source_module to target_module.
This is called by Module.addReads0(Module, Module).
Sourcepub fn can_read(&self, source_module: &str, target_module: &str) -> bool
pub fn can_read(&self, source_module: &str, target_module: &str) -> bool
Checks if source_module reads target_module.
Sourcepub fn define_module(&self, module: DefinedModule)
pub fn define_module(&self, module: DefinedModule)
Defines a new module.
This is called by Module.defineModule0.
Sourcepub fn get_module(&self, name: &str) -> Option<DefinedModule>
pub fn get_module(&self, name: &str) -> Option<DefinedModule>
Gets a defined module by name.
Sourcepub fn is_module_open(&self, name: &str) -> bool
pub fn is_module_open(&self, name: &str) -> bool
Checks if a module is defined and is open.
Sourcepub fn get_all_exports(
&self,
) -> AHashMap<String, AHashMap<String, AHashSet<String>>>
pub fn get_all_exports( &self, ) -> AHashMap<String, AHashMap<String, AHashSet<String>>>
Returns all dynamic exports for merging with static configuration.
Sourcepub fn get_all_opens(
&self,
) -> AHashMap<String, AHashMap<String, AHashSet<String>>>
pub fn get_all_opens( &self, ) -> AHashMap<String, AHashMap<String, AHashSet<String>>>
Returns all dynamic opens for merging with static configuration.
Sourcepub fn get_all_reads(&self) -> AHashMap<String, AHashSet<String>>
pub fn get_all_reads(&self) -> AHashMap<String, AHashSet<String>>
Returns all dynamic reads for merging with static configuration.
Sourcepub fn boot_unnamed_module(&self) -> Option<Value>
pub fn boot_unnamed_module(&self) -> Option<Value>
Gets the boot class loader’s unnamed module.
Returns None if the unnamed module has not been set yet (i.e., before
BootLoader.setBootLoaderUnnamedModule0 is called during JVM initialization).
Sourcepub fn get_module_for_package(&self, package: &str) -> Option<Value>
pub fn get_module_for_package(&self, package: &str) -> Option<Value>
Gets the java.lang.Module object for a given package.
This looks up which module contains the package and returns the corresponding
java.lang.Module object that was stored during defineModule0.
Returns None if:
- The package is not in any defined module
- The module was defined but no Module object was stored
Sourcepub fn set_boot_unnamed_module(&self, module: Value)
pub fn set_boot_unnamed_module(&self, module: Value)
Sets the boot class loader’s unnamed module.
This is called by BootLoader.setBootLoaderUnnamedModule0 during JVM initialization.
The unnamed module is used as the default module for classes that don’t have
an explicit module assignment.
Sourcepub fn check_dynamic_access(
&self,
from_module: &str,
to_module: &str,
package: &str,
) -> AccessCheckResult
pub fn check_dynamic_access( &self, from_module: &str, to_module: &str, package: &str, ) -> AccessCheckResult
Checks if from_module can access package in to_module based on dynamic state only.
This checks only runtime modifications (via Module.addExports0(), etc.).
For complete JPMS access checking, the VM should also check the static
ResolvedConfiguration from the classloader.
§Arguments
from_module- The module requesting access (useALL_UNNAMEDfor classpath code)to_module- The module containing the target typepackage- The package containing the target type (in internal format, e.g., “java/lang”)
§Returns
An AccessCheckResult indicating whether access is allowed by dynamic state.
Sourcepub fn check_dynamic_reflection_access(
&self,
from_module: &str,
to_module: &str,
package: &str,
) -> AccessCheckResult
pub fn check_dynamic_reflection_access( &self, from_module: &str, to_module: &str, package: &str, ) -> AccessCheckResult
Checks if from_module can reflect on package in to_module based on dynamic state only.
This checks only runtime modifications (via Module.addOpens0(), etc.).
For complete JPMS reflection checking, the VM should also check the static
ResolvedConfiguration from the classloader.
§Arguments
from_module- The module requesting access (useALL_UNNAMEDfor classpath code)to_module- The module containing the target typepackage- The package containing the target type (in internal format)
§Returns
An AccessCheckResult indicating whether reflection access is allowed by dynamic state.
Sourcepub fn package_from_class_name(class_name: &str) -> &str
pub fn package_from_class_name(class_name: &str) -> &str
Extracts the package name from a fully qualified class name.
§Examples
java/lang/String→java/langcom/example/MyClass→com/exampleMyClass→ empty string (default package)
Sourcepub fn illegal_access_error(
from_module: &str,
to_module: &str,
class_name: &str,
result: AccessCheckResult,
) -> String
pub fn illegal_access_error( from_module: &str, to_module: &str, class_name: &str, result: AccessCheckResult, ) -> String
Returns an IllegalAccessError message for a denied access check.
Sourcepub fn require_access(
&self,
from_module: Option<&str>,
to_module: Option<&str>,
to_class_name: &str,
) -> Result<()>
pub fn require_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> Result<()>
Requires module access from one module to a class in another module.
This is a convenience method that combines check_access with error generation.
It throws IllegalAccessError if access is denied.
§Arguments
from_module- The name of the module requesting access (None for unnamed/classpath)to_module- The name of the module containing the target class (None for unnamed/classpath)to_class_name- The fully qualified name of the target class (in internal format)
§Errors
Returns an IllegalAccessError if access is denied.
Sourcepub fn require_reflection_access(
&self,
from_module: Option<&str>,
to_module: Option<&str>,
to_class_name: &str,
) -> Result<()>
pub fn require_reflection_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> Result<()>
Requires reflection access from one module to a class in another module.
This is a convenience method that combines check_reflection_access with error generation.
It throws InaccessibleObjectException if reflection access is denied.
§Arguments
from_module- The name of the module requesting access (None for unnamed/classpath)to_module- The name of the module containing the target class (None for unnamed/classpath)to_class_name- The fully qualified name of the target class (in internal format)
§Errors
Returns an InaccessibleObjectException if reflection access is denied.
Trait Implementations§
Source§impl Debug for ModuleSystem
impl Debug for ModuleSystem
Source§impl Default for ModuleSystem
impl Default for ModuleSystem
Source§impl ModuleAccess for ModuleSystem
impl ModuleAccess for ModuleSystem
Source§fn add_export(
&self,
source_module: &str,
package: &str,
target_module: Option<&str>,
)
fn add_export( &self, source_module: &str, package: &str, target_module: Option<&str>, )
source_module of package to target_module.Source§fn add_export_to_all(&self, source_module: &str, package: &str)
fn add_export_to_all(&self, source_module: &str, package: &str)
source_module of package to all modules.Source§fn add_export_to_all_unnamed(&self, source_module: &str, package: &str)
fn add_export_to_all_unnamed(&self, source_module: &str, package: &str)
source_module of package to all unnamed modules.Source§fn add_read(&self, source_module: &str, target_module: &str)
fn add_read(&self, source_module: &str, target_module: &str)
source_module to target_module.Source§fn define_module(&self, module: DefinedModule)
fn define_module(&self, module: DefinedModule)
Source§fn check_reflection_access(
&self,
from_module: Option<&str>,
to_module: Option<&str>,
to_class_name: &str,
) -> AccessCheckResult
fn check_reflection_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> AccessCheckResult
Source§fn require_reflection_access(
&self,
from_module: Option<&str>,
to_module: Option<&str>,
to_class_name: &str,
) -> Result<()>
fn require_reflection_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> Result<()>
Source§fn set_boot_unnamed_module(&self, module: Value)
fn set_boot_unnamed_module(&self, module: Value)
Source§fn boot_unnamed_module(&self) -> Option<Value>
fn boot_unnamed_module(&self) -> Option<Value>
Auto Trait Implementations§
impl !Freeze for ModuleSystem
impl !RefUnwindSafe for ModuleSystem
impl Send for ModuleSystem
impl Sync for ModuleSystem
impl Unpin for ModuleSystem
impl !UnwindSafe for ModuleSystem
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more