jdk/src/java.base/share/classes/java/lang/module/ModuleReference.java
author |
alanb |
|
Thu, 17 Mar 2016 19:04:16 +0000 |
changeset 36511 |
9d0388c6b336 |
child 37779 |
7c84df693837 |
permissions |
-rw-r--r-- |
8142968: Module System implementation
Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
/*
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
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.Hasher.HashSupplier;
/**
* A reference to a module's content.
*
* <p> 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.
*
* @see ModuleFinder
* @see ModuleReader
* @since 9
*/
public final class ModuleReference {
private final ModuleDescriptor descriptor;
private final URI location;
private final Supplier<ModuleReader> readerSupplier;
// the function that computes the hash of this module reference
private final HashSupplier hasher;
// cached hash string to avoid needing to compute it many times
private String cachedHash;
/**
* Constructs a new instance of this class.
*/
ModuleReference(ModuleDescriptor descriptor,
URI location,
Supplier<ModuleReader> readerSupplier,
HashSupplier hasher)
{
this.descriptor = Objects.requireNonNull(descriptor);
this.location = location;
this.readerSupplier = Objects.requireNonNull(readerSupplier);
this.hasher = hasher;
}
/**
* Constructs a new instance of this class.
*
* <p> 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<ModuleReader> readerSupplier)
{
this(descriptor, location, readerSupplier, null);
}
/**
* Returns the module descriptor.
*
* @return The module descriptor
*/
public ModuleDescriptor descriptor() {
return descriptor;
}
/**
* Returns the location of this module's content, if known.
*
* <p> This URI, when present, is used as the {@linkplain
* java.security.CodeSource#getLocation location} value of a {@link
* java.security.CodeSource CodeSource} so that a module's classes can be
* granted specific permissions when loaded by a {@link
* java.security.SecureClassLoader SecureClassLoader}.
*
* @return The location or an empty {@code Optional} if not known
*/
public Optional<URI> location() {
return Optional.ofNullable(location);
}
/**
* Opens the module content for reading.
*
* <p> This method opens the module content by invoking the {@link
* Supplier#get() get()} method of the {@code readSupplier} specified at
* construction time. </p>
*
* @return A {@code ModuleReader} to read the module
*
* @throws IOException
* If an I/O error occurs
* @throws SecurityException
* If denied by the security manager
*/
public ModuleReader open() throws IOException {
try {
return readerSupplier.get();
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
/**
* Computes the hash of this module, returning it as a hex string.
* Returns {@code null} if the hash cannot be computed.
*
* @throws java.io.UncheckedIOException if an I/O error occurs
*/
String computeHash(String algorithm) {
String result = cachedHash;
if (result != null)
return result;
if (hasher == null)
return null;
cachedHash = result = hasher.generate(algorithm);
return result;
}
private int hash;
/**
* Computes a hash code for this module reference.
*
* <p> The hash code is based upon the components of the reference, and
* satisfies the general contract of the {@link Object#hashCode
* Object.hashCode} method. </p>
*
* @return The hash-code value for this module reference
*/
@Override
public int hashCode() {
int hc = hash;
if (hc == 0) {
hc = Objects.hash(descriptor, location, readerSupplier, hasher);
if (hc != 0) hash = hc;
}
return hc;
}
/**
* Tests this module reference for equality with the given object.
*
* <p> 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. </p>
*
* <p> This method satisfies the general contract of the {@link
* java.lang.Object#equals(Object) Object.equals} method. </p>
*
* @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);
}
/**
* Returns a string describing this module reference.
*
* @return A string describing this module reference
*/
@Override
public String toString() {
return ("[module " + descriptor().name()
+ ", location=" + location + "]");
}
}