< prev index next >

src/java.base/share/classes/java/lang/Module.java

Print this page
rev 48077 : 8193128: Reduce number of implementation classes returned by List/Set/Map.of()
Reviewed-by: smarks

@@ -245,17 +245,20 @@
         return null;
     }
 
     // --
 
+    private static class Special {
     // special Module to mean "all unnamed modules"
-    private static final Module ALL_UNNAMED_MODULE = new Module(null);
-    private static final Set<Module> ALL_UNNAMED_MODULE_SET = Set.of(ALL_UNNAMED_MODULE);
+        static final Module ALL_UNNAMED_MODULE = new Module(null);
+        static final Set<Module> ALL_UNNAMED_MODULE_SET = Set.of(ALL_UNNAMED_MODULE);
 
     // special Module to mean "everyone"
-    private static final Module EVERYONE_MODULE = new Module(null);
-    private static final Set<Module> EVERYONE_SET = Set.of(EVERYONE_MODULE);
+        static final Module EVERYONE_MODULE = new Module(null);
+        static final Set<Module> EVERYONE_SET = Set.of(EVERYONE_MODULE);
+    }
+
 
     /**
      * The holder of data structures to support readability, exports, and
      * service use added at runtime with the reflective APIs.
      */

@@ -323,11 +326,11 @@
             return true;
 
         // if other is an unnamed module then check if this module reads
         // all unnamed modules
         if (!other.isNamed()
-            && ReflectionData.reads.containsKeyPair(this, ALL_UNNAMED_MODULE))
+            && ReflectionData.reads.containsKeyPair(this, Special.ALL_UNNAMED_MODULE))
             return true;
 
         return false;
     }
 

