Module java.base
Package java.lang.module

## Class ModuleReference

- java.lang.Object
- - java.lang.module.ModuleReference

    public final class ModuleReference
    extends Object

A reference to a module's content.

A module reference contains the module's descriptor and its location, if known. It also has the ability to create a `ModuleReader` in order to access the module's content, which may be inside the Java run-time system itself or in an artifact such as a modular JAR file.

Since:
  9
See Also:
  `ModuleFinder`, `ModuleReader`

### Constructor Summary

Constructors
Constructor and Description
`ModuleReference(ModuleDescriptor descriptor, URI location, Supplier<ModuleReader> readerSupplier)` Constructs a new instance of this class.

### Method Summary

All Methods Instance Methods Concrete Methods
`ModuleDescriptor` `descriptor()` Returns the module descriptor.
`boolean` `equals(Object ob)` Tests this module reference for equality with the given object.
`int` `hashCode()` Computes a hash code for this module reference.
`Optional<URI>` `location()` Returns the location of this module's content, if known.
`ModuleReader` `open()` Opens the module content for reading.
`String` `toString()` Returns a string describing this module reference.

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

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

### Constructor Detail

#### ModuleReference

    public ModuleReference(ModuleDescriptor descriptor,
                           URI location,
                           Supplier<ModuleReader> readerSupplier)

Constructs a new instance of this class.

The `readSupplier` parameter is the supplier of the `ModuleReader` that may be used to read the module content. Its `get()` method throws `UncheckedIOException` if an I/O error occurs opening the module content. The `get()` method throws `SecurityException` if opening the module is denied by the security manager.

Parameters:
  `descriptor` - The module descriptor
  `location` - The module location or `null` if not known
  `readerSupplier` - The `Supplier` of the `ModuleReader`

### Method Detail

#### descriptor

    public ModuleDescriptor descriptor()

Returns the module descriptor.

Returns:
  The module descriptor

#### location

    public Optional<URI> location()

Returns the location of this module's content, if known.

This URI, when present, is used as the location value of a `CodeSource` so that a module's classes can be granted specific permissions when loaded by a `SecureClassLoader`.

Returns:
  The location or an empty `Optional` if not known

#### open

    public ModuleReader open()
                      throws IOException

Opens the module content for reading.

This method opens the module content by invoking the `get()` method of the `readSupplier` specified at construction time.

Returns:
  A `ModuleReader` to read the module
Throws:
  `IOException` - If an I/O error occurs
  `SecurityException` - If denied by the security manager

#### hashCode

    public int hashCode()

Computes a hash code for this module reference.

The hash code is based upon the components of the reference, and satisfies the general contract of the `Object.hashCode` method.

Overrides:
  `hashCode` in class `Object`
Returns:
  The hash-code value for this module reference
See Also:
  `Object.equals(java.lang.Object)`, `System.identityHashCode(java.lang.Object)`

#### equals

    public boolean equals(Object ob)

Tests this module reference for equality with the given object.

If the given object is not a `ModuleReference` then this method returns `false`. Two module references are equal if their module descriptors are equal, their locations are equal or both unknown, and were created with equal supplier objects to access the module content.

This method satisfies the general contract of the `Object.equals` method.

Overrides:
  `equals` in class `Object`
Parameters:
  `ob` - the object to which this object is to be compared
Returns:
  `true` if, and only if, the given object is a module reference that is equal to this module reference
See Also:
  `Object.hashCode()`, `HashMap`

#### toString

    public String toString()

Returns a string describing this module reference.

Overrides:
  `toString` in class `Object`
Returns:
  A string describing this module reference