8189671: jlink should clearly report error when an automatic module is used

Reviewed-by: alanb, mchung
This commit is contained in:
Athijegannathan Sundararajan 2017-10-20 17:16:05 +05:30
parent a6aad28019
commit 0da8aae161
3 changed files with 59 additions and 0 deletions

View File

@ -438,6 +438,16 @@ public class JlinkTask {
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()

View File

@ -107,6 +107,7 @@ main.extended.help.footer=\
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}

View File

@ -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.Arrays;
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 @@ public class JLinkNegativeTest {
}
}
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 {