< prev index next >

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

Print this page
rev 57943 : 8238599: Refactor and simplify implAddOpensToAllUnnamed
Reviewed-by: alanb

@@ -907,42 +907,45 @@
             map.putIfAbsent(pn, Boolean.FALSE);
         }
     }
 
     /**
-     * Updates a module to open all packages returned by the given iterator to
-     * all unnamed modules.
+     * Updates a module to open all packages in the given sets to all unnamed
+     * modules.
      *
      * @apiNote Used during startup to open packages for illegal access.
      */
-    void implAddOpensToAllUnnamed(Iterator<String> iterator) {
+    void implAddOpensToAllUnnamed(Set<String> concealedPkgs, Set<String> exportedPkgs) {
         if (jdk.internal.misc.VM.isModuleSystemInited()) {
             throw new IllegalStateException("Module system already initialized");
         }
 
         // replace this module's openPackages map with a new map that opens
         // the packages to all unnamed modules.
         Map<String, Set<Module>> openPackages = this.openPackages;
         if (openPackages == null) {
-            openPackages = new HashMap<>();
+            openPackages = new HashMap<>((4 * (concealedPkgs.size() + exportedPkgs.size()) / 3) + 1);
         } else {
             openPackages = new HashMap<>(openPackages);
         }
-        while (iterator.hasNext()) {
-            String pn = iterator.next();
+        implAddOpensToAllUnnamed(concealedPkgs, openPackages);
+        implAddOpensToAllUnnamed(exportedPkgs, openPackages);
+        this.openPackages = openPackages;
+    }
+
+    private void implAddOpensToAllUnnamed(Set<String> pkgs, Map<String, Set<Module>> openPackages) {
+        for (String pn : pkgs) {
             Set<Module> prev = openPackages.putIfAbsent(pn, ALL_UNNAMED_MODULE_SET);
             if (prev != null) {
                 prev.add(ALL_UNNAMED_MODULE);
             }
 
             // update VM to export the package
             addExportsToAllUnnamed0(this, pn);
         }
-        this.openPackages = openPackages;
     }
 
-
     // -- services --
 
     /**
      * If the caller's module is this module then update this module to add a
      * service dependence on the given service type. This method is intended
< prev index next >