1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.internal.module; 27 28 import java.io.IOException; 29 import java.io.UncheckedIOException; 30 import java.lang.module.ModuleDescriptor; 31 import java.lang.module.ModuleReader; 32 import java.lang.module.ModuleReference; 33 import java.net.URI; 34 import java.util.Objects; 35 import java.util.function.Supplier; 36 37 /** 38 * A ModuleReference implementation that supports referencing a module that 39 * is patched and/or can be tied to other modules by means of hashes. 40 */ 41 42 public class ModuleReferenceImpl extends ModuleReference { 43 44 private final Supplier<ModuleReader> readerSupplier; 45 46 // non-null if the module is patched 47 private final ModulePatcher patcher; 48 49 // the hashes of other modules recorded in this module 50 private final ModuleHashes recordedHashes; 51 52 // the function that computes the hash of this module 53 private final ModuleHashes.HashSupplier hasher; 54 55 // ModuleResolution flags 56 private final ModuleResolution moduleResolution; 57 58 // cached hash of this module to avoid needing to compute it many times 59 private byte[] cachedHash; 60 61 /** 62 * Constructs a new instance of this class. 63 */ 64 ModuleReferenceImpl(ModuleDescriptor descriptor, 65 URI location, 66 Supplier<ModuleReader> readerSupplier, 67 ModulePatcher patcher, 68 ModuleHashes recordedHashes, 69 ModuleHashes.HashSupplier hasher, 70 ModuleResolution moduleResolution) 71 { 72 super(descriptor, Objects.requireNonNull(location)); 73 this.readerSupplier = readerSupplier; 74 this.patcher = patcher; 75 this.recordedHashes = recordedHashes; 76 this.hasher = hasher; 77 this.moduleResolution = moduleResolution; 78 } 79 80 @Override 81 public ModuleReader open() throws IOException { 82 try { 83 return readerSupplier.get(); 84 } catch (UncheckedIOException e) { 85 throw e.getCause(); 86 } 87 } 88 89 /** 90 * Returns {@code true} if this module has been patched via --patch-module. 91 */ 92 public boolean isPatched() { 93 return (patcher != null); 94 } 95 96 /** 97 * Returns the hashes recorded in this module or {@code null} if there 98 * are no hashes recorded. 99 */ 100 public ModuleHashes recordedHashes() { 101 return recordedHashes; 102 } 103 104 /** 105 * Returns the supplier that computes the hash of this module. 106 */ 107 ModuleHashes.HashSupplier hasher() { 108 return hasher; 109 } 110 111 /** 112 * Returns the ModuleResolution flags. 113 */ 114 public ModuleResolution moduleResolution() { 115 return moduleResolution; 116 } 117 118 /** 119 * Computes the hash of this module. Returns {@code null} if the hash 120 * cannot be computed. 121 * 122 * @throws java.io.UncheckedIOException if an I/O error occurs 123 */ 124 public byte[] computeHash(String algorithm) { 125 byte[] result = cachedHash; 126 if (result != null) 127 return result; 128 if (hasher == null) 129 return null; 130 cachedHash = result = hasher.generate(algorithm); 131 return result; 132 } 133 134 @Override 135 public int hashCode() { 136 int hc = hash; 137 if (hc == 0) { 138 hc = descriptor().hashCode(); 139 hc = 43 * hc + Objects.hashCode(location()); 140 hc = 43 * hc + Objects.hashCode(patcher); 141 if (hc == 0) 142 hc = -1; 143 hash = hc; 144 } 145 return hc; 146 } 147 148 private int hash; 149 150 @Override 151 public boolean equals(Object ob) { 152 if (!(ob instanceof ModuleReferenceImpl)) 153 return false; 154 ModuleReferenceImpl that = (ModuleReferenceImpl)ob; 155 156 // assume module content, recorded hashes, etc. are the same 157 // when the modules have equal module descriptors, are at the 158 // same location, and are patched by the same patcher. 159 return Objects.equals(this.descriptor(), that.descriptor()) 160 && Objects.equals(this.location(), that.location()) 161 && Objects.equals(this.patcher, that.patcher); 162 } 163 164 @Override 165 public String toString() { 166 return super.toString(); 167 } 168 169 }