Module java.base
Package java.lang.reflect

## Class Module

- java.lang.Object
- - java.lang.reflect.Module

    public final class Module
    extends Object

Represents a run-time module, either `named` or unnamed.

Named modules have a `name` and are constructed by the Java Virtual Machine when a graph of modules is defined to the Java virtual machine to create a module `Layer`.

An unnamed module does not have a name. There is an unnamed module per `ClassLoader` that is obtained by invoking the class loader's `getUnnamedModule` method. The `getModule` method of all types defined by a class loader that are not in a named module return the class loader's unnamed module.

The package names that are parameters or returned by methods defined in this class are the fully-qualified names of the packages as defined in section 6.5.3 of The Java™ Language Specification , for example, `"java.lang"`.

Unless otherwise specified, passing a `null` argument to a method in this class causes a `NullPointerException` to be thrown.

Since:
  9
See Also:
  `Class.getModule()`

### Method Summary

All Methods Instance Methods Concrete Methods
`Module` `addExports(String pn, Module other)` If the caller's module is this module then update this module to export package `pn` to the given module.
`Module` `addReads(Module other)` If the caller's module is this module then update this module to read the given module.
`Module` `addUses(Class<?> st)` If the caller's module is this module then update this module to add a service dependence on the given service type.
`boolean` `canRead(Module other)` Indicates if this module reads the given module.
`boolean` `canUse(Class<?> st)` Indicates if this module has a service dependence on the given service type.
`ClassLoader` `getClassLoader()` Returns the `ClassLoader` for this module.
`ModuleDescriptor` `getDescriptor()` Returns the module descriptor for this module or `null` if this module is an unnamed module.
`Layer` `getLayer()` Returns the layer that contains this module or `null` if this module is not in a layer.
`String` `getName()` Returns the module name or `null` if this module is an unnamed module.
`String[]` `getPackages()` Returns an array of the package names of the packages in this module.
`InputStream` `getResourceAsStream(String name)` Returns an input stream for reading a resource in this module.
`boolean` `isExported(String pn)` Returns `true` if this module exports the given package unconditionally.
`boolean` `isExported(String pn, Module other)` Returns `true` if this module exports the given package to at least the given module.
`boolean` `isNamed()` Returns `true` if this module is a named module.
`String` `toString()` Returns the string representation of this module.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Method Detail

#### isNamed

    public boolean isNamed()

Returns `true` if this module is a named module.

Returns:
  `true` if this is a named module
See Also:
  `ClassLoader.getUnnamedModule()`

#### getName

    public String getName()

Returns the module name or `null` if this module is an unnamed module.

Returns:
  The module name

#### getClassLoader

    public ClassLoader getClassLoader()

Returns the `ClassLoader` for this module.

If there is a security manager then its `checkPermission` method if first called with a `RuntimePermission("getClassLoader")` permission to check that the caller is allowed to get access to the class loader.

Returns:
  The class loader for this module
Throws:
  `SecurityException` - If denied by the security manager

#### getDescriptor

    public ModuleDescriptor getDescriptor()

Returns the module descriptor for this module or `null` if this module is an unnamed module.

Returns:
  The module descriptor for this module

#### getLayer

    public Layer getLayer()

Returns the layer that contains this module or `null` if this module is not in a layer. A module `Layer` contains named modules and therefore this method always returns `null` when invoked on an unnamed module.

Dynamic modules are named modules that are generated at runtime. A dynamic module may or may not be in a module Layer.

Returns:
  The layer that contains this module
See Also:
  `Proxy`

#### canRead

    public boolean canRead(Module other)

Indicates if this module reads the given module. This method returns `true` if invoked to test if this module reads itself. It also returns `true` if invoked on an unnamed module (as unnamed modules read all modules).

Parameters:
  `other` - The other module
Returns:
  `true` if this module reads `other`
See Also:
  `addReads(Module)`

#### addReads

    public Module addReads(Module other)

