< prev index next >

src/java.base/share/classes/jdk/internal/module/Builder.java

Print this page




  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();


 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 
 173     Builder synthetic(boolean value) {
 174         this.synthetic = value;
 175         return this;
 176     }
 177 
 178     /**
 179      * Sets module exports.
 180      */
 181     public Builder exports(Exports[] exports) {
 182         this.exports = Set.of(exports);
 183         return this;
 184     }
 185 
 186     /**
 187      * Sets module opens.
 188      */
 189     public Builder opens(Opens[] opens) {
 190         this.opens = Set.of(opens);
 191         return this;
 192     }


 211      * Sets the set of service dependences.
 212      */
 213     public Builder uses(Set<String> uses) {
 214         this.uses = uses;
 215         return this;
 216     }
 217 
 218     /**
 219      * Sets module provides.
 220      */
 221     public Builder provides(Provides[] provides) {
 222         this.provides = Set.of(provides);
 223         return this;
 224     }
 225 
 226     /**
 227      * Sets the module version.
 228      *
 229      * @throws IllegalArgumentException if {@code v} is null or cannot be
 230      *         parsed as a version string
 231      * @throws IllegalStateException if the module version is already set
 232      *
 233      * @see Version#parse(String)
 234      */
 235     public Builder version(String v) {
 236         if (version != null)
 237             throw new IllegalStateException("module version already set");
 238         Version ver = cachedVersion;
 239         if (ver != null && v.equals(ver.toString())) {
 240             version = ver;
 241         } else {
 242             cachedVersion = version = Version.parse(v);
 243         }
 244         return this;
 245     }
 246 
 247     /**
 248      * Sets the module main class.
 249      *
 250      * @throws IllegalStateException if already set
 251      */
 252     public Builder mainClass(String mc) {
 253         if (mainClass != null)
 254             throw new IllegalStateException("main class already set");
 255         mainClass = mc;
 256         return this;
 257     }
 258 
 259     /**
 260      * Sets the OS name.
 261      *
 262      * @throws IllegalStateException if already set
 263      */
 264     public Builder osName(String name) {
 265         if (osName != null)
 266             throw new IllegalStateException("OS name already set");
 267         this.osName = name;
 268         return this;
 269     }
 270 
 271     /**
 272      * Sets the OS arch.
 273      *
 274      * @throws IllegalStateException if already set
 275      */
 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 }


  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.HashSet;
  35 import java.util.List;
  36 import java.util.Set;
  37 
  38 import jdk.internal.misc.JavaLangModuleAccess;
  39 import jdk.internal.misc.SharedSecrets;
  40 
  41 /**
  42  * This builder is optimized for reconstituting ModuleDescriptor
  43  * for system modules.  The validation should be done at jlink time.
  44  *
  45  * 1. skip name validation
  46  * 2. ignores dependency hashes.
  47  * 3. ModuleDescriptor skips the defensive copy and directly uses the
  48  *    sets/maps created in this Builder.
  49  *
  50  * SystemModules should contain modules for the boot layer.
  51  */
  52 final class Builder {
  53     private static final JavaLangModuleAccess JLMA =
  54         SharedSecrets.getJavaLangModuleAccess();


 121     }
 122 
 123     /**
 124      * Returns a {@link Exports} for an unqualified export with a given set
 125      * of modifiers.
 126      */
 127     public static Exports newExports(Set<Exports.Modifier> ms, String pn) {
 128         return JLMA.newExports(ms, pn);
 129     }
 130 
 131     /**
 132      * Returns a {@link Provides} for a service with a given list of
 133      * implementation classes.
 134      */
 135     public static Provides newProvides(String st, List<String> pcs) {
 136         return JLMA.newProvides(st, pcs);
 137     }
 138 
 139     final String name;
 140     boolean open;

 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 synthetic(boolean value) {
 169         this.synthetic = value;
 170         return this;
 171     }
 172 
 173     /**
 174      * Sets module exports.
 175      */
 176     public Builder exports(Exports[] exports) {
 177         this.exports = Set.of(exports);
 178         return this;
 179     }
 180 
 181     /**
 182      * Sets module opens.
 183      */
 184     public Builder opens(Opens[] opens) {
 185         this.opens = Set.of(opens);
 186         return this;
 187     }


 206      * Sets the set of service dependences.
 207      */
 208     public Builder uses(Set<String> uses) {
 209         this.uses = uses;
 210         return this;
 211     }
 212 
 213     /**
 214      * Sets module provides.
 215      */
 216     public Builder provides(Provides[] provides) {
 217         this.provides = Set.of(provides);
 218         return this;
 219     }
 220 
 221     /**
 222      * Sets the module version.
 223      *
 224      * @throws IllegalArgumentException if {@code v} is null or cannot be
 225      *         parsed as a version string

 226      *
 227      * @see Version#parse(String)
 228      */
 229     public Builder version(String v) {


 230         Version ver = cachedVersion;
 231         if (ver != null && v.equals(ver.toString())) {
 232             version = ver;
 233         } else {
 234             cachedVersion = version = Version.parse(v);
 235         }
 236         return this;
 237     }
 238 
 239     /**
 240      * Sets the module main class.


 241      */
 242     public Builder mainClass(String mc) {


 243         mainClass = mc;
 244         return this;
 245     }
 246 
 247     /**
 248      * Sets the OS name.


 249      */
 250     public Builder osName(String name) {


 251         this.osName = name;
 252         return this;
 253     }
 254 
 255     /**
 256      * Sets the OS arch.


 257      */
 258     public Builder osArch(String arch) {


 259         this.osArch = arch;
 260         return this;
 261     }
 262 
 263     /**
 264      * Sets the OS version.
 265      *
 266      * @throws IllegalStateException if already set
 267      */
 268     public Builder osVersion(String version) {


 269         this.osVersion = version;
 270         return this;
 271     }
 272 
 273     /**
 274      * Builds a {@code ModuleDescriptor} from the components.
 275      */
 276     public ModuleDescriptor build(int hashCode) {
 277         assert name != null;
 278 
 279         Set<ModuleDescriptor.Modifier> modifiers;
 280         if (open || synthetic) {
 281             modifiers = new HashSet<>();
 282             if (open) modifiers.add(ModuleDescriptor.Modifier.OPEN);
 283             if (synthetic) modifiers.add(ModuleDescriptor.Modifier.SYNTHETIC);
 284             modifiers = Collections.unmodifiableSet(modifiers);
 285         } else {
 286             modifiers = Collections.emptySet();
 287         }
 288 
 289         return JLMA.newModuleDescriptor(name,
 290                                         version,
 291                                         modifiers,


 292                                         requires,
 293                                         exports,
 294                                         opens,
 295                                         uses,
 296                                         provides,
 297                                         packages,
 298                                         mainClass,
 299                                         osName,
 300                                         osArch,
 301                                         osVersion,
 302                                         hashCode);
 303     }
 304 }
< prev index next >