< prev index next >

src/java.base/share/classes/jdk/internal/loader/Loader.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -196,11 +196,10 @@
         this.localPackageToModule = localPackageToModule;
 
         this.acc = AccessController.getContext();
     }
 
-
     /**
      * Completes initialization of this Loader. This method populates
      * remotePackageToLoader with the packages of the remote modules, where
      * "remote modules" are the modules read by modules defined to this loader.
      *

@@ -251,12 +250,16 @@
                     if (loader == null)
                         loader = ClassLoaders.platformClassLoader();
                 }
 
                 // find the packages that are exported to the target module
-                String target = resolvedModule.name();
                 ModuleDescriptor descriptor = other.reference().descriptor();
+                if (descriptor.isAutomatic()) {
+                    ClassLoader l = loader;
+                    descriptor.packages().forEach(pn -> remotePackage(pn, l));
+                } else {
+                    String target = resolvedModule.name();
                 for (ModuleDescriptor.Exports e : descriptor.exports()) {
                     boolean delegate;
                     if (e.isQualified()) {
                         // qualified export in same configuration
                         delegate = (other.configuration() == cf)

@@ -265,15 +268,11 @@
                         // unqualified
                         delegate = true;
                     }
 
                     if (delegate) {
-                        String pn = e.source();
-                        ClassLoader l = remotePackageToLoader.putIfAbsent(pn, loader);
-                        if (l != null && l != loader) {
-                            throw new IllegalArgumentException("Package "
-                                + pn + " cannot be imported from multiple loaders");
+                            remotePackage(e.source(), loader);
                         }
                     }
                 }
             }
 

@@ -281,10 +280,26 @@
 
         return this;
     }
 
     /**
+     * Adds to remotePackageToLoader so that an attempt to load a class in
+     * the package delegates to the given class loader.
+     *
+     * @throws IllegalStateException
+     *         if the package is already mapped to a different class loader
+     */
+    private void remotePackage(String pn, ClassLoader loader) {
+        ClassLoader l = remotePackageToLoader.putIfAbsent(pn, loader);
+        if (l != null && l != loader) {
+            throw new IllegalStateException("Package "
+                + pn + " cannot be imported from multiple loaders");
+        }
+    }
+
+
+    /**
      * Find the layer corresponding to the given configuration in the tree
      * of layers rooted at the given parent.
      */
     private Optional<ModuleLayer> findModuleLayer(ModuleLayer parent, Configuration cf) {
         return SharedSecrets.getJavaLangAccess().layers(parent)
< prev index next >