< prev index next >

src/java.base/share/classes/java/lang/module/ModuleDescriptor.java

Print this page

  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 java.lang.module;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.io.UncheckedIOException;

  32 import java.nio.ByteBuffer;
  33 import java.nio.file.Path;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.Collection;
  37 import java.util.Collections;
  38 import java.util.EnumSet;
  39 import java.util.HashMap;
  40 import java.util.HashSet;
  41 import java.util.List;
  42 import java.util.Locale;
  43 import java.util.Map;
  44 import java.util.Objects;
  45 import java.util.Optional;
  46 import java.util.Set;
  47 import java.util.function.Supplier;
  48 import java.util.stream.Collectors;
  49 import java.util.stream.Stream;
  50 
  51 import static jdk.internal.module.Checks.*;

  88  *
  89  * @see java.lang.Module
  90  * @since 9
  91  */
  92 
  93 public class ModuleDescriptor
  94     implements Comparable<ModuleDescriptor>
  95 {
  96 
  97     /**
  98      * A modifier on a module.
  99      *
 100      * @see ModuleDescriptor#modifiers()
 101      * @since 9
 102      */
 103     public enum Modifier {
 104         /**
 105          * An open module. An open module does not declare any open packages
 106          * but the resulting module is treated as if all packages are open.
 107          */
 108         OPEN,
 109 
 110         /**
 111          * An automatic module. An automatic module is treated as if it exports
 112          * and opens all packages.
 113          *
 114          * @apiNote This modifier does not correspond to a module flag in the
 115          * binary form of a module declaration ({@code module-info.class}).
 116          */
 117         AUTOMATIC,
 118 
 119         /**
 120          * The module was not explicitly or implicitly declared.
 121          */
 122         SYNTHETIC,
 123 
 124         /**
 125          * The module was implicitly declared.
 126          */
 127         MANDATED;
 128     }
 129 






 130 
 131     /**
 132      * <p> A dependence upon a module. </p>
 133      *
 134      * @see ModuleDescriptor#requires()
 135      * @since 9
 136      */
 137 
 138     public static final class Requires
 139         implements Comparable<Requires>
 140     {
 141 
 142         /**
 143          * A modifier on a module dependence.
 144          *
 145          * @see Requires#modifiers()
 146          * @since 9
 147          */
 148         public enum Modifier {
 149 
 150             /**
 151              * The dependence causes any module which depends on the <i>current
 152              * module</i> to have an implicitly declared dependence on the module
 153              * named by the {@code Requires}.
 154              */
 155             TRANSITIVE,
 156 
 157             /**
 158              * The dependence is mandatory in the static phase, during compilation,
 159              * but is optional in the dynamic phase, during execution.
 160              */
 161             STATIC,
 162 
 163             /**
 164              * The dependence was not explicitly or implicitly declared in the
 165              * source of the module declaration.
 166              */
 167             SYNTHETIC,
 168 
 169             /**
 170              * The dependence was implicitly declared in the source of the module
 171              * declaration.
 172              */
 173             MANDATED;
 174 




 175         }
 176 
 177         private final Set<Modifier> mods;
 178         private final String name;
 179         private final Version compiledVersion;
 180         private final String rawCompiledVersion;
 181 
 182         private Requires(Set<Modifier> ms, String mn, Version v, String vs) {
 183             assert v == null || vs == null;
 184             this.mods = Set.copyOf(ms);
 185             this.name = mn;
 186             this.compiledVersion = v;
 187             this.rawCompiledVersion = vs;
 188         }
 189 
 190         private Requires(Set<Modifier> ms, String mn, Version v, boolean unused) {
 191             this.mods = ms;
 192             this.name = mn;
 193             this.compiledVersion = v;
 194             this.rawCompiledVersion = null;
 195         }
 196 
 197         /**
 198          * Returns the set of modifiers.
 199          *
 200          * @return A possibly-empty unmodifiable set of modifiers
 201          */
 202         public Set<Modifier> modifiers() {
 203             return mods;
 204         }
 205 















 206         /**
 207          * Return the module name.
 208          *
 209          * @return The module name
 210          */
 211         public String name() {
 212             return name;
 213         }
 214 
 215         /**
 216          * Returns the version of the module if recorded at compile-time.
 217          *
 218          * @return The version of the module if recorded at compile-time,
 219          *         or an empty {@code Optional} if no version was recorded or
 220          *         the version string recorded is {@linkplain Version#parse(String)
 221          *         unparseable}
 222          */
 223         public Optional<Version> compiledVersion() {
 224             return Optional.ofNullable(compiledVersion);
 225         }

 359      * @see ModuleDescriptor#exports()
 360      * @since 9
 361      */
 362 
 363     public static final class Exports
 364         implements Comparable<Exports>
 365     {
 366 
 367         /**
 368          * A modifier on an exported package.
 369          *
 370          * @see Exports#modifiers()
 371          * @since 9
 372          */
 373         public enum Modifier {
 374 
 375             /**
 376              * The export was not explicitly or implicitly declared in the
 377              * source of the module declaration.
 378              */
 379             SYNTHETIC,
 380 
 381             /**
 382              * The export was implicitly declared in the source of the module
 383              * declaration.
 384              */
 385             MANDATED;
 386 





 387         }
 388 
 389         private final Set<Modifier> mods;
 390         private final String source;
 391         private final Set<String> targets;  // empty if unqualified export
 392 
 393         /**
 394          * Constructs an export
 395          */
 396         private Exports(Set<Modifier> ms, String source, Set<String> targets) {
 397             this.mods = Set.copyOf(ms);
 398             this.source = source;
 399             this.targets = Set.copyOf(targets);
 400         }
 401 
 402         private Exports(Set<Modifier> ms,
 403                         String source,
 404                         Set<String> targets,
 405                         boolean unused) {
 406             this.mods = ms;
 407             this.source = source;
 408             this.targets = targets;
 409         }
 410 
 411         /**
 412          * Returns the set of modifiers.
 413          *
 414          * @return A possibly-empty unmodifiable set of modifiers
 415          */
 416         public Set<Modifier> modifiers() {
 417             return mods;
 418         }
 419 















 420         /**
 421          * Returns {@code true} if this is a qualified export.
 422          *
 423          * @return {@code true} if this is a qualified export
 424          */
 425         public boolean isQualified() {
 426             return !targets.isEmpty();
 427         }
 428 
 429         /**
 430          * Returns the package name.
 431          *
 432          * @return The package name
 433          */
 434         public String source() {
 435             return source;
 436         }
 437 
 438         /**
 439          * For a qualified export, returns the non-empty and immutable set

 562      *
 563      * @see ModuleDescriptor#opens()
 564      * @since 9
 565      */
 566 
 567     public static final class Opens
 568         implements Comparable<Opens>
 569     {
 570         /**
 571          * A modifier on an open package.
 572          *
 573          * @see Opens#modifiers()
 574          * @since 9
 575          */
 576         public enum Modifier {
 577 
 578             /**
 579              * The open package was not explicitly or implicitly declared in
 580              * the source of the module declaration.
 581              */
 582             SYNTHETIC,
 583 
 584             /**
 585              * The open package was implicitly declared in the source of the
 586              * module declaration.
 587              */
 588             MANDATED;
 589 




 590         }
 591 
 592         private final Set<Modifier> mods;
 593         private final String source;
 594         private final Set<String> targets;  // empty if unqualified export
 595 
 596         /**
 597          * Constructs an {@code Opens}.
 598          */
 599         private Opens(Set<Modifier> ms, String source, Set<String> targets) {
 600             this.mods = Set.copyOf(ms);
 601             this.source = source;
 602             this.targets = Set.copyOf(targets);
 603         }
 604 
 605         private Opens(Set<Modifier> ms,
 606                       String source,
 607                       Set<String> targets,
 608                       boolean unused) {
 609             this.mods = ms;
 610             this.source = source;
 611             this.targets = targets;
 612         }
 613 
 614         /**
 615          * Returns the set of modifiers.
 616          *
 617          * @return A possibly-empty unmodifiable set of modifiers
 618          */
 619         public Set<Modifier> modifiers() {
 620             return mods;
 621         }
 622 















 623         /**
 624          * Returns {@code true} if this is a qualified {@code Opens}.
 625          *
 626          * @return {@code true} if this is a qualified {@code Opens}
 627          */
 628         public boolean isQualified() {
 629             return !targets.isEmpty();
 630         }
 631 
 632         /**
 633          * Returns the package name.
 634          *
 635          * @return The package name
 636          */
 637         public String source() {
 638             return source;
 639         }
 640 
 641         /**
 642          * For a qualified {@code Opens}, returns the non-empty and immutable set

1273     }
1274 
1275     /**
1276      * <p> Returns the module name. </p>
1277      *
1278      * @return The module name
1279      */
1280     public String name() {
1281         return name;
1282     }
1283 
1284     /**
1285      * <p> Returns the set of module modifiers. </p>
1286      *
1287      * @return A possibly-empty unmodifiable set of modifiers
1288      */
1289     public Set<Modifier> modifiers() {
1290         return modifiers;
1291     }
1292 















1293     /**
1294      * <p> Returns {@code true} if this is an open module. </p>
1295      *
1296      * <p> This method is equivalent to testing if the set of {@link #modifiers()
1297      * modifiers} contains the {@link Modifier#OPEN OPEN} modifier. </p>
1298      *
1299      * @return  {@code true} if this is an open module
1300      */
1301     public boolean isOpen() {
1302         return open;
1303     }
1304 
1305     /**
1306      * <p> Returns {@code true} if this is an automatic module. </p>
1307      *
1308      * <p> This method is equivalent to testing if the set of {@link #modifiers()
1309      * modifiers} contains the {@link Modifier#AUTOMATIC AUTOMATIC} modifier. </p>
1310      *
1311      * @return  {@code true} if this is an automatic module
1312      */

  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 java.lang.module;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.io.UncheckedIOException;
  32 import java.lang.reflect.AccessFlag;
  33 import java.nio.ByteBuffer;
  34 import java.nio.file.Path;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.Collection;
  38 import java.util.Collections;
  39 import java.util.EnumSet;
  40 import java.util.HashMap;
  41 import java.util.HashSet;
  42 import java.util.List;
  43 import java.util.Locale;
  44 import java.util.Map;
  45 import java.util.Objects;
  46 import java.util.Optional;
  47 import java.util.Set;
  48 import java.util.function.Supplier;
  49 import java.util.stream.Collectors;
  50 import java.util.stream.Stream;
  51 
  52 import static jdk.internal.module.Checks.*;

  89  *
  90  * @see java.lang.Module
  91  * @since 9
  92  */
  93 
  94 public class ModuleDescriptor
  95     implements Comparable<ModuleDescriptor>
  96 {
  97 
  98     /**
  99      * A modifier on a module.
 100      *
 101      * @see ModuleDescriptor#modifiers()
 102      * @since 9
 103      */
 104     public enum Modifier {
 105         /**
 106          * An open module. An open module does not declare any open packages
 107          * but the resulting module is treated as if all packages are open.
 108          */
 109         OPEN(AccessFlag.OPEN.mask()),
 110 
 111         /**
 112          * An automatic module. An automatic module is treated as if it exports
 113          * and opens all packages.
 114          *
 115          * @apiNote This modifier does not correspond to a module flag in the
 116          * binary form of a module declaration ({@code module-info.class}).
 117          */
 118         AUTOMATIC(0 /* no flag per above comment */),
 119 
 120         /**
 121          * The module was not explicitly or implicitly declared.
 122          */
 123         SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
 124 
 125         /**
 126          * The module was implicitly declared.
 127          */
 128         MANDATED(AccessFlag.MANDATED.mask());

 129 
 130         private int mask;
 131         private Modifier(int mask) {
 132             this.mask = mask;
 133         }
 134         private int mask() {return mask;}
 135     }
 136 
 137     /**
 138      * <p> A dependence upon a module. </p>
 139      *
 140      * @see ModuleDescriptor#requires()
 141      * @since 9
 142      */
 143 
 144     public static final class Requires
 145         implements Comparable<Requires>
 146     {
 147 
 148         /**
 149          * A modifier on a module dependence.
 150          *
 151          * @see Requires#modifiers()
 152          * @since 9
 153          */
 154         public enum Modifier {
 155 
 156             /**
 157              * The dependence causes any module which depends on the <i>current
 158              * module</i> to have an implicitly declared dependence on the module
 159              * named by the {@code Requires}.
 160              */
 161             TRANSITIVE(AccessFlag.TRANSITIVE.mask()),
 162 
 163             /**
 164              * The dependence is mandatory in the static phase, during compilation,
 165              * but is optional in the dynamic phase, during execution.
 166              */
 167             STATIC(AccessFlag.STATIC.mask()),
 168 
 169             /**
 170              * The dependence was not explicitly or implicitly declared in the
 171              * source of the module declaration.
 172              */
 173             SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
 174 
 175             /**
 176              * The dependence was implicitly declared in the source of the module
 177              * declaration.
 178              */
 179             MANDATED(AccessFlag.MANDATED.mask());
 180             private int mask;
 181             private Modifier(int mask) {
 182                 this.mask = mask;
 183             }
 184             private int mask() {return mask;}
 185         }

 186         private final Set<Modifier> mods;
 187         private final String name;
 188         private final Version compiledVersion;
 189         private final String rawCompiledVersion;
 190 
 191         private Requires(Set<Modifier> ms, String mn, Version v, String vs) {
 192             assert v == null || vs == null;
 193             this.mods = Set.copyOf(ms);
 194             this.name = mn;
 195             this.compiledVersion = v;
 196             this.rawCompiledVersion = vs;
 197         }
 198 
 199         private Requires(Set<Modifier> ms, String mn, Version v, boolean unused) {
 200             this.mods = ms;
 201             this.name = mn;
 202             this.compiledVersion = v;
 203             this.rawCompiledVersion = null;
 204         }
 205 
 206         /**
 207          * Returns the set of modifiers.
 208          *
 209          * @return A possibly-empty unmodifiable set of modifiers
 210          */
 211         public Set<Modifier> modifiers() {
 212             return mods;
 213         }
 214 
 215         /**
 216          * {@return an unmodifiable set of the module {@linkplain AccessFlag
 217          * requires flags, possibly empty}}
 218          * @see #modifiers()
 219          * @jvms 4.7.25 The Module Attribute
 220          * @since 20
 221          */
 222         public Set<AccessFlag> accessFlags() {
 223             int mask = 0;
 224             for (var modifier : mods) {
 225                 mask |= modifier.mask();
 226             }
 227             return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE_REQUIRES);
 228         }
 229 
 230         /**
 231          * Return the module name.
 232          *
 233          * @return The module name
 234          */
 235         public String name() {
 236             return name;
 237         }
 238 
 239         /**
 240          * Returns the version of the module if recorded at compile-time.
 241          *
 242          * @return The version of the module if recorded at compile-time,
 243          *         or an empty {@code Optional} if no version was recorded or
 244          *         the version string recorded is {@linkplain Version#parse(String)
 245          *         unparseable}
 246          */
 247         public Optional<Version> compiledVersion() {
 248             return Optional.ofNullable(compiledVersion);
 249         }

 383      * @see ModuleDescriptor#exports()
 384      * @since 9
 385      */
 386 
 387     public static final class Exports
 388         implements Comparable<Exports>
 389     {
 390 
 391         /**
 392          * A modifier on an exported package.
 393          *
 394          * @see Exports#modifiers()
 395          * @since 9
 396          */
 397         public enum Modifier {
 398 
 399             /**
 400              * The export was not explicitly or implicitly declared in the
 401              * source of the module declaration.
 402              */
 403             SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
 404 
 405             /**
 406              * The export was implicitly declared in the source of the module
 407              * declaration.
 408              */
 409             MANDATED(AccessFlag.MANDATED.mask());
 410 
 411             private int mask;
 412             private Modifier(int mask) {
 413                 this.mask = mask;
 414             }
 415             private int mask() {return mask;}
 416         }
 417 
 418         private final Set<Modifier> mods;
 419         private final String source;
 420         private final Set<String> targets;  // empty if unqualified export
 421 
 422         /**
 423          * Constructs an export
 424          */
 425         private Exports(Set<Modifier> ms, String source, Set<String> targets) {
 426             this.mods = Set.copyOf(ms);
 427             this.source = source;
 428             this.targets = Set.copyOf(targets);
 429         }
 430 
 431         private Exports(Set<Modifier> ms,
 432                         String source,
 433                         Set<String> targets,
 434                         boolean unused) {
 435             this.mods = ms;
 436             this.source = source;
 437             this.targets = targets;
 438         }
 439 
 440         /**
 441          * Returns the set of modifiers.
 442          *
 443          * @return A possibly-empty unmodifiable set of modifiers
 444          */
 445         public Set<Modifier> modifiers() {
 446             return mods;
 447         }
 448 
 449         /**
 450          * {@return an unmodifiable set of the module {@linkplain AccessFlag
 451          * export flags} for this module descriptor, possibly empty}
 452          * @see #modifiers()
 453          * @jvms 4.7.25 The Module Attribute
 454          * @since 20
 455          */
 456         public Set<AccessFlag> accessFlags() {
 457             int mask = 0;
 458             for (var modifier : mods) {
 459                 mask |= modifier.mask();
 460             }
 461             return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE_EXPORTS);
 462         }
 463 
 464         /**
 465          * Returns {@code true} if this is a qualified export.
 466          *
 467          * @return {@code true} if this is a qualified export
 468          */
 469         public boolean isQualified() {
 470             return !targets.isEmpty();
 471         }
 472 
 473         /**
 474          * Returns the package name.
 475          *
 476          * @return The package name
 477          */
 478         public String source() {
 479             return source;
 480         }
 481 
 482         /**
 483          * For a qualified export, returns the non-empty and immutable set

 606      *
 607      * @see ModuleDescriptor#opens()
 608      * @since 9
 609      */
 610 
 611     public static final class Opens
 612         implements Comparable<Opens>
 613     {
 614         /**
 615          * A modifier on an open package.
 616          *
 617          * @see Opens#modifiers()
 618          * @since 9
 619          */
 620         public enum Modifier {
 621 
 622             /**
 623              * The open package was not explicitly or implicitly declared in
 624              * the source of the module declaration.
 625              */
 626             SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
 627 
 628             /**
 629              * The open package was implicitly declared in the source of the
 630              * module declaration.
 631              */
 632             MANDATED(AccessFlag.MANDATED.mask());
 633             private int mask;
 634             private Modifier(int mask) {
 635                 this.mask = mask;
 636             }
 637             private int mask() {return mask;}
 638         }
 639 
 640         private final Set<Modifier> mods;
 641         private final String source;
 642         private final Set<String> targets;  // empty if unqualified export
 643 
 644         /**
 645          * Constructs an {@code Opens}.
 646          */
 647         private Opens(Set<Modifier> ms, String source, Set<String> targets) {
 648             this.mods = Set.copyOf(ms);
 649             this.source = source;
 650             this.targets = Set.copyOf(targets);
 651         }
 652 
 653         private Opens(Set<Modifier> ms,
 654                       String source,
 655                       Set<String> targets,
 656                       boolean unused) {
 657             this.mods = ms;
 658             this.source = source;
 659             this.targets = targets;
 660         }
 661 
 662         /**
 663          * Returns the set of modifiers.
 664          *
 665          * @return A possibly-empty unmodifiable set of modifiers
 666          */
 667         public Set<Modifier> modifiers() {
 668             return mods;
 669         }
 670 
 671         /**
 672          * {@return an unmodifiable set of the module {@linkplain AccessFlag
 673          * opens flags}, possibly empty}
 674          * @see #modifiers()
 675          * @jvms 4.7.25 The Module Attribute
 676          * @since 20
 677          */
 678         public Set<AccessFlag> accessFlags() {
 679             int mask = 0;
 680             for (var modifier : mods) {
 681                 mask |= modifier.mask();
 682             }
 683             return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE_OPENS);
 684         }
 685 
 686         /**
 687          * Returns {@code true} if this is a qualified {@code Opens}.
 688          *
 689          * @return {@code true} if this is a qualified {@code Opens}
 690          */
 691         public boolean isQualified() {
 692             return !targets.isEmpty();
 693         }
 694 
 695         /**
 696          * Returns the package name.
 697          *
 698          * @return The package name
 699          */
 700         public String source() {
 701             return source;
 702         }
 703 
 704         /**
 705          * For a qualified {@code Opens}, returns the non-empty and immutable set

1336     }
1337 
1338     /**
1339      * <p> Returns the module name. </p>
1340      *
1341      * @return The module name
1342      */
1343     public String name() {
1344         return name;
1345     }
1346 
1347     /**
1348      * <p> Returns the set of module modifiers. </p>
1349      *
1350      * @return A possibly-empty unmodifiable set of modifiers
1351      */
1352     public Set<Modifier> modifiers() {
1353         return modifiers;
1354     }
1355 
1356     /**
1357      * {@return an unmodifiable set of the {@linkplain AccessFlag
1358      * module flags}, possibly empty}
1359      * @see #modifiers()
1360      * @jvms 4.7.25 The Module Attribute
1361      * @since 20
1362      */
1363     public Set<AccessFlag> accessFlags() {
1364         int mask = 0;
1365         for (var modifier : modifiers) {
1366             mask |= modifier.mask();
1367         }
1368         return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE);
1369     }
1370 
1371     /**
1372      * <p> Returns {@code true} if this is an open module. </p>
1373      *
1374      * <p> This method is equivalent to testing if the set of {@link #modifiers()
1375      * modifiers} contains the {@link Modifier#OPEN OPEN} modifier. </p>
1376      *
1377      * @return  {@code true} if this is an open module
1378      */
1379     public boolean isOpen() {
1380         return open;
1381     }
1382 
1383     /**
1384      * <p> Returns {@code true} if this is an automatic module. </p>
1385      *
1386      * <p> This method is equivalent to testing if the set of {@link #modifiers()
1387      * modifiers} contains the {@link Modifier#AUTOMATIC AUTOMATIC} modifier. </p>
1388      *
1389      * @return  {@code true} if this is an automatic module
1390      */
< prev index next >