changeset 19256:eb788ea0f527

Top-level "java" package not handled correctly in modules
author alanb
date Sat, 11 Mar 2017 09:47:50 +0000
parents b48266d80890
children 7f64508eaab8
files src/java.base/share/classes/java/lang/reflect/Layer.java test/java/lang/reflect/Layer/BasicLayerTest.java
diffstat 2 files changed, 30 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/reflect/Layer.java	Fri Mar 10 13:56:38 2017 -0800
+++ b/src/java.base/share/classes/java/lang/reflect/Layer.java	Sat Mar 11 09:47:50 2017 +0000
@@ -407,8 +407,8 @@
      * </ul>
      *
      * <p> In addition, a layer cannot be created if the configuration contains
-     * a module named "{@code java.base}" or a module with a package name
-     * starting with "{@code java.}". </p>
+     * a module named "{@code java.base}", or a module contains a package named
+     * "{@code java}" or a package with a name starting with "{@code java.}". </p>
      *
      * <p> If there is a security manager then the class loader created by
      * this method will load classes and resources with privileges that are
@@ -417,7 +417,7 @@
      * @param  cf
      *         The configuration for the layer
      * @param  parentLayers
-     *         The list parent layers in search order
+     *         The list of parent layers in search order
      * @param  parentLoader
      *         The parent class loader for the class loader created by this
      *         method; may be {@code null} for the bootstrap class loader
@@ -484,7 +484,7 @@
      * @param  cf
      *         The configuration for the layer
      * @param  parentLayers
-     *         The list parent layers in search order
+     *         The list of parent layers in search order
      * @param  parentLoader
      *         The parent class loader for each of the class loaders created by
      *         this method; may be {@code null} for the bootstrap class loader
@@ -496,8 +496,10 @@
      *         the parent layers, including order
      * @throws LayerInstantiationException
      *         If the layer cannot be created because the configuration contains
-     *         a module named "{@code java.base}" or a module with a package
-     *         name starting with "{@code java.}"
+     *         a module named "{@code java.base}" or a module contains a package
+     *         named "{@code java}" or a package with a name starting with
+     *         "{@code java.}"
+     *
      * @throws SecurityException
      *         If {@code RuntimePermission("createClassLoader")} or
      *         {@code RuntimePermission("getClassLoader")} is denied by
@@ -557,10 +559,11 @@
      *
      * <p> In addition, a layer cannot be created if the configuration contains
      * a module named "{@code java.base}", a configuration contains a module
-     * with a package name starting with "{@code java.}" is mapped to a class
-     * loader other than the {@link ClassLoader#getPlatformClassLoader()
-     * platform class loader}, or the function to map a module name to a class
-     * loader returns {@code null}. </p>
+     * with a package named "{@code java}" or a package name starting with
+     * "{@code java.}" and the module is mapped to a class loader other than
+     * the {@link ClassLoader#getPlatformClassLoader() platform class loader},
+     * or the function to map a module name to a class loader returns
+     * {@code null}. </p>
      *
      * <p> If the function to map a module name to class loader throws an error
      * or runtime exception then it is propagated to the caller of this method.
@@ -574,7 +577,7 @@
      * @param  cf
      *         The configuration for the layer
      * @param  parentLayers
-     *         The list parent layers in search order
+     *         The list of parent layers in search order
      * @param  clf
      *         The function to map a module name to a class loader
      *
--- a/test/java/lang/reflect/Layer/BasicLayerTest.java	Fri Mar 10 13:56:38 2017 -0800
+++ b/test/java/lang/reflect/Layer/BasicLayerTest.java	Sat Mar 11 09:47:50 2017 +0000
@@ -45,6 +45,8 @@
 import java.util.stream.Collectors;
 
 import jdk.internal.misc.SharedSecrets;
+
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 import static org.testng.Assert.*;
 
@@ -796,28 +798,31 @@
     }
 
 
+    @DataProvider(name = "javaPackages")
+    public Object[][] javaPackages() {
+        return new Object[][] { { "m1", "java" }, { "m2", "java.x" } };
+    }
+
     /**
-     * Attempt to create a Layer with a module containing a "java." package.
+     * Attempt to create a Layer with a module containing a "java" package.
      * This should only be allowed when the module is defined to the platform
      * class loader.
      */
-    public void testLayerWithJavaPackage() {
-        ModuleDescriptor descriptor = newBuilder("foo")
-                .packages(Set.of("java.foo"))
-                .build();
-
+    @Test(dataProvider = "javaPackages")
+    public void testLayerWithJavaPackage(String mn, String pn) {
+        ModuleDescriptor descriptor = newBuilder(mn).packages(Set.of(pn)).build();
         ModuleFinder finder = ModuleUtils.finderOf(descriptor);
 
         Configuration cf = Layer.boot()
                 .configuration()
-                .resolve(finder, ModuleFinder.of(), Set.of("foo"));
+                .resolve(finder, ModuleFinder.of(), Set.of(mn));
         assertTrue(cf.modules().size() == 1);
 
         ClassLoader pcl = ClassLoader.getPlatformClassLoader();
         ClassLoader scl = ClassLoader.getSystemClassLoader();
 
         try {
-            Layer.boot().defineModules(cf, mn -> new ClassLoader() { });
+            Layer.boot().defineModules(cf, _mn -> new ClassLoader() { });
             assertTrue(false);
         } catch (LayerInstantiationException e) { }
 
@@ -832,13 +837,13 @@
         } catch (LayerInstantiationException e) { }
 
         // create layer with module defined to platform class loader
-        Layer layer = Layer.boot().defineModules(cf, mn -> pcl);
-        Optional<Module> om = layer.findModule("foo");
+        Layer layer = Layer.boot().defineModules(cf, _mn -> pcl);
+        Optional<Module> om = layer.findModule(mn);
         assertTrue(om.isPresent());
         Module foo = om.get();
         assertTrue(foo.getClassLoader() == pcl);
         assertTrue(foo.getPackages().length == 1);
-        assertTrue(foo.getPackages()[0].equals("java.foo"));
+        assertTrue(foo.getPackages()[0].equals(pn));
     }