Skip to main content

ModuleSystem

Struct ModuleSystem 

Source
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

Source

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:

  1. Loads system modules from the jimage file (for Java 9+)
  2. Loads modules from the module path (if specified)
  3. Resolves the module graph starting from root modules
  4. 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.

Source

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.

Source

pub fn resolved_configuration(&self) -> &ResolvedConfiguration

Returns the resolved configuration.

Source

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:

  1. Static configuration: Module descriptors and command-line options
  2. Dynamic state: Runtime modifications via Module.addExports0(), etc.
§Arguments
  • from_module - The module requesting access (use ALL_UNNAMED for classpath code)
  • to_module - The module containing the target type
  • to_class_name - The fully qualified class name (in internal format)
§Returns

An AccessCheckResult indicating if access is allowed.

Source

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:

  1. Static configuration: Module descriptors and command-line options
  2. Dynamic state: Runtime modifications via Module.addOpens0(), etc.
§Arguments
  • from_module - The module requesting access (use ALL_UNNAMED for classpath code)
  • to_module - The module containing the target type
  • to_class_name - The fully qualified class name (in internal format)
§Returns

An AccessCheckResult indicating if reflection access is allowed.

Source

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.

Source

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).

Source

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).

Source

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.

Source

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.

Source

pub fn add_opens_to_all(&self, source_module: &str, package: &str)

Adds an unqualified opens from source_module of package to all modules.

Source

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.

Source

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.

Source

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).

Source

pub fn can_read(&self, source_module: &str, target_module: &str) -> bool

Checks if source_module reads target_module.

Source

pub fn define_module(&self, module: DefinedModule)

Defines a new module.

This is called by Module.defineModule0.

Source

pub fn get_module(&self, name: &str) -> Option<DefinedModule>

Gets a defined module by name.

Source

pub fn is_module_open(&self, name: &str) -> bool

Checks if a module is defined and is open.

Source

pub fn get_all_exports( &self, ) -> AHashMap<String, AHashMap<String, AHashSet<String>>>

Returns all dynamic exports for merging with static configuration.

Source

pub fn get_all_opens( &self, ) -> AHashMap<String, AHashMap<String, AHashSet<String>>>

Returns all dynamic opens for merging with static configuration.

Source

pub fn get_all_reads(&self) -> AHashMap<String, AHashSet<String>>

Returns all dynamic reads for merging with static configuration.

Source

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).

Source

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
Source

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.

Source

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 (use ALL_UNNAMED for classpath code)
  • to_module - The module containing the target type
  • package - The package containing the target type (in internal format, e.g., “java/lang”)
§Returns

An AccessCheckResult indicating whether access is allowed by dynamic state.

Source

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 (use ALL_UNNAMED for classpath code)
  • to_module - The module containing the target type
  • package - The package containing the target type (in internal format)
§Returns

An AccessCheckResult indicating whether reflection access is allowed by dynamic state.

Source

pub fn package_from_class_name(class_name: &str) -> &str

Extracts the package name from a fully qualified class name.

§Examples
  • java/lang/Stringjava/lang
  • com/example/MyClasscom/example
  • MyClass → empty string (default package)
Source

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.

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ModuleSystem

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl ModuleAccess for ModuleSystem

Source§

fn add_export( &self, source_module: &str, package: &str, target_module: Option<&str>, )

Add an export from source_module of package to target_module.
Source§

fn add_export_to_all(&self, source_module: &str, package: &str)

Add an export from source_module of package to all modules.
Source§

fn add_export_to_all_unnamed(&self, source_module: &str, package: &str)

Add an export from source_module of package to all unnamed modules.
Source§

fn add_read(&self, source_module: &str, target_module: &str)

Add a read edge from source_module to target_module.
Source§

fn define_module(&self, module: DefinedModule)

Define a module.
Source§

fn check_reflection_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> AccessCheckResult

Check reflection access between modules.
Source§

fn require_reflection_access( &self, from_module: Option<&str>, to_module: Option<&str>, to_class_name: &str, ) -> Result<()>

Require reflection access, returning an error if denied. Read more
Source§

fn set_boot_unnamed_module(&self, module: Value)

Set the boot class loader’s unnamed module.
Source§

fn boot_unnamed_module(&self) -> Option<Value>

Get the boot class loader’s unnamed module.
Source§

fn get_module_for_package(&self, package: &str) -> Option<Value>

Get the module object for a given package.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more