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 } | 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 } |