changeset 3513:44535f5efc07

8144342: javac doesn't report errors if module exports non-existent package
author vromero
date Tue, 22 Dec 2015 16:25:08 -0800
parents 8a2b2c2caa15
children 9483d696696e
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties src/jdk.compiler/share/classes/module-info.java test/tools/javac/classfiles/attributes/Module/ModuleTest.java test/tools/javac/modules/ReportNonExistentPackageTest.java
diffstat 5 files changed, 100 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Tue Dec 22 13:03:15 2015 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Tue Dec 22 16:25:08 2015 -0800
@@ -607,6 +607,9 @@
         }
 
         public void visitExports(JCExports tree) {
+            if (tree.directive.packge.members().isEmpty()) {
+                log.error(tree.qualid.pos(), "package.empty.or.doesnt.exists", tree.directive.packge);
+            }
             msym.directives = msym.directives.prepend(tree.directive);
         }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Dec 22 13:03:15 2015 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Dec 22 16:25:08 2015 -0800
@@ -2698,6 +2698,10 @@
 compiler.err.bad.service.implementation=\
     the service implementation provided is abstract or an inner class: {0}
 
+# 0: symbol
+compiler.err.package.empty.or.doesnt.exists=\
+    package is empty or does not exists: {0}
+
 compiler.err.no.output.dir=\
     no class output directory specified
 
--- a/src/jdk.compiler/share/classes/module-info.java	Tue Dec 22 13:03:15 2015 -0800
+++ b/src/jdk.compiler/share/classes/module-info.java	Tue Dec 22 16:25:08 2015 -0800
@@ -53,8 +53,6 @@
         jdk.jshell;
     exports com.sun.tools.javac.platform to
         jdk.javadoc;
-    exports com.sun.tools.javac.sym to
-        jdk.javadoc;
     exports com.sun.tools.javac.tree to
         jdk.javadoc,
         jdk.jshell;
--- a/test/tools/javac/classfiles/attributes/Module/ModuleTest.java	Tue Dec 22 13:03:15 2015 -0800
+++ b/test/tools/javac/classfiles/attributes/Module/ModuleTest.java	Tue Dec 22 16:25:08 2015 -0800
@@ -67,6 +67,10 @@
                 .exports("pack2")
                 .exports("pack3")
                 .write(base);
+        tb.writeJavaFiles(base,
+                "package pack; public class A {}",
+                "package pack2; public class B {}",
+                "package pack3; public class C {}");
         compile(base);
         testModuleAttribute(base, moduleDescriptor);
     }
@@ -76,6 +80,7 @@
         ModuleDescriptor moduleDescriptor = new ModuleDescriptor("m1")
                 .exportsTo("pack", "jdk.compiler")
                 .write(base);
+        tb.writeJavaFiles(base, "package pack; public class A {}");
         compile(base);
         testModuleAttribute(base, moduleDescriptor);
     }
@@ -87,6 +92,10 @@
                 .exportsTo("pack2", "java.xml")
                 .exportsTo("pack3", "jdk.compiler")
                 .write(base);
+        tb.writeJavaFiles(base,
+                "package pack; public class A {}",
+                "package pack2; public class B {}",
+                "package pack3; public class C {}");
         compile(base);
         testModuleAttribute(base, moduleDescriptor);
     }
@@ -187,6 +196,9 @@
                 .write(base);
         tb.writeJavaFiles(base, "package pack1; public class C extends java.util.ArrayList{}",
                 "package pack2; public class D extends java.util.ArrayList{}");
+        tb.writeJavaFiles(base,
+                "package packTo1; public class T1 {}",
+                "package packTo2; public class T2 {}");
         compile(base);
         testModuleAttribute(base, moduleDescriptor);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/modules/ReportNonExistentPackageTest.java	Tue Dec 22 16:25:08 2015 -0800
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2015, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test 8144342
+ * @summary javac doesn't report errors if module exports non-existent package
+ * @library /tools/lib
+ * @modules
+ *      jdk.compiler/com.sun.tools.javac.api
+ *      jdk.compiler/com.sun.tools.javac.main
+ * @build ToolBox ModuleTestBase
+ * @run main ReportNonExistentPackageTest
+ */
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class ReportNonExistentPackageTest extends ModuleTestBase {
+    public static void main(String... args) throws Exception {
+        ReportNonExistentPackageTest t = new ReportNonExistentPackageTest();
+        t.runTests();
+    }
+
+    @Test
+    void testExportUnknownPackage(Path base) throws Exception {
+        Path src = base.resolve("src");
+        tb.writeJavaFiles(src, "module m { exports p1; }");
+        Path classes = base.resolve("classes");
+        Files.createDirectories(classes);
+
+        String log = tb.new JavacTask()
+                .options("-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(ToolBox.Expect.FAIL)
+                .writeAll()
+                .getOutput(ToolBox.OutputKind.DIRECT);
+        if (!log.contains("module-info.java:1:20: compiler.err.package.empty.or.doesnt.exists: p1"))
+            throw new Exception("expected output not found");
+    }
+
+    @Test
+    void testExportEmptyPackage(Path base) throws Exception {
+        Path src = base.resolve("src");
+        tb.writeJavaFiles(src,
+                "module m { exports p1; }",
+                "package p1;");
+        Path classes = base.resolve("classes");
+        Files.createDirectories(classes);
+
+        String log = tb.new JavacTask()
+                .options("-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(ToolBox.Expect.FAIL)
+                .writeAll()
+                .getOutput(ToolBox.OutputKind.DIRECT);
+        if (!log.contains("module-info.java:1:20: compiler.err.package.empty.or.doesnt.exists: p1"))
+            throw new Exception("expected output not found");
+    }
+}