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 // ModuleTarget if the module is OS/architecture specific 50 private final ModuleTarget target; 51 52 // the hashes of other modules recorded in this module 53 private final ModuleHashes recordedHashes; 54 55 // the function that computes the hash of this module 56 private final ModuleHashes.HashSupplier hasher; 57 58 // ModuleResolution flags 59 private final ModuleResolution moduleResolution; 60 61 // cached hash of this module to avoid needing to compute it many times 62 private byte[] cachedHash; 63 64 /** 65 * Constructs a new instance of this class. 66 */ 67 ModuleReferenceImpl(ModuleDescriptor descriptor, 68 URI location, 69 Supplier<ModuleReader> readerSupplier, 70 ModulePatcher patcher, 71 ModuleTarget target, 72 ModuleHashes recordedHashes, 73 ModuleHashes.HashSupplier hasher, 74 ModuleResolution moduleResolution) 75 { 76 super(descriptor, Objects.requireNonNull(location)); 77 this.readerSupplier = readerSupplier; 78 this.patcher = patcher; 79 this.target = target; 80 this.recordedHashes = recordedHashes; 81 this.hasher = hasher; 82 this.moduleResolution = moduleResolution; 83 } 84 85 @Override 86 public ModuleReader open() throws IOException { 87 try { 88 return readerSupplier.get(); 89 } catch (UncheckedIOException e) { 90 throw e.getCause(); 91 } 92 } 93 94 /** 95 * Returns {@code true} if this module has been patched via --patch-module. 96 */ 97 public boolean isPatched() { 98 return (patcher != null); 99 } 100 101 /** 102 * Returns the ModuleTarget or {@code null} if the no target platform. 103 */ 104 public ModuleTarget moduleTarget() { 105 return target; 106 } 107 108 /** 109 * Returns the hashes recorded in this module or {@code null} if there 110 * are no hashes recorded. 111 */ 112 public ModuleHashes recordedHashes() { 113 return recordedHashes; 114 } 115 116 /** 117 * Returns the supplier that computes the hash of this module. 118 */ 119 ModuleHashes.HashSupplier hasher() { 120 return hasher; 121 } 122 123 /** 124 * Returns the ModuleResolution flags. 125 */ 126 public ModuleResolution moduleResolution() { 127 return moduleResolution; 128 } 129 130 /** 131 * Computes the hash of this module. Returns {@code null} if the hash 132 * cannot be computed. 133 * 134 * @throws java.io.UncheckedIOException if an I/O error occurs 135 */ 136 public byte[] computeHash(String algorithm) { 137 byte[] result = cachedHash; 138 if (result != null) 139 return result; 140 if (hasher == null) 141 return null; 142 cachedHash = result = hasher.generate(algorithm); 143 return result; 144 } 145 146 @Override 147 public int hashCode() { 148 int hc = hash; 149 if (hc == 0) { 150 hc = descriptor().hashCode(); 151 hc = 43 * hc + Objects.hashCode(location()); 152 hc = 43 * hc + Objects.hashCode(patcher); 153 if (hc == 0) 154 hc = -1; 155 hash = hc; 156 } 157 return hc; 158 } 159 160 private int hash; 161 162 @Override 163 public boolean equals(Object ob) { 164 if (!(ob instanceof ModuleReferenceImpl)) 165 return false; 166 ModuleReferenceImpl that = (ModuleReferenceImpl)ob; 167 168 // assume module content, recorded hashes, etc. are the same 169 // when the modules have equal module descriptors, are at the 170 // same location, and are patched by the same patcher. 171 return Objects.equals(this.descriptor(), that.descriptor()) 172 && Objects.equals(this.location(), that.location()) 173 && Objects.equals(this.patcher, that.patcher); 174 } 175 176 @Override 177 public String toString() { 178 StringBuilder sb = new StringBuilder(); 179 sb.append("[module "); 180 sb.append(descriptor().name()); 181 sb.append(", location="); 182 sb.append(location().orElseThrow(() -> new InternalError())); 183 if (isPatched()) sb.append(" (patched)"); 184 sb.append("]"); 185 return sb.toString(); 186 } 187 188 }