8175193: jlink and `requires static`
authormchung
Fri, 24 Feb 2017 09:26:59 -0800
changeset 44000 1c98f4d91595
parent 43999 4cc44dd9f14f
child 44001 47c9904f9d02
8175193: jlink and `requires static` Reviewed-by: alanb, forax
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ModuleSorter.java
jdk/test/tools/jlink/plugins/SystemModuleDescriptors/UserModuleTest.java
jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m3/module-info.java
jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m3/p3/Foo.java
jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m3/p3/Lib.java
jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m5/module-info.java
jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m5/p5/Main.java
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ModuleSorter.java	Fri Feb 24 17:05:00 2017 +0000
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ModuleSorter.java	Fri Feb 24 09:26:59 2017 -0800
@@ -30,6 +30,8 @@
 import jdk.tools.jlink.plugin.ResourcePoolModuleView;
 
 import java.lang.module.ModuleDescriptor;
+import java.lang.module.ModuleDescriptor.Requires.Modifier;
+
 import java.nio.ByteBuffer;
 import java.util.Deque;
 import java.util.HashMap;
@@ -67,14 +69,15 @@
 
     private ModuleSorter addModule(ResourcePoolModule module) {
         addNode(module);
-        readModuleDescriptor(module).requires().stream()
-            .forEach(req -> {
-                String dm = req.name();
-                ResourcePoolModule dep = moduleView.findModule(dm)
-                    .orElseThrow(() -> new PluginException(dm + " not found"));
+        readModuleDescriptor(module).requires().forEach(req -> {
+            ResourcePoolModule dep = moduleView.findModule(req.name()).orElse(null);
+            if (dep != null) {
                 addNode(dep);
                 edges.get(module.name()).add(dep);
-            });
+            } else if (!req.modifiers().contains(Modifier.STATIC)) {
+                throw new PluginException(req.name() + " not found");
+            }
+        });
         return this;
     }
 
@@ -113,7 +116,7 @@
             return;
         }
         visited.add(node);
-        edges.get(node.name()).stream()
+        edges.get(node.name())
              .forEach(x -> visit(x, visited, done));
         done.add(node);
         result.addLast(node);
--- a/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/UserModuleTest.java	Fri Feb 24 17:05:00 2017 +0000
+++ b/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/UserModuleTest.java	Fri Feb 24 09:26:59 2017 -0800
@@ -64,7 +64,7 @@
     private static final String MAIN_MID = "m1/p1.Main";
 
     // the names of the modules in this test
-    private static String[] modules = new String[] {"m1", "m2", "m3", "m4"};
+    private static String[] modules = new String[] {"m1", "m2", "m3", "m4", "m5"};
 
 
     private static boolean hasJmods() {
@@ -160,6 +160,50 @@
                         .getExitValue() == 0);
     }
 
+    @Test
+    public void testRequiresStatic() throws Throwable {
+        if (!hasJmods()) return;
+
+        Path dir = Paths.get("requiresStatic");
+        createImage(dir, "m5");
+        Path java = dir.resolve("bin").resolve("java");
+        assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
+                        .outputTo(System.out)
+                        .errorTo(System.out)
+                        .getExitValue() == 0);
+
+        // run with m3 present
+        assertTrue(executeProcess(java.toString(),
+                                  "--module-path", MODS_DIR.toString(),
+                                  "--add-modules", "m3",
+                                  "-m", "m5/p5.Main")
+                        .outputTo(System.out)
+                        .errorTo(System.out)
+                        .getExitValue() == 0);
+    }
+
+    @Test
+    public void testRequiresStatic2() throws Throwable {
+        if (!hasJmods()) return;
+
+        Path dir = Paths.get("requiresStatic2");
+        createImage(dir, "m3", "m5");
+
+        Path java = dir.resolve("bin").resolve("java");
+        assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
+                        .outputTo(System.out)
+                        .errorTo(System.out)
+                        .getExitValue() == 0);
+
+        // boot layer with m3 and m5
+        assertTrue(executeProcess(java.toString(),
+                                  "--add-modules", "m3",
+                                  "-m", "m5/p5.Main")
+                        .outputTo(System.out)
+                        .errorTo(System.out)
+                        .getExitValue() == 0);
+    }
+
     private void createJmods(String... modules) throws IOException {
         // use the same target platform as in java.base
         ModuleDescriptor md = Layer.boot().findModule("java.base").get()
--- a/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m3/module-info.java	Fri Feb 24 17:05:00 2017 +0000
+++ b/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m3/module-info.java	Fri Feb 24 09:26:59 2017 -0800
@@ -23,4 +23,5 @@
 
 module m3 {
     requires m4;
+    exports p3;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m3/p3/Foo.java	Fri Feb 24 09:26:59 2017 -0800
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017, 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 p3;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Documented
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.SOURCE)
+public @interface Foo {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m3/p3/Lib.java	Fri Feb 24 09:26:59 2017 -0800
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2017, 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 p3;
+
+public class Lib {
+    public static String concat(String x, String y) {
+        return x + y;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m5/module-info.java	Fri Feb 24 09:26:59 2017 -0800
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2017, 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 m5 {
+    requires static m3;
+    exports p5;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m5/p5/Main.java	Fri Feb 24 09:26:59 2017 -0800
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2017, 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 p5;
+
+import java.lang.reflect.Layer;
+import p3.Foo;
+import p3.Lib;
+
+/**
+ * This test verifies jlink support of requires static.
+ */
+public class Main {
+    public static void main(String... args) {
+        boolean libPresent = Layer.boot().findModule("m3").isPresent();
+        if (LibHelper.libClassFound != libPresent) {
+            throw new RuntimeException("Expected module m3 not in the boot layer");
+        }
+
+        if (libPresent) {
+            // p3.Lib must be present
+            LibHelper.concat("x", "y");
+        }
+    }
+
+    static class LibHelper {
+        @Foo
+        static final boolean libClassFound;
+
+        static {
+            boolean found = false;
+            try {
+                Class<?> c = Class.forName("p3.Lib");
+                found = true;
+            } catch (ClassNotFoundException e) {
+            }
+            libClassFound = found;
+        }
+
+        public static String concat(String x, String y) {
+            return Lib.concat(x, y);
+        }
+    }
+}