changeset 11029:2b2df03a8b30

Add java -limitmods
author alanb
date Thu, 20 Nov 2014 10:45:12 +0000
parents f1ccae5de625
children 64d9388269da
files src/java.base/share/classes/jdk/jigsaw/module/runtime/ModuleBootstrap.java src/java.base/share/native/libjli/java.c test/jdk/jigsaw/launcher/dashmods/basic.sh test/jdk/jigsaw/launcher/dashmods/src/test/jdk/test/UseAWT.java test/jdk/jigsaw/launcher/dashmods/src/test/module-info.java test/jdk/jigsaw/launcher/limitmods/basic.sh test/jdk/jigsaw/launcher/limitmods/src/test/jdk/test/UseAWT.java test/jdk/jigsaw/launcher/limitmods/src/test/module-info.java
diffstat 8 files changed, 189 insertions(+), 122 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/jdk/jigsaw/module/runtime/ModuleBootstrap.java	Tue Nov 18 12:31:27 2014 +0000
+++ b/src/java.base/share/classes/jdk/jigsaw/module/runtime/ModuleBootstrap.java	Thu Nov 20 10:45:12 2014 +0000
@@ -112,7 +112,7 @@
 
         // additional module(s) specified by -addmods
         Set<String> additionalMods = null;
-        propValue = System.getProperty("jdk.launcher.modules");
+        propValue = System.getProperty("jdk.launcher.addmods");
         if (propValue != null) {
             additionalMods = new HashSet<>();
             for (String mod: propValue.split(",")) {
@@ -120,6 +120,18 @@
             }
         }
 
+        // -limitmods
+        propValue = System.getProperty("jdk.launcher.limitmods");
+        if (propValue != null) {
+            Set<String> mods = new HashSet<>();
+            for (String mod: propValue.split(",")) {
+                mods.add(mod);
+            }
+            if (mainMid != null)
+                mods.add(mainMid.name());
+            finder = limitFinder(finder, mods);
+        }
+
         // If the class path is set then assume the unnamed module is observable.
         // We implement this here by putting the names of all observable (named)
         // modules into the set of modules to resolve.
