8189671: jlink should clearly report error when an automatic module is used
authorsundar
Fri, 20 Oct 2017 17:16:05 +0530
changeset 47399 fb677b3f0888
parent 47398 1fd27535bc57
child 47426 7d5509425e4a
child 47671 4dd1715f647f
8189671: jlink should clearly report error when an automatic module is used Reviewed-by: alanb, mchung
src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties
test/jdk/tools/jlink/JLinkNegativeTest.java
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Fri Oct 20 13:33:35 2017 +0200
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Fri Oct 20 17:16:05 2017 +0530
@@ -438,6 +438,16 @@
         Configuration cf = bindService ? config.resolveAndBind()
                                        : config.resolve();
 
+        cf.modules().stream()
+            .map(ResolvedModule::reference)
+            .filter(mref -> mref.descriptor().isAutomatic())
+            .findAny()
+            .ifPresent(mref -> {
+                String loc = mref.location().map(URI::toString).orElse("<unknown>");
+                throw new IllegalArgumentException(
+                    taskHelper.getMessage("err.automatic.module", mref.descriptor().name(), loc));
+            });
+
         if (verbose && log != null) {
             // print modules to be linked in
             cf.modules().stream()
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties	Fri Oct 20 13:33:35 2017 +0200
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties	Fri Oct 20 17:16:05 2017 +0530
@@ -107,6 +107,7 @@
 error.prefix=Error:
 warn.prefix=Warning:
 
+err.automatic.module:automatic module cannot be used with jlink: {0} from {1}
 err.unknown.byte.order:unknown byte order {0}
 err.launcher.main.class.empty:launcher main class name cannot be empty: {0}
 err.launcher.module.name.empty:launcher module name cannot be empty: {0}
--- a/test/jdk/tools/jlink/JLinkNegativeTest.java	Fri Oct 20 13:33:35 2017 +0200
+++ b/test/jdk/tools/jlink/JLinkNegativeTest.java	Fri Oct 20 17:16:05 2017 +0530
@@ -26,6 +26,7 @@
  * @summary Negative tests for jlink
  * @bug 8130861
  * @bug 8174718
+ * @bug 8189671
  * @author Andrei Eremeev
  * @library ../lib
  * @modules java.base/jdk.internal.jimage
@@ -39,6 +40,8 @@
  * @run testng JLinkNegativeTest
  */
 
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.module.ModuleDescriptor;
@@ -52,6 +55,8 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
 
 import jdk.internal.module.ModuleInfoWriter;
 import org.testng.SkipException;
@@ -199,6 +204,49 @@
         }
     }
 
+    private static File createJarFile(File dir, String filename, String pkg, String name) throws IOException {
+        File jarFile = new File(dir, filename + ".jar");
+
+        try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) {
+            JarEntry entry = new JarEntry(filename + "/" + pkg + "/" + name);
+            output.putNextEntry(entry);
+        }
+
+        return jarFile;
+    }
+
+    public void testAutomaticModuleAsRoot() throws IOException {
+        Path imageFile = helper.createNewImageDir("test");
+        String jarName = "myautomod";
+        File jarFile = createJarFile(new File("jars"), jarName, "com/acme", "Bar.class");
+        try {
+            JImageGenerator.getJLinkTask()
+                    .output(imageFile)
+                    .addMods(jarName)
+                    .modulePath(helper.defaultModulePath())
+                    .call().assertFailure("Error: automatic module cannot be used with jlink: " + jarName);
+        } finally {
+            jarFile.delete();
+        }
+    }
+
+    public void testAutomaticModuleAsDependency() throws IOException {
+        Path imageFile = helper.createNewImageDir("test");
+        String autoJarName = "myautomod";
+        File autoJarFile = createJarFile(new File("jars"), autoJarName, "com/acme", "Bar.class");
+        String rootMod = "autodepender";
+        helper.generateDefaultJModule(rootMod, autoJarName).assertSuccess();
+        try {
+            JImageGenerator.getJLinkTask()
+                    .output(imageFile)
+                    .addMods(rootMod)
+                    .modulePath(helper.defaultModulePath())
+                    .call().assertFailure("Error: automatic module cannot be used with jlink: " + autoJarName);
+        } finally {
+            autoJarFile.delete();
+        }
+    }
+
     // Temporarily exclude; the jmod tool can no longer be used to create a jmod
     // with a class in the unnamed package. Find another way, or remove.
 //    public void testAddDefaultPackage() throws IOException {