If the caller's module is this module then update this module to read the given module. This method is a no-op if `other` is this module (all modules can read themselves) or this module is an unnamed module (as unnamed modules read all modules).

Parameters:
  `other` - The other module
Returns:
  this module
Throws:
  `IllegalStateException` - If this is a named module and the caller is not this module
See Also:
  `canRead(java.lang.reflect.Module)`

#### isExported

    public boolean isExported(String pn,
                              Module other)

Returns `true` if this module exports the given package to at least the given module.

This method always return `true` when invoked on an unnamed module.

This method does not check if the given module reads this module

Parameters:
  `pn` - The package name
  `other` - The other module
Returns:
  `true` if this module exports the package to at least the given module

#### isExported

    public boolean isExported(String pn)

Returns `true` if this module exports the given package unconditionally.

This method always return `true` when invoked on an unnamed module.

This method does not check if the given module reads this module

Parameters:
  `pn` - The package name
Returns:
  `true` if this module exports the package unconditionally

#### addExports

    public Module addExports(String pn,
                             Module other)

If the caller's module is this module then update this module to export package `pn` to the given module.

This method has no effect if the package is already exported to the given module. If also has no effect if invoked on an unnamed module (as unnamed modules export all packages).

Parameters:
  `pn` - The package name
  `other` - The module
Returns:
  this module
Throws:
  `IllegalArgumentException` - If `pn` is `null`, or this is a named module and the package `pn` is not a package in this module
  `IllegalStateException` - If this is a named module and the caller is not this module

#### addUses

    public Module addUses(Class<?> st)

If the caller's module is this module then update this module to add a service dependence on the given service type. This method is intended for use by frameworks that invoke `ServiceLoader` on behalf of other modules or where the framework is passed a reference to the service type by other code. This method is a no-op when invoked on an unnamed module.

This method does not cause `resolveRequiresAndUses` to be re-run.

Parameters:
  `st` - The service type
Returns:
  this module
Throws:
  `IllegalStateException` - If this is a named module and the caller is not this module
See Also:
  `canUse(Class)`, `ModuleDescriptor.uses()`

#### canUse

    public boolean canUse(Class<?> st)

Indicates if this module has a service dependence on the given service type. This method always returns `true` when invoked on an unnamed module.

Parameters:
  `st` - The service type
Returns:
  `true` if this module uses service type `st`
See Also:
  `addUses(Class)`

#### getPackages

    public String[] getPackages()

Returns an array of the package names of the packages in this module.

For named modules, the returned array contains an element for each package in the module. It may contain elements corresponding to packages added to the module, dynamic modules for example, after it was loaded.

For unnamed modules, this method is the equivalent of invoking the `getDefinedPackages` method of this module's class loader and returning the array of package names.

A package name appears at most once in the returned array.

API Note:
  This method returns an array rather than a `Set` for consistency with other `java.lang.reflect` types.
Returns:
  an array of the package names of the packages in this module

#### getResourceAsStream

    public InputStream getResourceAsStream(String name)
                                    throws IOException

Returns an input stream for reading a resource in this module. Returns `null` if the resource is not in this module or access to the resource is denied by the security manager. The `name` is a `'/'`-separated path name that identifies the resource.

If this module is an unnamed module, and the `ClassLoader` for this module is not `null`, then this method is equivalent to invoking the `getResourceAsStream` method on the class loader for this module.

Parameters:
  `name` - The resource name
Returns:
  An input stream for reading the resource or `null`
Throws:
  `IOException` - If an I/O error occurs
See Also:
  `ModuleReader.open(String)`

#### toString

    public String toString()

Returns the string representation of this module. For a named module, the representation is the string `"module"`, followed by a space, and then the module name. For an unnamed module, the representation is the string `"unnamed module"`, followed by a space, and then an implementation specific identifier for the unnamed module.

Overrides:
  `toString` in class `Object`
Returns:
  The string representation of this module