changeset 14497:c1a46b310ca2

Using -limitmods so that it includes, but does not resolve, the modules specified to -m and -addmods
author alanb
date Wed, 18 Nov 2015 15:15:34 +0000
parents fa885780a428
children 1d2dd559597e
files src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java src/jdk.jlink/share/classes/jdk/tools/jlink/JlinkTask.java test/jdk/jigsaw/launcher/addmods/AddModsTest.java test/jdk/jigsaw/launcher/limitmods/LimitModsTest.java
diffstat 4 files changed, 148 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Wed Nov 18 12:52:31 2015 +0100
+++ b/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Wed Nov 18 15:15:34 2015 +0000
@@ -126,22 +126,6 @@
             }
         }
 
-        // -limitmods
-        boolean limitmods = false;
-        propValue = System.getProperty("jdk.launcher.limitmods");
-        if (propValue != null) {
-            Set<String> mods = new HashSet<>();
-            for (String mod: propValue.split(",")) {
-                mods.add(mod);
-            }
-            if (mainModule != null)
-                mods.add(mainModule);
-            if (addModules != null)
-                mods.addAll(addModules);
-            finder = limitFinder(finder, mods);
-            limitmods = true;
-        }
-
         // The root modules to resolve
         Set<String> roots = new HashSet<>();
 
@@ -153,6 +137,20 @@
         if (addModules != null)
             roots.addAll(addModules);
 
+
+        // -limitmods
+        boolean limitmods = false;
+        propValue = System.getProperty("jdk.launcher.limitmods");
+        if (propValue != null) {
+            Set<String> mods = new HashSet<>();
+            for (String mod: propValue.split(",")) {
+                mods.add(mod);
+            }
+            finder = limitFinder(finder, mods, roots);
+            limitmods = true;
+        }
+
+
         // If there is no initial module specified then assume that the
         // initial module is the unnamed module of the application class
         // loader. By convention, and for compatibility, this is
@@ -244,17 +242,18 @@
     }
 
     /**
-     * Returns a ModuleFinder that locates modules via the given
-     * ModuleFinder but limits what can be found to the given
-     * modules and their transitive dependences.
+     * Returns a ModuleFinder that limits observability to the given root
+     * modules, their transitive dependences, plus a set of other modules.
      */
     private static ModuleFinder limitFinder(ModuleFinder finder,
-                                            Set<String> mods)
+                                            Set<String> roots,
+                                            Set<String> otherMods)
     {
+        // resolve all root modules
         Configuration cf = Configuration.resolve(finder,
                 Layer.empty(),
                 ModuleFinder.empty(),
-                mods);
+                roots);
 
         // module name -> reference
         Map<String, ModuleReference> map = new HashMap<>();
@@ -263,8 +262,21 @@
             map.put(name, finder.find(name).get());
         });
 
+        // set of modules that are observable
         Set<ModuleReference> mrefs = new HashSet<>(map.values());
 
