8177311: Denied access when named module accesses unreferences package from the unnamed module
authorjlahoda
Wed, 29 Mar 2017 10:27:23 +0200
changeset 44451 1619c00af9df
parent 44450 eb4f067bae4c
child 44452 93f6470b1045
8177311: Denied access when named module accesses unreferences package from the unnamed module Summary: Ensure access to the unnamed module is allowed if the given module reads the unnamed module. Reviewed-by: jjg
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/modules/EdgeCases.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Mon Mar 27 17:53:00 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Mar 29 10:27:23 2017 +0200
@@ -334,7 +334,10 @@
                     currModule.complete();
                     PackageSymbol p = c.packge();
                     isAccessible =
-                        (currModule == p.modle) || currModule.visiblePackages.get(p.fullname) == p || p == syms.rootPackage;
+                        currModule == p.modle ||
+                        currModule.visiblePackages.get(p.fullname) == p ||
+                        p == syms.rootPackage ||
+                        (p.modle == syms.unnamedModule && currModule.readModules.contains(p.modle));
                 } else {
                     isAccessible = true;
                 }
--- a/langtools/test/tools/javac/modules/EdgeCases.java	Mon Mar 27 17:53:00 2017 -0700
+++ b/langtools/test/tools/javac/modules/EdgeCases.java	Wed Mar 29 10:27:23 2017 +0200
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8154283 8167320 8171098 8172809 8173068 8173117 8176045
+ * @bug 8154283 8167320 8171098 8172809 8173068 8173117 8176045 8177311
  * @summary tests for multi-module mode compilation
  * @library /tools/lib
  * @modules
@@ -958,4 +958,41 @@
             throw new Exception("expected output not found: " + log);
     }
 
+    @Test
+    public void testDependOnUnnamedAccessibility(Path base) throws Exception {
+        Path unnamedSrc = base.resolve("unnamed-src");
+        tb.writeJavaFiles(unnamedSrc,
+                          "package p1; public class First { public static p2.Second get() { return null; } }",
+                          "package p2; public class Second { public void test() { } }");
+        Path unnamedClasses = base.resolve("unnamed-classes");
+        tb.createDirectories(unnamedClasses);
+
+        System.err.println("compiling unnamed sources:");
+
+        new JavacTask(tb)
+                .outdir(unnamedClasses)
+                .files(findJavaFiles(unnamedSrc))
+                .run()
+                .writeAll();
+
+        //test sources:
+        Path src = base.resolve("src");
+        Path m = src.resolve("m");
+        tb.writeJavaFiles(m,
+                          "module m { }",
+                          "package p; public class Test { { p1.First.get().test(); } }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        System.err.println("compiling test module:");
+
+        new JavacTask(tb)
+            .options("-classpath", unnamedClasses.toString(),
+                     "--add-reads", "m=ALL-UNNAMED")
+            .outdir(classes)
+            .files(findJavaFiles(src))
+            .run()
+            .writeAll();
+    }
+
 }