changeset 14802:ec4fb763dbce

Update definePackage @throws IAE javadoc and builtin loader to handle IAE case
author mchung
date Thu, 17 Dec 2015 12:10:05 -0800
parents 2cf87b107845
children 387f9070f3bf
files src/java.base/share/classes/java/lang/ClassLoader.java src/java.base/share/classes/jdk/internal/misc/BuiltinClassLoader.java
diffstat 2 files changed, 49 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/ClassLoader.java	Thu Dec 17 17:44:52 2015 +0000
+++ b/src/java.base/share/classes/java/lang/ClassLoader.java	Thu Dec 17 12:10:05 2015 -0800
@@ -819,7 +819,12 @@
                 } catch (MalformedURLException e) {
                 }
             }
-            p = definePackage(pn, null, null, null, null, null, null, url);
+            try {
+                p = definePackage(pn, null, null, null, null, null, null, url);
+            } catch (IllegalArgumentException e) {
+                // this class loader already defines a package
+                p = getDefinedPackage(pn);
+            }
         }
         return p;
     }
@@ -1808,7 +1813,10 @@
      *          if {@code name} is {@code null}.
      *
      * @throws  IllegalArgumentException
-     *          if {@code name} denotes a package already defined by this class loader.
+     *          if a package of the given {@code name} is already
+     *          defined by this class loader but the versioning information
+     *          or code source of the defined package is different than
+     *          the specified arguments
      *
      * @since  1.2
      *
--- a/src/java.base/share/classes/jdk/internal/misc/BuiltinClassLoader.java	Thu Dec 17 17:44:52 2015 +0000
+++ b/src/java.base/share/classes/jdk/internal/misc/BuiltinClassLoader.java	Thu Dec 17 12:10:05 2015 -0800
@@ -587,27 +587,6 @@
     // -- packages
 
     /**
-     * Define a Package this to this class loader if not already defined.
-     * If the package name is in a module defined to this class loader then
-     * the resulting Package is sealed with the code source that is the
-     * module location.
-     *
-     * @param pn package name
-     */
-    Package definePackage(String pn) {
-        Package pkg = getDefinedPackage(pn);
-        if (pkg == null) {
-            LoadedModule loadedModule = packageToModule.get(pn);
-            if (loadedModule == null || loadedModule.loader() != this) {
-                pkg = definePackage(pn, null, null, null, null, null, null, null);
-            } else {
-                pkg = definePackage(pn, loadedModule);
-            }
-        }
-        return pkg;
-    }
-
-    /**
      * Define a package for the given class to this class loader, if not
      * already defined.
      *
@@ -644,7 +623,13 @@
                 }
             }
 
-            p = definePackage(pn, null, null, null, null, null, null, url);
+            try {
+                p = definePackage(pn, null, null, null, null, null, null, url);
+            } catch (IllegalArgumentException e) {
+                // Package being defined should always have the same location
+                // either location of a named module or null.
+                throw new InternalError(e);
+            }
         }
         return p;
     }
@@ -654,8 +639,38 @@
      * is sealed with the code source that is the module location.
      */
     private Package definePackage(String pn, LoadedModule loadedModule) {
-        URL url = loadedModule.location();
-        return definePackage(pn, null, null, null, null, null, null, url);
+        final URL url;
+        if (loadedModule != null && loadedModule.loader() == this) {
+            url = loadedModule.location();
+        } else {
+            url = null;
+        }
+
+        try {
+            return definePackage(pn, null, null, null, null, null, null, url);
+        } catch (IllegalArgumentException e) {
+            // Package being defined should always have the same location
+            // either location of a named module or null.
+            throw new InternalError(e);
+        }
+    }
+
+    /**
+     * Define a Package this to this class loader if not already defined.
+     * If the package name is in a module defined to this class loader then
+     * the resulting Package is sealed with the code source that is the
+     * module location.
+     *
+     * This method is used to define Package objects for the boot loader.
+     *
+     * @param pn package name
+     */
+    Package definePackage(String pn) {
+        Package pkg = getDefinedPackage(pn);
+        if (pkg == null) {
+            pkg = definePackage(pn, packageToModule.get(pn));
+        }
+        return pkg;
     }
 
     /**