+        // add the other modules
+        for (String mod : otherMods) {
+            Optional<ModuleReference> omref = finder.find(mod);
+            if (omref.isPresent()) {
+                ModuleReference mref = omref.get();
+                map.putIfAbsent(mod, mref);
+                mrefs.add(mref);
+            } else {
+                // no need to fail
+            }
+        }
+
         return new ModuleFinder() {
             @Override
             public Optional<ModuleReference> find(String name) {
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/JlinkTask.java	Wed Nov 18 12:52:31 2015 +0100
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/JlinkTask.java	Wed Nov 18 15:15:34 2015 +0000
@@ -299,7 +299,8 @@
         }
         Path[] arr = new Path[config.getModulepaths().size()];
         arr = config.getModulepaths().toArray(arr);
-        ModuleFinder finder = newModuleFinder(arr, config.getLimitmods());
+        ModuleFinder finder
+            = newModuleFinder(arr, config.getLimitmods(), config.getModules());
 
         Path[] pluginsPath = new Path[config.getPluginpaths().size()];
         pluginsPath = config.getPluginpaths().toArray(pluginsPath);
@@ -364,7 +365,8 @@
         if (options.output == null) {
             throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
         }
-        ModuleFinder finder = newModuleFinder(options.modulePath, options.limitMods);
+        ModuleFinder finder
+            = newModuleFinder(options.modulePath, options.limitMods, options.addMods);
         try {
             options.addMods = checkAddMods(options.addMods);
         } catch (IllegalArgumentException ex) {
@@ -396,7 +398,10 @@
         return addMods;
     }
 
-    private static ModuleFinder newModuleFinder(Path[] paths, Set<String> limitMods) {
+    private static ModuleFinder newModuleFinder(Path[] paths,
+                                                Set<String> limitMods,
+                                                Set<String> addMods)
+    {
         ModuleFinder finder = ModuleFinder.of(paths);
 
         // jmods are located at link-time
@@ -405,7 +410,7 @@
 
         // if limitmods is specified then limit the universe
         if (!limitMods.isEmpty()) {
-            finder = limitFinder(finder, limitMods);
+            finder = limitFinder(finder, limitMods, addMods);
         }
         return finder;
     }
@@ -431,16 +436,18 @@
 
 
     /**
-     * Returns a ModuleFinder that locates modules via the given ModuleFinder
-     * but limits what can be found to the given modules and their transitive
-     * dependences.
+     * Returns a ModuleFinder that limits observability to the given root
+     * modules, their transitive dependences, plus a set of other modules.
      */
-    private static ModuleFinder limitFinder(ModuleFinder finder, Set<String> mods) {
-        Configuration cf
-            = Configuration.resolve(finder,
+    private static ModuleFinder limitFinder(ModuleFinder finder,
+                                            Set<String> roots,
+                                            Set<String> otherMods)
+    {
+        // resolve all root modules
+        Configuration cf = Configuration.resolve(finder,
                 Layer.empty(),
                 ModuleFinder.empty(),
-                mods);
+                roots);
 
         // module name -> reference
         Map<String, ModuleReference> map = new HashMap<>();
@@ -449,8 +456,21 @@
             map.put(name, finder.find(name).get());
         });
 
+        // set of modules that are observable
         Set<ModuleReference> mrefs = new HashSet<>(map.values());
 
+        // add the other modules
+        for (String mod : otherMods) {
+            Optional<ModuleReference> omref = finder.find(mod);
+            if (omref.isPresent()) {
+                ModuleReference mref = omref.get();
+                map.putIfAbsent(mod, mref);
+                mrefs.add(mref);
+            } else {
+                // no need to fail
+            }
+        }
+
         return new ModuleFinder() {
             @Override
             public Optional<ModuleReference> find(String name) {
--- a/test/jdk/jigsaw/launcher/addmods/AddModsTest.java	Wed Nov 18 12:52:31 2015 +0100
+++ b/test/jdk/jigsaw/launcher/addmods/AddModsTest.java	Wed Nov 18 15:15:34 2015 +0000
@@ -83,47 +83,6 @@
 
 
     /**
-     * Basic test of -addmods, using the output of -listmods to check that the
-     * module is resolved.
-     */
-    public void testAddOneModule() throws Exception {
-
-        int exitValue
-            = executeTestJava("-limitmods", "java.base",
-                              "-addmods", "java.sql",
-                              "-listmods")
-                .outputTo(System.out)
-                .errorTo(System.out)
-                .shouldContain("java.sql")
-                .shouldNotContain("java.corba")
-                .getExitValue();
-
-        assertTrue(exitValue == 0);
-    }
-
-
-    /**
-     * Basic test of -addmods, using the output of -listmods to check that the
-     * module is resolved.
-     */
-    public void testAddTwoModules() throws Exception {
-
-        int exitValue
-            = executeTestJava("-limitmods", "java.base",
-                              "-addmods", "java.sql,java.naming",
-                              "-listmods")
-                .outputTo(System.out)
-                .errorTo(System.out)
-                .shouldContain("java.sql")
-                .shouldContain("java.naming")
-                .shouldNotContain("java.corba")
-                .getExitValue();
-
-        assertTrue(exitValue == 0);
-    }
-
-
-    /**
      * Basic test of -addmods ALL-SYSTEM, using the output of -listmods to
      * check that the a sample of the system modules are resolved
      */
--- a/test/jdk/jigsaw/launcher/limitmods/LimitModsTest.java	Wed Nov 18 12:52:31 2015 +0100
+++ b/test/jdk/jigsaw/launcher/limitmods/LimitModsTest.java	Wed Nov 18 15:15:34 2015 +0000
@@ -24,7 +24,7 @@
 /**
  * @test
  * @library ../../lib /lib/testlibrary
- * @modules jdk.compiler
+ * @modules java.desktop java.compact1 jdk.compiler
  * @build LimitModsTest CompilerUtils jdk.testlibrary.*
  * @run testng LimitModsTest
  * @summary Basic tests for java -limitmods
@@ -48,10 +48,8 @@
     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
     private static final Path MODS_DIR = Paths.get("mods");
 
-    // the module name of the test module
+    // the module name / main class of the test module
     private static final String TEST_MODULE = "test";
-
-    // the module main class
     private static final String MAIN_CLASS = "jdk.test.UseAWT";
 
 
@@ -61,11 +59,77 @@
         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
         boolean compiled
             = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
-                                    MODS_DIR.resolve(TEST_MODULE));
+                MODS_DIR.resolve(TEST_MODULE));
 
         assertTrue(compiled, "test module did not compile");
     }
 
+
+    /**
+     * Basic test of -limitmods to limit which platform modules are observable.
+     */
+    public void testLimitingPlatformModules() throws Exception {
+        int exitValue;
+
+        // java -limitmods java.base -listmods
+        exitValue = executeTestJava("-limitmods", "java.base", "-listmods")
+            .outputTo(System.out)
+            .errorTo(System.out)
+            .shouldContain("java.base")
+            .shouldNotContain("java.logging")
+            .shouldNotContain("java.xml")
+            .getExitValue();
+
+        assertTrue(exitValue == 0);
+
+
+        // java -limitmods java.compact1 -listmods
+        exitValue = executeTestJava("-limitmods", "java.compact1", "-listmods")
+            .outputTo(System.out)
+            .errorTo(System.out)
+            .shouldContain("java.base")
+            .shouldContain("java.logging")
+            .shouldContain("java.compact1")
+            .shouldNotContain("java.xml")
+            .getExitValue();
+
+        assertTrue(exitValue == 0);
+    }
+
+
+    /**
+     * Test -limitmods with -addmods
+     */
+    public void testWithAddMods() throws Exception {
+        int exitValue;
+
+        // java -limitmods java.base -addmods java.logging -listmods
+        exitValue = executeTestJava("-limitmods", "java.base",
+                                    "-addmods", "java.logging",
+                                    "-listmods")
+            .outputTo(System.out)
+            .errorTo(System.out)
+            .shouldContain("java.base")
+            .shouldContain("java.logging")
+            .shouldNotContain("java.xml")
+            .getExitValue();
+
+        assertTrue(exitValue == 0);
+
+
+        // java -limitmods java.base -addmods java.sql -listmods
+        // This should fail because java.sql has dependences beyond java.base
+        exitValue = executeTestJava("-limitmods", "java.base",
+                                    "-addmods", "java.sql",
+                                    "-listmods")
+            .outputTo(System.out)
+            .errorTo(System.out)
+            .getExitValue();
+
+        assertTrue(exitValue != 0);
+    }
+
+
     /**
      * Run class path application with -limitmods
      */
@@ -97,18 +161,29 @@
         assertTrue(exitValue2 == 0);
     }
 
+
     /**
      * Run named module with -limitmods
      */
     public void testNamedModule() throws Exception {
+
         String modulepath = MODS_DIR.toString();
         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 
         // java -limitmods java.base -mp mods -m $TESTMODULE/$MAINCLASS
-        int exitValue
-            = executeTestJava("-limitmods", "java.base",
-                              "-mp", modulepath,
-                              "-m", mid)
+        int exitValue = executeTestJava("-limitmods", "java.base",
+                                        "-mp", modulepath,
+                                        "-m", mid)
+                .outputTo(System.out)
+                .errorTo(System.out)
+                .getExitValue();
+
+        assertTrue(exitValue != 0);
+
+        // java -limitmods java.desktop -mp mods -m $TESTMODULE/$MAINCLASS
+        exitValue = executeTestJava("-limitmods", "java.desktop",
+                                    "-mp", modulepath,
+                                    "-m", mid)
                 .outputTo(System.out)
                 .errorTo(System.out)
                 .getExitValue();