8220702: compiling in the context of an automatic module disallows --add-modules ALL-MODULE-PATH
authorjlahoda
Fri, 24 May 2019 12:25:18 +0200
changeset 55027 adb2493b120e
parent 55026 a8673ccddffd
child 55028 da5435d9a801
8220702: compiling in the context of an automatic module disallows --add-modules ALL-MODULE-PATH Summary: Allow --add-modules ALL-MODULE-PATH when compiling in the context of an automatic module. Reviewed-by: jjg
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
test/langtools/tools/javac/modules/AutomaticModules.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Thu May 23 12:36:54 2019 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Fri May 24 12:25:18 2019 +0200
@@ -355,7 +355,7 @@
     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 @@
                         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 @@
                     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 @@
                         }
                         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
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu May 23 12:36:54 2019 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri May 24 12:25:18 2019 +0200
@@ -3301,7 +3301,8 @@
     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=\
--- a/test/langtools/tools/javac/modules/AutomaticModules.java	Thu May 23 12:36:54 2019 +0530
+++ b/test/langtools/tools/javac/modules/AutomaticModules.java	Fri May 24 12:25:18 2019 +0200
@@ -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 @@
         }
     }
 
+    @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);
+    }
+
 }