--- old/src/java.base/share/classes/java/lang/module/ModuleReference.java 2016-12-15 09:19:07.602764027 +0000 +++ new/src/java.base/share/classes/java/lang/module/ModuleReference.java 2016-12-15 09:19:07.472755133 +0000 @@ -26,96 +26,42 @@ package java.lang.module; import java.io.IOException; -import java.io.UncheckedIOException; import java.net.URI; import java.util.Objects; import java.util.Optional; -import java.util.function.Supplier; - -import jdk.internal.module.ModuleHashes.HashSupplier; /** * 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 {@link 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. + *

A module reference is a concrete implementation of this class that + * implements the abstract methods defined by this class. It contains the + * module's descriptor and its location, if known. It also has the ability to + * create a {@link 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. * * @see ModuleFinder * @see ModuleReader * @since 9 */ -public final class ModuleReference { +public abstract class ModuleReference { private final ModuleDescriptor descriptor; private final URI location; - private final Supplier readerSupplier; - - // true if this is a reference to a patched module - private boolean patched; - - // the function that computes the hash of this module reference - private final HashSupplier hasher; - - // cached hash to avoid needing to compute it many times - private byte[] cachedHash; - - - /** - * Constructs a new instance of this class. - */ - ModuleReference(ModuleDescriptor descriptor, - URI location, - Supplier readerSupplier, - boolean patched, - HashSupplier hasher) - - { - this.descriptor = Objects.requireNonNull(descriptor); - this.location = location; - this.readerSupplier = Objects.requireNonNull(readerSupplier); - this.patched = patched; - this.hasher = hasher; - } - - /** - * Constructs a new instance of this class. - */ - ModuleReference(ModuleDescriptor descriptor, - URI location, - Supplier readerSupplier, - HashSupplier hasher) - - { - this(descriptor, location, readerSupplier, false, hasher); - } - /** * Constructs a new instance of this class. * - *

The {@code readSupplier} parameter is the supplier of the {@link - * ModuleReader} that may be used to read the module content. Its {@link - * Supplier#get() get()} method throws {@link UncheckedIOException} if an - * I/O error occurs opening the module content. The {@code get()} method - * throws {@link SecurityException} if opening the module is denied by the - * security manager. - * * @param descriptor * The module descriptor * @param location * The module location or {@code null} if not known - * @param readerSupplier - * The {@code Supplier} of the {@code ModuleReader} */ - public ModuleReference(ModuleDescriptor descriptor, - URI location, - Supplier readerSupplier) - { - this(descriptor, location, readerSupplier, false, null); + protected ModuleReference(ModuleDescriptor descriptor, URI location) { + this.descriptor = Objects.requireNonNull(descriptor); + this.location = location; } /** @@ -123,11 +69,10 @@ * * @return The module descriptor */ - public ModuleDescriptor descriptor() { + public final ModuleDescriptor descriptor() { return descriptor; } - /** * Returns the location of this module's content, if known. * @@ -139,18 +84,13 @@ * * @return The location or an empty {@code Optional} if not known */ - public Optional location() { + public final Optional location() { return Optional.ofNullable(location); } - /** * Opens the module content for reading. * - *

This method opens the module content by invoking the {@link - * Supplier#get() get()} method of the {@code readSupplier} specified at - * construction time.

- * * @return A {@code ModuleReader} to read the module * * @throws IOException @@ -158,113 +98,5 @@ * @throws SecurityException * If denied by the security manager */ - public ModuleReader open() throws IOException { - try { - return readerSupplier.get(); - } catch (UncheckedIOException e) { - throw e.getCause(); - } - - } - - - /** - * Returns {@code true} if this module has been patched via --patch-module. - */ - boolean isPatched() { - return patched; - } - - /** - * Returns the hash supplier for this module. - */ - HashSupplier hasher() { - return hasher; - } - - /** - * Computes the hash of this module. Returns {@code null} if the hash - * cannot be computed. - * - * @throws java.io.UncheckedIOException if an I/O error occurs - */ - byte[] computeHash(String algorithm) { - byte[] result = cachedHash; - if (result != null) - return result; - if (hasher == null) - return null; - cachedHash = result = hasher.generate(algorithm); - return result; - } - - /** - * 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 {@link Object#hashCode - * Object.hashCode} method.

- * - * @return The hash-code value for this module reference - */ - @Override - public int hashCode() { - int hc = hash; - if (hc == 0) { - hc = descriptor.hashCode(); - hc = 43 * hc + readerSupplier.hashCode(); - hc = 43 * hc + Objects.hashCode(location); - hc = 43 * hc + Objects.hashCode(hasher); - hc = 43 * hc + Boolean.hashCode(patched); - if (hc == 0) - hc = -1; - hash = hc; - } - return hc; - } - - private int hash; - - /** - * Tests this module reference for equality with the given object. - * - *

If the given object is not a {@code ModuleReference} then this - * method returns {@code 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 {@link - * java.lang.Object#equals(Object) Object.equals} method.

- * - * @param ob - * the object to which this object is to be compared - * - * @return {@code true} if, and only if, the given object is a module - * reference that is equal to this module reference - */ - @Override - public boolean equals(Object ob) { - if (!(ob instanceof ModuleReference)) - return false; - ModuleReference that = (ModuleReference)ob; - - return Objects.equals(this.descriptor, that.descriptor) - && Objects.equals(this.location, that.location) - && Objects.equals(this.readerSupplier, that.readerSupplier) - && Objects.equals(this.hasher, that.hasher) - && this.patched == that.patched; - } - - /** - * Returns a string describing this module reference. - * - * @return A string describing this module reference - */ - @Override - public String toString() { - return ("[module " + descriptor().name() - + ", location=" + location + "]"); - } - + public abstract ModuleReader open() throws IOException; }