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 package jdk.internal.module; 26 27 import java.lang.module.ModuleDescriptor; 28 import java.lang.module.ModuleDescriptor.Exports; 29 import java.lang.module.ModuleDescriptor.Opens; 30 import java.lang.module.ModuleDescriptor.Provides; 31 import java.lang.module.ModuleDescriptor.Requires; 32 import java.lang.module.ModuleDescriptor.Version; 33 import java.util.ArrayList; 34 import java.util.Collections; 35 import java.util.HashMap; 36 import java.util.List; 37 import java.util.Map; 38 import java.util.Set; 39 40 import jdk.internal.misc.JavaLangModuleAccess; 41 import jdk.internal.misc.SharedSecrets; 42 43 /** 44 * This builder is optimized for reconstituting ModuleDescriptor 45 * for system modules. The validation should be done at jlink time. 46 * 47 * 1. skip name validation 48 * 2. ignores dependency hashes. 49 * 3. ModuleDescriptor skips the defensive copy and directly uses the 50 * sets/maps created in this Builder. 51 * 52 * SystemModules should contain modules for the boot layer. 53 */ 54 final class Builder { 55 private static final JavaLangModuleAccess jlma = 56 SharedSecrets.getJavaLangModuleAccess(); 57 58 // Static cache of the most recently seen Version to cheaply deduplicate 59 // most Version objects. JDK modules have the same version. 60 static Version cachedVersion; 61 62 /** 63 * Returns a {@link Requires} for a dependence on a module 64 * with the given (and possibly empty) set of modifiers. 65 */ 66 public static Requires newRequires(Set<Requires.Modifier> mods, 67 String mn) 68 { 69 return jlma.newRequires(mods, mn); 70 } 71 72 /** 73 * Returns a {@link Exports} for a qualified export, with 74 * the given (and possibly empty) set of modifiers, 75 * to a set of target modules. 76 */ 77 public static Exports newExports(Set<Exports.Modifier> ms, 78 String pn, 79 Set<String> targets) { 80 return jlma.newExports(ms, pn, targets); 81 } 82 83 /** 84 * Returns an {@link Opens} for an unqualified open with a given set of 85 * modifiers. 86 */ 87 public static Opens newOpens(Set<Opens.Modifier> ms, String pn) { 88 return jlma.newOpens(ms, pn); 89 } 90 91 /** 92 * Returns an {@link Opens} for a qualified opens, with 93 * the given (and possibly empty) set of modifiers, 94 * to a set of target modules. 95 */ 96 public static Opens newOpens(Set<Opens.Modifier> ms, 97 String pn, 98 Set<String> targets) { 99 return jlma.newOpens(ms, pn, targets); 100 } 101 102 /** 103 * Returns a {@link Exports} for an unqualified export with a given set 104 * of modifiers. 105 */ 106 public static Exports newExports(Set<Exports.Modifier> ms, String pn) { 107 return jlma.newExports(ms, pn); 108 } 109 110 /** 111 * Returns a {@link Provides} for a service with a given list of 112 * implementation classes. 113 */ 114 public static Provides newProvides(String st, List<String> pcs) { 115 return jlma.newProvides(st, pcs); 116 } 117 118 final String name; 119 boolean open; 120 boolean automatic; 121 boolean synthetic; 122 Set<Requires> requires; 123 Set<Exports> exports; 124 Set<Opens> opens; 125 Set<String> packages; 126 Set<String> uses; 127 Set<Provides> provides; 128 Version version; 129 String mainClass; 130 String osName; 131 String osArch; 132 String osVersion; 133 String algorithm; 134 Map<String, byte[]> hashes; 135 136 Builder(String name) { 137 this.name = name; 138 this.requires = Collections.emptySet(); 139 this.exports = Collections.emptySet(); 140 this.opens = Collections.emptySet(); 141 this.provides = Collections.emptySet(); 142 this.uses = Collections.emptySet(); 143 } 144 145 Builder open(boolean value) { 146 this.open = value; 147 return this; 148 } 149 150 Builder automatic(boolean value) { 151 this.automatic = value; 152 return this; 153 } 154 258 public Builder osArch(String arch) { 259 if (osArch != null) 260 throw new IllegalStateException("OS arch already set"); 261 this.osArch = arch; 262 return this; 263 } 264 265 /** 266 * Sets the OS version. 267 * 268 * @throws IllegalStateException if already set 269 */ 270 public Builder osVersion(String version) { 271 if (osVersion != null) 272 throw new IllegalStateException("OS version already set"); 273 this.osVersion = version; 274 return this; 275 } 276 277 /** 278 * Sets the algorithm of the module hashes 279 */ 280 public Builder algorithm(String algorithm) { 281 this.algorithm = algorithm; 282 return this; 283 } 284 285 /** 286 * Sets the module hash for the given module name 287 */ 288 public Builder moduleHash(String mn, byte[] hash) { 289 if (hashes == null) 290 hashes = new HashMap<>(); 291 292 hashes.put(mn, hash); 293 return this; 294 } 295 296 /** 297 * Builds a {@code ModuleDescriptor} from the components. 298 */ 299 public ModuleDescriptor build(int hashCode) { 300 assert name != null; 301 302 ModuleHashes moduleHashes = 303 hashes != null ? new ModuleHashes(algorithm, hashes) : null; 304 305 return jlma.newModuleDescriptor(name, 306 open, 307 automatic, 308 synthetic, 309 requires, 310 exports, 311 opens, 312 uses, 313 provides, 314 version, 315 mainClass, 316 osName, 317 osArch, 318 osVersion, 319 packages, 320 moduleHashes, 321 hashCode); 322 } 323 } | 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 package jdk.internal.module; 26 27 import java.lang.module.ModuleDescriptor; 28 import java.lang.module.ModuleDescriptor.Exports; 29 import java.lang.module.ModuleDescriptor.Opens; 30 import java.lang.module.ModuleDescriptor.Provides; 31 import java.lang.module.ModuleDescriptor.Requires; 32 import java.lang.module.ModuleDescriptor.Version; 33 import java.util.Collections; 34 import java.util.List; 35 import java.util.Set; 36 37 import jdk.internal.misc.JavaLangModuleAccess; 38 import jdk.internal.misc.SharedSecrets; 39 40 /** 41 * This builder is optimized for reconstituting ModuleDescriptor 42 * for system modules. The validation should be done at jlink time. 43 * 44 * 1. skip name validation 45 * 2. ignores dependency hashes. 46 * 3. ModuleDescriptor skips the defensive copy and directly uses the 47 * sets/maps created in this Builder. 48 * 49 * SystemModules should contain modules for the boot layer. 50 */ 51 final class Builder { 52 private static final JavaLangModuleAccess JLMA = 53 SharedSecrets.getJavaLangModuleAccess(); 54 55 // Static cache of the most recently seen Version to cheaply deduplicate 56 // most Version objects. JDK modules have the same version. 57 static Version cachedVersion; 58 59 /** 60 * Returns a {@link Requires} for a dependence on a module with the given 61 * (and possibly empty) set of modifiers, and optionally the version 62 * recorded at compile time. 63 */ 64 public static Requires newRequires(Set<Requires.Modifier> mods, 65 String mn, 66 String compiledVersion) 67 { 68 Version version = null; 69 if (compiledVersion != null) { 70 // use the cached version if the same version string 71 Version ver = cachedVersion; 72 if (ver != null && compiledVersion.equals(ver.toString())) { 73 version = ver; 74 } else { 75 version = Version.parse(compiledVersion); 76 } 77 } 78 return JLMA.newRequires(mods, mn, version); 79 } 80 81 /** 82 * Returns a {@link Requires} for a dependence on a module with the given 83 * (and possibly empty) set of modifiers, and optionally the version 84 * recorded at compile time. 85 */ 86 public static Requires newRequires(Set<Requires.Modifier> mods, 87 String mn) 88 { 89 return newRequires(mods, mn, null); 90 } 91 92 /** 93 * Returns a {@link Exports} for a qualified export, with 94 * the given (and possibly empty) set of modifiers, 95 * to a set of target modules. 96 */ 97 public static Exports newExports(Set<Exports.Modifier> ms, 98 String pn, 99 Set<String> targets) { 100 return JLMA.newExports(ms, pn, targets); 101 } 102 103 /** 104 * Returns an {@link Opens} for an unqualified open with a given set of 105 * modifiers. 106 */ 107 public static Opens newOpens(Set<Opens.Modifier> ms, String pn) { 108 return JLMA.newOpens(ms, pn); 109 } 110 111 /** 112 * Returns an {@link Opens} for a qualified opens, with 113 * the given (and possibly empty) set of modifiers, 114 * to a set of target modules. 115 */ 116 public static Opens newOpens(Set<Opens.Modifier> ms, 117 String pn, 118 Set<String> targets) { 119 return JLMA.newOpens(ms, pn, targets); 120 } 121 122 /** 123 * Returns a {@link Exports} for an unqualified export with a given set 124 * of modifiers. 125 */ 126 public static Exports newExports(Set<Exports.Modifier> ms, String pn) { 127 return JLMA.newExports(ms, pn); 128 } 129 130 /** 131 * Returns a {@link Provides} for a service with a given list of 132 * implementation classes. 133 */ 134 public static Provides newProvides(String st, List<String> pcs) { 135 return JLMA.newProvides(st, pcs); 136 } 137 138 final String name; 139 boolean open; 140 boolean automatic; 141 boolean synthetic; 142 Set<Requires> requires; 143 Set<Exports> exports; 144 Set<Opens> opens; 145 Set<String> packages; 146 Set<String> uses; 147 Set<Provides> provides; 148 Version version; 149 String mainClass; 150 String osName; 151 String osArch; 152 String osVersion; 153 154 Builder(String name) { 155 this.name = name; 156 this.requires = Collections.emptySet(); 157 this.exports = Collections.emptySet(); 158 this.opens = Collections.emptySet(); 159 this.provides = Collections.emptySet(); 160 this.uses = Collections.emptySet(); 161 } 162 163 Builder open(boolean value) { 164 this.open = value; 165 return this; 166 } 167 168 Builder automatic(boolean value) { 169 this.automatic = value; 170 return this; 171 } 172 276 public Builder osArch(String arch) { 277 if (osArch != null) 278 throw new IllegalStateException("OS arch already set"); 279 this.osArch = arch; 280 return this; 281 } 282 283 /** 284 * Sets the OS version. 285 * 286 * @throws IllegalStateException if already set 287 */ 288 public Builder osVersion(String version) { 289 if (osVersion != null) 290 throw new IllegalStateException("OS version already set"); 291 this.osVersion = version; 292 return this; 293 } 294 295 /** 296 * Builds a {@code ModuleDescriptor} from the components. 297 */ 298 public ModuleDescriptor build(int hashCode) { 299 assert name != null; 300 301 return JLMA.newModuleDescriptor(name, 302 version, 303 open, 304 automatic, 305 synthetic, 306 requires, 307 exports, 308 opens, 309 uses, 310 provides, 311 packages, 312 mainClass, 313 osName, 314 osArch, 315 osVersion, 316 hashCode); 317 } 318 } |