@@ -380,11 +383,11 @@
      * Updates this module to read all unnamed modules.
      *
      * @apiNote Used by the --add-reads command line option.
      */
     void implAddReadsAllUnnamed() {
-        implAddReads(Module.ALL_UNNAMED_MODULE, true);
+        implAddReads(Special.ALL_UNNAMED_MODULE, true);
     }
 
     /**
      * Updates this module to read another module without notifying the VM.
      *

@@ -402,11 +405,11 @@
     private void implAddReads(Module other, boolean syncVM) {
         Objects.requireNonNull(other);
         if (!canRead(other)) {
             // update VM first, just in case it fails
             if (syncVM) {
-                if (other == ALL_UNNAMED_MODULE) {
+                if (other == Special.ALL_UNNAMED_MODULE) {
                     addReads0(this, null);
                 } else {
                     addReads0(this, other);
                 }
             }

@@ -505,11 +508,11 @@
      *
      * @see ModuleDescriptor#exports()
      */
     public boolean isExported(String pn) {
         Objects.requireNonNull(pn);
-        return implIsExportedOrOpen(pn, EVERYONE_MODULE, /*open*/false);
+        return implIsExportedOrOpen(pn, Special.EVERYONE_MODULE, /*open*/false);
     }
 
     /**
      * Returns {@code true} if this module has <em>opened</em> a package
      * unconditionally.

@@ -529,11 +532,11 @@
      *
      * @see ModuleDescriptor#opens()
      */
     public boolean isOpen(String pn) {
         Objects.requireNonNull(pn);
-        return implIsExportedOrOpen(pn, EVERYONE_MODULE, /*open*/true);
+        return implIsExportedOrOpen(pn, Special.EVERYONE_MODULE, /*open*/true);
     }
 
 
     /**
      * Returns {@code true} if this module exports or opens the given package

@@ -592,16 +595,16 @@
      * or the given module. Also returns true if the given module is an unnamed
      * module and targets contains ALL_UNNAMED_MODULE.
      */
     private boolean allows(Set<Module> targets, Module module) {
        if (targets != null) {
-           if (targets.contains(EVERYONE_MODULE))
+           if (targets.contains(Special.EVERYONE_MODULE))
                return true;
-           if (module != EVERYONE_MODULE) {
+           if (module != Special.EVERYONE_MODULE) {
                if (targets.contains(module))
                    return true;
-               if (!module.isNamed() && targets.contains(ALL_UNNAMED_MODULE))
+               if (!module.isNamed() && targets.contains(Special.ALL_UNNAMED_MODULE))
                    return true;
            }
         }
         return false;
     }

@@ -610,20 +613,20 @@
      * Returns {@code true} if this module reflectively exports or opens the
      * given package to the given module.
      */
     private boolean isReflectivelyExportedOrOpen(String pn, Module other, boolean open) {
         // exported or open to all modules
-        Map<String, Boolean> exports = ReflectionData.exports.get(this, EVERYONE_MODULE);
+        Map<String, Boolean> exports = ReflectionData.exports.get(this, Special.EVERYONE_MODULE);
         if (exports != null) {
             Boolean b = exports.get(pn);
             if (b != null) {
                 boolean isOpen = b.booleanValue();
                 if (!open || isOpen) return true;
             }
         }
 
-        if (other != EVERYONE_MODULE) {
+        if (other != Special.EVERYONE_MODULE) {
 
             // exported or open to other
             exports = ReflectionData.exports.get(this, other);
             if (exports != null) {
                 Boolean b = exports.get(pn);

@@ -633,11 +636,11 @@
                 }
             }
 
             // other is an unnamed module && exported or open to all unnamed
             if (!other.isNamed()) {
-                exports = ReflectionData.exports.get(this, ALL_UNNAMED_MODULE);
+                exports = ReflectionData.exports.get(this, Special.ALL_UNNAMED_MODULE);
                 if (exports != null) {
                     Boolean b = exports.get(pn);
                     if (b != null) {
                         boolean isOpen = b.booleanValue();
                         if (!open || isOpen) return true;

@@ -772,11 +775,11 @@
      * Updates this module to export a package unconditionally.
      *
      * @apiNote This method is for JDK tests only.
      */
     void implAddExports(String pn) {
-        implAddExportsOrOpens(pn, Module.EVERYONE_MODULE, false, true);
+        implAddExportsOrOpens(pn, Special.EVERYONE_MODULE, false, true);
     }
 
     /**
      * Updates this module to export a package to another module.
      *

@@ -790,21 +793,21 @@
      * Updates this module to export a package to all unnamed modules.
      *
      * @apiNote Used by the --add-exports command line option.
      */
     void implAddExportsToAllUnnamed(String pn) {
-        implAddExportsOrOpens(pn, Module.ALL_UNNAMED_MODULE, false, true);
+        implAddExportsOrOpens(pn, Special.ALL_UNNAMED_MODULE, false, true);
     }
 
     /**
      * Updates this export to export a package unconditionally without
      * notifying the VM.
      *
      * @apiNote This method is for VM white-box testing.
      */
     void implAddExportsNoSync(String pn) {
-        implAddExportsOrOpens(pn.replace('/', '.'), Module.EVERYONE_MODULE, false, false);
+        implAddExportsOrOpens(pn.replace('/', '.'), Special.EVERYONE_MODULE, false, false);
     }
 
     /**
      * Updates a module to export a package to another module without
      * notifying the VM.

@@ -819,11 +822,11 @@
      * Updates this module to open a package unconditionally.
      *
      * @apiNote This method is for JDK tests only.
      */
     void implAddOpens(String pn) {
-        implAddExportsOrOpens(pn, Module.EVERYONE_MODULE, true, true);
+        implAddExportsOrOpens(pn, Special.EVERYONE_MODULE, true, true);
     }
 
     /**
      * Updates this module to open a package to another module.
      *

@@ -837,11 +840,11 @@
      * Updates this module to open a package to all unnamed modules.
      *
      * @apiNote Used by the --add-opens command line option.
      */
     void implAddOpensToAllUnnamed(String pn) {
-        implAddExportsOrOpens(pn, Module.ALL_UNNAMED_MODULE, true, true);
+        implAddExportsOrOpens(pn, Special.ALL_UNNAMED_MODULE, true, true);
     }
 
     /**
      * Updates a module to export or open a module to another module.
      *

@@ -887,13 +890,13 @@
                                                + " not in contents");
         }
 
         // update VM first, just in case it fails
         if (syncVM) {
-            if (other == EVERYONE_MODULE) {
+            if (other == Special.EVERYONE_MODULE) {
                 addExportsToAll0(this, pn);
-            } else if (other == ALL_UNNAMED_MODULE) {
+            } else if (other == Special.ALL_UNNAMED_MODULE) {
                 addExportsToAllUnnamed0(this, pn);
             } else {
                 addExports0(this, pn, other);
             }
         }

@@ -928,13 +931,13 @@
         } else {
             openPackages = new HashMap<>(openPackages);
         }
         while (iterator.hasNext()) {
             String pn = iterator.next();
-            Set<Module> prev = openPackages.putIfAbsent(pn, ALL_UNNAMED_MODULE_SET);
+            Set<Module> prev = openPackages.putIfAbsent(pn, Special.ALL_UNNAMED_MODULE_SET);
             if (prev != null) {
-                prev.add(ALL_UNNAMED_MODULE);
+                prev.add(Special.ALL_UNNAMED_MODULE);
             }
 
             // update VM to export the package
             addExportsToAllUnnamed0(this, pn);
         }

@@ -1150,11 +1153,11 @@
             }
             m.reads = reads;
 
             // automatic modules read all unnamed modules
             if (descriptor.isAutomatic()) {
-                m.implAddReads(ALL_UNNAMED_MODULE, true);
+                m.implAddReads(Special.ALL_UNNAMED_MODULE, true);
             }
 
             // exports and opens, skipped for open and automatic
             if (!descriptor.isOpen() && !descriptor.isAutomatic()) {
                 if (isBootLayer && descriptor.opens().isEmpty()) {

@@ -1242,11 +1245,11 @@
                     exportedPackages.put(source, targets);
                 }
             } else {
                 // unqualified exports
                 addExportsToAll0(m, source);
-                exportedPackages.put(source, EVERYONE_SET);
+                exportedPackages.put(source, Special.EVERYONE_SET);
             }
         }
 
         if (!exportedPackages.isEmpty())
             m.exportedPackages = exportedPackages;

@@ -1287,21 +1290,21 @@
                     openPackages.put(source, targets);
                 }
             } else {
                 // unqualified opens
                 addExportsToAll0(m, source);
-                openPackages.put(source, EVERYONE_SET);
+                openPackages.put(source, Special.EVERYONE_SET);
             }
         }
 
         // next the exports, skipping exports when the package is open
         for (Exports exports : descriptor.exports()) {
             String source = exports.source();
 
             // skip export if package is already open to everyone
             Set<Module> openToTargets = openPackages.get(source);
-            if (openToTargets != null && openToTargets.contains(EVERYONE_MODULE))
+            if (openToTargets != null && openToTargets.contains(Special.EVERYONE_MODULE))
                 continue;
 
             if (exports.isQualified()) {
                 // qualified exports
                 Set<Module> targets = new HashSet<>();

@@ -1319,11 +1322,11 @@
                     exportedPackages.put(source, targets);
                 }
             } else {
                 // unqualified exports
                 addExportsToAll0(m, source);
-                exportedPackages.put(source, EVERYONE_SET);
+                exportedPackages.put(source, Special.EVERYONE_SET);
             }
         }
 
         if (!openPackages.isEmpty())
             m.openPackages = openPackages;
< prev index next >