changeset 59939:e0fca02bb611

8238599: Refactor and simplify implAddOpensToAllUnnamed Reviewed-by: alanb
author redestad
date Fri, 07 Feb 2020 09:47:25 +0100
parents 5e402c63694f
children 538611d777d2
files src/java.base/share/classes/java/lang/Module.java src/java.base/share/classes/java/lang/System.java src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java
diffstat 4 files changed, 24 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/Module.java	Thu Feb 06 21:03:40 2020 -0500
+++ b/src/java.base/share/classes/java/lang/Module.java	Fri Feb 07 09:47:25 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -909,12 +909,12 @@
     }
 
     /**
-     * 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");
         }
@@ -923,12 +923,17 @@
         // 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);
@@ -937,10 +942,8 @@
             // update VM to export the package
             addExportsToAllUnnamed0(this, pn);
         }
-        this.openPackages = openPackages;
     }
 
-
     // -- services --
 
     /**
--- a/src/java.base/share/classes/java/lang/System.java	Thu Feb 06 21:03:40 2020 -0500
+++ b/src/java.base/share/classes/java/lang/System.java	Fri Feb 07 09:47:25 2020 +0100
@@ -56,6 +56,7 @@
 import java.util.Properties;
 import java.util.PropertyPermission;
 import java.util.ResourceBundle;
+import java.util.Set;
 import java.util.function.Supplier;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Stream;
@@ -2234,8 +2235,8 @@
             public void addOpensToAllUnnamed(Module m, String pn) {
                 m.implAddOpensToAllUnnamed(pn);
             }
-            public void addOpensToAllUnnamed(Module m, Iterator<String> packages) {
-                m.implAddOpensToAllUnnamed(packages);
+            public void addOpensToAllUnnamed(Module m, Set<String> concealedPackages, Set<String> exportedPackages) {
+                m.implAddOpensToAllUnnamed(concealedPackages, exportedPackages);
             }
             public void addUses(Module m, Class<?> service) {
                 m.implAddUses(service);
--- a/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java	Thu Feb 06 21:03:40 2020 -0500
+++ b/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java	Fri Feb 07 09:47:25 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Stream;
 
@@ -221,9 +222,9 @@
     void addOpensToAllUnnamed(Module m, String pkg);
 
     /**
-     * Updates module m to open all packages returned by the given iterator.
+     * Updates module m to open all packages in the given sets.
      */
-    void addOpensToAllUnnamed(Module m, Iterator<String> packages);
+    void addOpensToAllUnnamed(Module m, Set<String> concealedPkgs, Set<String> exportedPkgs);
 
     /**
      * Updates module m to use a service.
--- a/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Thu Feb 06 21:03:40 2020 -0500
+++ b/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Fri Feb 07 09:47:25 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -801,6 +801,7 @@
         }
 
         // open specific packages in the system modules
+        Set<String> emptySet = Set.of();
         for (Module m : bootLayer.modules()) {
             ModuleDescriptor descriptor = m.getDescriptor();
             String name = m.getName();
@@ -816,8 +817,8 @@
                 continue;
             }
 
-            Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, Set.of());
-            Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, Set.of());
+            Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, emptySet);
+            Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, emptySet);
 
             // refresh the set of concealed and exported packages if needed
             if (extraExportsOrOpens) {
@@ -850,8 +851,7 @@
 
             // open the packages to unnamed modules
             JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
-            jla.addOpensToAllUnnamed(m, concat(concealedPackages.iterator(),
-                                               exportedPackages.iterator()));
+            jla.addOpensToAllUnnamed(m, concealedPackages, exportedPackages);
         }
 
         builder.complete();
@@ -996,25 +996,6 @@
     }
 
     /**
-     * Returns an iterator that yields all elements of the first iterator
-     * followed by all the elements of the second iterator.
-     */
-    static <T> Iterator<T> concat(Iterator<T> iterator1, Iterator<T> iterator2) {
-        return new Iterator<T>() {
-            @Override
-            public boolean hasNext() {
-                return iterator1.hasNext() || iterator2.hasNext();
-            }
-            @Override
-            public T next() {
-                if (iterator1.hasNext()) return iterator1.next();
-                if (iterator2.hasNext()) return iterator2.next();
-                throw new NoSuchElementException();
-            }
-        };
-    }
-
-    /**
      * Wraps a (potentially not thread safe) ModuleFinder created during startup
      * for use after startup.
      */