@@ -205,6 +217,40 @@
     }
 
     /**
+     * Returns a ModuleArtifactFinder that locates modules via the given
+     * ModuleArtifactFinder but limits what can be found to the given
+     * modules and their transitive dependences.
+     */
+    private static ModuleArtifactFinder limitFinder(ModuleArtifactFinder finder,
+                                                    Set<String> mods)
+    {
+        Configuration cf = Configuration.resolve(finder,
+                                                 Layer.emptyLayer(),
+                                                 ModuleArtifactFinder.nullFinder(),
+                                                 mods);
+
+        // module name -> artifact
+        Map<String, ModuleArtifact> map = new HashMap<>();
+        cf.descriptors().forEach(md -> {
+            String name = md.name();
+            map.put(name, finder.find(name));
+        });
+
+        Set<ModuleArtifact> artifacts = new HashSet<>(map.values());
+
+        return new ModuleArtifactFinder() {
+            @Override
+            public ModuleArtifact find(String name) {
+                return map.get(name);
+            }
+            @Override
+            public Set<ModuleArtifact> allModules() {
+                return artifacts;
+            }
+        };
+    }
+
+    /**
      * Returns a map of module to class loader.
      */
     private static Map<ModuleArtifact, ClassLoader> loaderMap(ModuleArtifactFinder systemLibrary) {
--- a/src/java.base/share/native/libjli/java.c	Tue Nov 18 12:31:27 2014 +0000
+++ b/src/java.base/share/native/libjli/java.c	Thu Nov 20 10:45:12 2014 +0000
@@ -101,6 +101,7 @@
 static void SetModulePath(const char *s);
 static void SetMainModule(const char *s);
 static void SetAddModulesProp(const char *mods);
+static void SetLimitModulesProp(const char *mods);
 static void SelectVersion(int argc, char **argv, char **main_class);
 static void SetJvmEnvironment(int argc, char **argv);
 static jboolean ParseArguments(int *pargc, char ***pargv,
@@ -860,7 +861,15 @@
 SetAddModulesProp(const char *mods) {
     size_t buflen = JLI_StrLen(mods) + 40;
     char *prop = (char *)JLI_MemAlloc(buflen);
-    JLI_Snprintf(prop, buflen, "-Djdk.launcher.modules=%s", mods);
+    JLI_Snprintf(prop, buflen, "-Djdk.launcher.addmods=%s", mods);
+    AddOption(prop, NULL);
+}
+
+static void
+SetLimitModulesProp(const char *mods) {
+    size_t buflen = JLI_StrLen(mods) + 40;
+    char *prop = (char *)JLI_MemAlloc(buflen);
+    JLI_Snprintf(prop, buflen, "-Djdk.launcher.limitmods=%s", mods);
     AddOption(prop, NULL);
 }
 
@@ -1145,6 +1154,10 @@
             ARG_CHECK (argc, ARG_ERROR6, arg);
             SetAddModulesProp(*argv);
             argv++; --argc;
+        } else if (JLI_StrCmp(arg, "-limitmods") == 0) {
+            ARG_CHECK (argc, ARG_ERROR6, arg);
+            SetLimitModulesProp(*argv);
+            argv++; --argc;
         } else if (JLI_StrCmp(arg, "-help") == 0 ||
                    JLI_StrCmp(arg, "-h") == 0 ||
                    JLI_StrCmp(arg, "-?") == 0) {
--- a/test/jdk/jigsaw/launcher/dashmods/basic.sh	Tue Nov 18 12:31:27 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-#
-# Copyright (c) 2014, 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.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
-# or visit www.oracle.com if you need additional information or have any
-# questions.
-#
-
-# @test
-# @ignore until -limitmods is added
-# @summary Basic test of launcher -mods option
-# @run shell basic.sh
-
-set -e
-
-if [ -z "$TESTJAVA" ]; then
-  if [ $# -lt 1 ]; then exit 1; fi
-  TESTJAVA="$1"; shift
-  COMPILEJAVA="${TESTJAVA}"
-  TESTSRC="`pwd`"
-  TESTCLASSES="`pwd`"
-fi
-
-JAVAC="$COMPILEJAVA/bin/javac"
-JAVA="$TESTJAVA/bin/java"
-
-mkdir -p mods/test
-$JAVAC -d mods/test `find $TESTSRC/src/test -name "*.java"`
-
-# -classpath && -mods
-$JAVA -mods java.desktop -cp mods/test jdk.test.UseAWT expect-pass
-$JAVA -mods java.base -cp mods/test jdk.test.UseAWT expect-fail
-
-# -m && -mods
-$JAVA -mods java.base -mp mods -m test/jdk.test.UseAWT expect-pass
-
-exit 0
--- a/test/jdk/jigsaw/launcher/dashmods/src/test/jdk/test/UseAWT.java	Tue Nov 18 12:31:27 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-/*
- * Copyright (c) 2014, 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.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.test;
-
-public class UseAWT {
-    public static void main(String[] args) {
-        boolean expectFail = args[0].equals("expect-fail");
-        boolean expectPass = args[0].equals("expect-pass");
-        if (expectFail == expectPass)
-            throw new RuntimeException("Need to run with expect-* argument");
-
-        try {
-            Class<?> c = java.awt.Component.class;
-            if (expectFail) throw new RuntimeException("No Error thrown");
-        } catch (NoClassDefFoundError e) {
-            // exact Error is TBD, will likely be NoClassDefFoundError as
-            // class should not be observable
-            if (expectPass) throw new RuntimeException(e);
-        }
-    }
-}
--- a/test/jdk/jigsaw/launcher/dashmods/src/test/module-info.java	Tue Nov 18 12:31:27 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-/*
- * Copyright (c) 2014, 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.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-module test {
-    requires java.desktop;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jigsaw/launcher/limitmods/basic.sh	Thu Nov 20 10:45:12 2014 +0000
@@ -0,0 +1,51 @@
+#
+# Copyright (c) 2014, 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.
+#
+# This code is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# version 2 for more details (a copy is included in the LICENSE file that
+# accompanied this code).
+#
+# You should have received a copy of the GNU General Public License version
+# 2 along with this work; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+# or visit www.oracle.com if you need additional information or have any
+# questions.
+#
+
+# @test
+# @summary Basic test of launcher -limitmods option
+# @run shell basic.sh
+
+set -e
+
+if [ -z "$TESTJAVA" ]; then
+  if [ $# -lt 1 ]; then exit 1; fi
+  TESTJAVA="$1"; shift
+  COMPILEJAVA="${TESTJAVA}"
+  TESTSRC="`pwd`"
+  TESTCLASSES="`pwd`"
+fi
+
+JAVAC="$COMPILEJAVA/bin/javac"
+JAVA="$TESTJAVA/bin/java"
+
+mkdir -p mods/test
+$JAVAC -d mods/test `find $TESTSRC/src/test -name "*.java"`
+
+# -classpath && -limitmods
+$JAVA -limitmods java.desktop -cp mods/test jdk.test.UseAWT expect-pass
+$JAVA -limitmods java.base -cp mods/test jdk.test.UseAWT expect-fail
+
+# -limitmods and -m
+$JAVA -limitmods java.base -mp mods -m test/jdk.test.UseAWT expect-pass
+
+exit 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jigsaw/launcher/limitmods/src/test/jdk/test/UseAWT.java	Thu Nov 20 10:45:12 2014 +0000
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2014, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.test;
+
+import java.awt.Component;
+import java.lang.reflect.Module;
+
+public class UseAWT {
+    public static void main(String[] args) {
+        boolean expectFail = args[0].equals("expect-fail");
+        boolean expectPass = args[0].equals("expect-pass");
+        if (expectFail == expectPass)
+            throw new RuntimeException("Need to run with expect-* argument");
+
+        try {
+            Class<?> c = Component.class;
+
+            // workaround until VM throws NCFE for types in modules that
+            // aren't defined to the VM
+            if (c.getModule() == null)
+                throw new NoClassDefFoundError();
+
+            if (expectFail) throw new RuntimeException("No Error thrown");
+        } catch (NoClassDefFoundError e) {
+            // exact Error is TBD, will likely be NoClassDefFoundError as
+            // class should not be observable
+            if (expectPass) throw new RuntimeException(e);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jigsaw/launcher/limitmods/src/test/module-info.java	Thu Nov 20 10:45:12 2014 +0000
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2014, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+module test {
+    requires java.desktop;
+}