8220702: compiling in the context of an automatic module disallows --add-modules ALL-MODULE-PATH

Allow --add-modules ALL-MODULE-PATH when compiling in the context of an automatic module.

Reviewed-by: jjg
This commit is contained in:
Jan Lahoda 2019-05-24 12:25:18 +02:00
parent 9a193f38a3
commit 0112514fde
3 changed files with 96 additions and 5 deletions

View File

@ -355,7 +355,7 @@ public class Modules extends JCTree.Visitor {
private void setCompilationUnitModules(List<JCCompilationUnit> trees, Set<ModuleSymbol> rootModules, ClassSymbol c) {
// update the module for each compilation unit
if (multiModuleMode) {
checkNoAllModulePath();
boolean patchesAutomaticModules = false;
for (JCCompilationUnit tree: trees) {
if (tree.defs.isEmpty()) {
tree.modle = syms.unnamedModule;
@ -375,6 +375,7 @@ public class Modules extends JCTree.Visitor {
ModuleSymbol msym = moduleFinder.findModule(name);
tree.modle = msym;
rootModules.add(msym);
patchesAutomaticModules |= (msym.flags_field & Flags.AUTOMATIC_MODULE) != 0;
if (msplocn != null) {
Name mspname = names.fromString(fileManager.inferModuleName(msplocn));
@ -438,6 +439,9 @@ public class Modules extends JCTree.Visitor {
log.useSource(prev);
}
}
if (!patchesAutomaticModules) {
checkNoAllModulePath();
}
if (syms.unnamedModule.sourceLocation == null) {
syms.unnamedModule.completer = getUnnamedModuleCompleter();
syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
@ -458,9 +462,11 @@ public class Modules extends JCTree.Visitor {
}
if (defaultModule == syms.unnamedModule) {
if (moduleOverride != null) {
checkNoAllModulePath();
defaultModule = moduleFinder.findModule(names.fromString(moduleOverride));
defaultModule.patchOutputLocation = StandardLocation.CLASS_OUTPUT;
if ((defaultModule.flags_field & Flags.AUTOMATIC_MODULE) == 0) {
checkNoAllModulePath();
}
} else {
// Question: why not do findAllModules and initVisiblePackages here?
// i.e. body of unnamedModuleCompleter

View File

@ -3301,7 +3301,8 @@ compiler.warn.module.for.option.not.found=\
module name in {0} option not found: {1}
compiler.err.addmods.all.module.path.invalid=\
--add-modules ALL-MODULE-PATH can only be used when compiling the unnamed module
--add-modules ALL-MODULE-PATH can only be used when compiling the unnamed module or \
when compiling in the context of an automatic module
# 0: symbol
compiler.err.add.exports.with.release=\

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019, 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
@ -23,7 +23,7 @@
/**
* @test
* @bug 8155026 8178011
* @bug 8155026 8178011 8220702
* @summary Test automatic modules
* @library /tools/lib
* @modules
@ -818,4 +818,88 @@ public class AutomaticModules extends ModuleTestBase {
}
}
@Test
public void testAutomaticModulePatchingAndAllModulePath(Path base) throws Exception {
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
Path libaSrc = base.resolve("libaSrc");
tb.writeJavaFiles(libaSrc,
"module liba { exports api1; }",
"package api1; public class Api1 {}");
Path libaClasses = modulePath.resolve("liba");
tb.createDirectories(libaClasses);
new JavacTask(tb)
.outdir(libaClasses)
.files(findJavaFiles(libaSrc))
.run()
.writeAll();
Path libbSrc = base.resolve("libbSrc");
tb.writeJavaFiles(libbSrc,
"module libb { exports api2; }",
"package api2; public class Api2 {}");
Path libbClasses = modulePath.resolve("libb");
tb.createDirectories(libbClasses);
new JavacTask(tb)
.outdir(libbClasses)
.files(findJavaFiles(libbSrc))
.run()
.writeAll();
Path automaticSrc = base.resolve("automaticSrc");
tb.writeJavaFiles(automaticSrc, "package aut; public class Aut1 { api1.Api1 a1; }");
Path automaticClasses = base.resolve("automaticClasses");
tb.createDirectories(automaticClasses);
new JavacTask(tb)
.outdir(automaticClasses)
.options("--add-modules", "liba",
"--module-path", modulePath.toString())
.files(findJavaFiles(automaticSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
Path automaticJar = modulePath.resolve("automatic-1.0.jar");
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(automaticJar))) {
out.putNextEntry(new ZipEntry("aut/Aut1.class"));
Files.copy(automaticClasses.resolve("aut").resolve("Aut1.class"), out);
}
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"package aut; public class Aut2 { api2.Api2 a2; aut.Aut1 aut1;}");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--patch-module", "automatic=" + src.toString(),
"--add-modules", "ALL-MODULE-PATH")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--patch-module", "automatic=" + src.toString(),
"--module-source-path", "dummy",
"--add-modules", "ALL-MODULE-PATH")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
}
}