8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options

Reviewed-by: mchung, jlaskey
This commit is contained in:
Athijegannathan Sundararajan 2016-09-28 22:13:14 +05:30
parent 0c70a986f9
commit 323aded97f
3 changed files with 65 additions and 0 deletions
jdk
src/jdk.jlink/share/classes/jdk/tools/jlink/internal
test/tools

@ -86,6 +86,9 @@ public class JlinkTask {
task.options.help = true;
}, "--help", "-h"),
new Option<JlinkTask>(true, (task, opt, arg) -> {
// if used multiple times, the last one wins!
// So, clear previous values, if any.
task.options.modulePath.clear();
String[] dirs = arg.split(File.pathSeparator);
int i = 0;
Arrays.stream(dirs)
@ -93,6 +96,9 @@ public class JlinkTask {
.forEach(task.options.modulePath::add);
}, "--module-path", "-p"),
new Option<JlinkTask>(true, (task, opt, arg) -> {
// if used multiple times, the last one wins!
// So, clear previous values, if any.
task.options.limitMods.clear();
for (String mn : arg.split(",")) {
if (mn.isEmpty()) {
throw taskHelper.newBadArgs("err.mods.must.be.specified",

@ -149,6 +149,43 @@ public class JLinkTest {
.call().assertSuccess();
}
{
String moduleName = "m_8165735"; // JDK-8165735
helper.generateDefaultJModule(moduleName+"dependency").assertSuccess();
Path jmod = helper.generateDefaultJModule(moduleName, moduleName+"dependency").assertSuccess();
JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath())
.repeatedModulePath(".") // second --module-path overrides the first one
.output(helper.createNewImageDir(moduleName))
.addMods(moduleName)
// second --module-path does not have that module
.call().assertFailure("Error: Module m_8165735 not found");
JImageGenerator.getJLinkTask()
.modulePath(".") // first --module-path overridden later
.repeatedModulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName))
.addMods(moduleName)
// second --module-path has that module
.call().assertSuccess();
JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName))
.limitMods(moduleName)
.repeatedLimitMods("java.base") // second --limit-modules overrides first
.addMods(moduleName)
.call().assertFailure("Error: Module m_8165735dependency not found, required by m_8165735");
JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath())
.output(helper.createNewImageDir(moduleName))
.limitMods("java.base")
.repeatedLimitMods(moduleName) // second --limit-modules overrides first
.addMods(moduleName)
.call().assertSuccess();
}
{
// Help
StringWriter writer = new StringWriter();

@ -564,6 +564,10 @@ public class JImageGenerator {
private final List<String> limitMods = new ArrayList<>();
private final List<String> options = new ArrayList<>();
private String modulePath;
// if you want to specifiy repeated --module-path option
private String repeatedModulePath;
// if you want to specifiy repeated --limit-modules option
private String repeatedLimitMods;
private Path output;
private Path existing;
@ -572,6 +576,11 @@ public class JImageGenerator {
return this;
}
public JLinkTask repeatedModulePath(String modulePath) {
this.repeatedModulePath = modulePath;
return this;
}
public JLinkTask addJars(Path jars) {
this.jars.add(jars);
return this;
@ -597,6 +606,11 @@ public class JImageGenerator {
return this;
}
public JLinkTask repeatedLimitMods(String modules) {
this.repeatedLimitMods = modules;
return this;
}
public JLinkTask output(Path output) {
this.output = output;
return this;
@ -639,6 +653,10 @@ public class JImageGenerator {
options.add(LIMIT_MODULES_OPTION);
options.add(limitMods.stream().collect(Collectors.joining(",")));
}
if (repeatedLimitMods != null) {
options.add(LIMIT_MODULES_OPTION);
options.add(repeatedLimitMods);
}
if (!jars.isEmpty() || !jmods.isEmpty()) {
options.add(MODULE_PATH_OPTION);
options.add(modulePath());
@ -647,6 +665,10 @@ public class JImageGenerator {
options.add(MODULE_PATH_OPTION);
options.add(modulePath);
}
if (repeatedModulePath != null) {
options.add(MODULE_PATH_OPTION);
options.add(repeatedModulePath);
}
if (!pluginModulePath.isEmpty()) {
options.add(PLUGIN_MODULE_PATH);
options.add(toPath(pluginModulePath));