Path: blob/master/src/java.base/share/classes/jdk/internal/module/ModuleReferenceImpl.java
41159 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.module;2627import java.io.IOException;28import java.io.UncheckedIOException;29import java.lang.module.ModuleDescriptor;30import java.lang.module.ModuleReader;31import java.lang.module.ModuleReference;32import java.net.URI;33import java.util.Objects;34import java.util.function.Supplier;3536/**37* A ModuleReference implementation that supports referencing a module that38* is patched and/or can be tied to other modules by means of hashes.39*/4041public class ModuleReferenceImpl extends ModuleReference {4243// location of module44private final URI location;4546// the module reader47private final Supplier<ModuleReader> readerSupplier;4849// non-null if the module is patched50private final ModulePatcher patcher;5152// ModuleTarget if the module is OS/architecture specific53private final ModuleTarget target;5455// the hashes of other modules recorded in this module56private final ModuleHashes recordedHashes;5758// the function that computes the hash of this module59private final ModuleHashes.HashSupplier hasher;6061// ModuleResolution flags62private final ModuleResolution moduleResolution;6364// cached hash of this module to avoid needing to compute it many times65private byte[] cachedHash;6667/**68* Constructs a new instance of this class.69*/70public ModuleReferenceImpl(ModuleDescriptor descriptor,71URI location,72Supplier<ModuleReader> readerSupplier,73ModulePatcher patcher,74ModuleTarget target,75ModuleHashes recordedHashes,76ModuleHashes.HashSupplier hasher,77ModuleResolution moduleResolution)78{79super(descriptor, Objects.requireNonNull(location));80this.location = location;81this.readerSupplier = readerSupplier;82this.patcher = patcher;83this.target = target;84this.recordedHashes = recordedHashes;85this.hasher = hasher;86this.moduleResolution = moduleResolution;87}8889@Override90public ModuleReader open() throws IOException {91try {92return readerSupplier.get();93} catch (UncheckedIOException e) {94throw e.getCause();95}96}9798/**99* Returns {@code true} if this module has been patched via --patch-module.100*/101public boolean isPatched() {102return (patcher != null);103}104105/**106* Returns the ModuleTarget or {@code null} if the no target platform.107*/108public ModuleTarget moduleTarget() {109return target;110}111112/**113* Returns the hashes recorded in this module or {@code null} if there114* are no hashes recorded.115*/116public ModuleHashes recordedHashes() {117return recordedHashes;118}119120/**121* Returns the supplier that computes the hash of this module.122*/123ModuleHashes.HashSupplier hasher() {124return hasher;125}126127/**128* Returns the ModuleResolution flags.129*/130public ModuleResolution moduleResolution() {131return moduleResolution;132}133134/**135* Computes the hash of this module. Returns {@code null} if the hash136* cannot be computed.137*138* @throws java.io.UncheckedIOException if an I/O error occurs139*/140public byte[] computeHash(String algorithm) {141byte[] result = cachedHash;142if (result != null)143return result;144if (hasher == null)145return null;146cachedHash = result = hasher.generate(algorithm);147return result;148}149150@Override151public int hashCode() {152int hc = hash;153if (hc == 0) {154hc = descriptor().hashCode();155hc = 43 * hc + Objects.hashCode(location);156hc = 43 * hc + Objects.hashCode(patcher);157if (hc == 0)158hc = -1;159hash = hc;160}161return hc;162}163164private int hash;165166@Override167public boolean equals(Object ob) {168if (!(ob instanceof ModuleReferenceImpl))169return false;170ModuleReferenceImpl that = (ModuleReferenceImpl)ob;171172// assume module content, recorded hashes, etc. are the same173// when the modules have equal module descriptors, are at the174// same location, and are patched by the same patcher.175return Objects.equals(this.descriptor(), that.descriptor())176&& Objects.equals(this.location, that.location)177&& Objects.equals(this.patcher, that.patcher);178}179180@Override181public String toString() {182StringBuilder sb = new StringBuilder();183sb.append("[module ");184sb.append(descriptor().name());185sb.append(", location=");186sb.append(location);187if (isPatched()) sb.append(" (patched)");188sb.append("]");189return sb.toString();190}191192}193194195