< prev index next >

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

Print this page
@@ -27,10 +27,11 @@
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.PrintStream;
  import java.io.UncheckedIOException;
+ import java.lang.reflect.AccessFlag;
  import java.nio.ByteBuffer;
  import java.nio.file.Path;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collection;

@@ -103,32 +104,37 @@
      public enum Modifier {
          /**
           * An open module. An open module does not declare any open packages
           * but the resulting module is treated as if all packages are open.
           */
-         OPEN,
+         OPEN(AccessFlag.OPEN.mask()),
  
          /**
           * An automatic module. An automatic module is treated as if it exports
           * and opens all packages.
           *
           * @apiNote This modifier does not correspond to a module flag in the
           * binary form of a module declaration ({@code module-info.class}).
           */
-         AUTOMATIC,
+         AUTOMATIC(0 /* no flag per above comment */),
  
          /**
           * The module was not explicitly or implicitly declared.
           */
-         SYNTHETIC,
+         SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
  
          /**
           * The module was implicitly declared.
           */
-         MANDATED;
-     }
+         MANDATED(AccessFlag.MANDATED.mask());
  
+         private int mask;
+         private Modifier(int mask) {
+             this.mask = mask;
+         }
+         private int mask() {return mask;}
+     }
  
      /**
       * <p> A dependence upon a module. </p>
       *
       * @see ModuleDescriptor#requires()

@@ -150,32 +156,35 @@
              /**
               * The dependence causes any module which depends on the <i>current
               * module</i> to have an implicitly declared dependence on the module
               * named by the {@code Requires}.
               */
-             TRANSITIVE,
+             TRANSITIVE(AccessFlag.TRANSITIVE.mask()),
  
              /**
               * The dependence is mandatory in the static phase, during compilation,
               * but is optional in the dynamic phase, during execution.
               */
-             STATIC,
+             STATIC(AccessFlag.STATIC.mask()),
  
              /**
               * The dependence was not explicitly or implicitly declared in the
               * source of the module declaration.
               */
-             SYNTHETIC,
+             SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
  
              /**
               * The dependence was implicitly declared in the source of the module
               * declaration.
               */
-             MANDATED;
- 
+             MANDATED(AccessFlag.MANDATED.mask());
+             private int mask;
+             private Modifier(int mask) {
+                 this.mask = mask;
+             }
+             private int mask() {return mask;}
          }
- 
          private final Set<Modifier> mods;
          private final String name;
          private final Version compiledVersion;
          private final String rawCompiledVersion;
  

@@ -201,10 +210,25 @@
           */
          public Set<Modifier> modifiers() {
              return mods;
          }
  
+         /**
+          * {@return an unmodifiable set of the module {@linkplain AccessFlag
+          * requires flags, possibly empty}}
+          * @see #modifiers()
+          * @jvms 4.7.25 The Module Attribute
+          * @since 20
+          */
+         public Set<AccessFlag> accessFlags() {
+             int mask = 0;
+             for (var modifier : mods) {
+                 mask |= modifier.mask();
+             }
+             return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE_REQUIRES);
+         }
+ 
          /**
           * Return the module name.
           *
           * @return The module name
           */

@@ -374,18 +398,23 @@
  
              /**
               * The export was not explicitly or implicitly declared in the
               * source of the module declaration.
               */
-             SYNTHETIC,
+             SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
  
              /**
               * The export was implicitly declared in the source of the module
               * declaration.
               */
-             MANDATED;
+             MANDATED(AccessFlag.MANDATED.mask());
  
+             private int mask;
+             private Modifier(int mask) {
+                 this.mask = mask;
+             }
+             private int mask() {return mask;}
          }
  
          private final Set<Modifier> mods;
          private final String source;
          private final Set<String> targets;  // empty if unqualified export

@@ -415,10 +444,25 @@
           */
          public Set<Modifier> modifiers() {
              return mods;
          }
  
+         /**
+          * {@return an unmodifiable set of the module {@linkplain AccessFlag
+          * export flags} for this module descriptor, possibly empty}
+          * @see #modifiers()
+          * @jvms 4.7.25 The Module Attribute
+          * @since 20
+          */
+         public Set<AccessFlag> accessFlags() {
+             int mask = 0;
+             for (var modifier : mods) {
+                 mask |= modifier.mask();
+             }
+             return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE_EXPORTS);
+         }
+ 
          /**
           * Returns {@code true} if this is a qualified export.
           *
           * @return {@code true} if this is a qualified export
           */

@@ -577,18 +621,22 @@
  
              /**
               * The open package was not explicitly or implicitly declared in
               * the source of the module declaration.
               */
-             SYNTHETIC,
+             SYNTHETIC(AccessFlag.SYNTHETIC.mask()),
  
              /**
               * The open package was implicitly declared in the source of the
               * module declaration.
               */
-             MANDATED;
- 
+             MANDATED(AccessFlag.MANDATED.mask());
+             private int mask;
+             private Modifier(int mask) {
+                 this.mask = mask;
+             }
+             private int mask() {return mask;}
          }
  
          private final Set<Modifier> mods;
          private final String source;
          private final Set<String> targets;  // empty if unqualified export

@@ -618,10 +666,25 @@
           */
          public Set<Modifier> modifiers() {
              return mods;
          }
  
+         /**
+          * {@return an unmodifiable set of the module {@linkplain AccessFlag
+          * opens flags}, possibly empty}
+          * @see #modifiers()
+          * @jvms 4.7.25 The Module Attribute
+          * @since 20
+          */
+         public Set<AccessFlag> accessFlags() {
+             int mask = 0;
+             for (var modifier : mods) {
+                 mask |= modifier.mask();
+             }
+             return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE_OPENS);
+         }
+ 
          /**
           * Returns {@code true} if this is a qualified {@code Opens}.
           *
           * @return {@code true} if this is a qualified {@code Opens}
           */

@@ -1288,10 +1351,25 @@
       */
      public Set<Modifier> modifiers() {
          return modifiers;
      }
  
+     /**
+      * {@return an unmodifiable set of the {@linkplain AccessFlag
+      * module flags}, possibly empty}
+      * @see #modifiers()
+      * @jvms 4.7.25 The Module Attribute
+      * @since 20
+      */
+     public Set<AccessFlag> accessFlags() {
+         int mask = 0;
+         for (var modifier : modifiers) {
+             mask |= modifier.mask();
+         }
+         return AccessFlag.maskToAccessFlags(mask, AccessFlag.Location.MODULE);
+     }
+ 
      /**
       * <p> Returns {@code true} if this is an open module. </p>
       *
       * <p> This method is equivalent to testing if the set of {@link #modifiers()
       * modifiers} contains the {@link Modifier#OPEN OPEN} modifier. </p>
< prev index next >