changeset 2968:31fd9957342a

add checks for module/package conflicts
author jjg
date Fri, 03 Apr 2015 15:10:18 -0700
parents aa22f1ec8ded
children 1477517b933e
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java 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/tools/javac/modules/PackageConflictTest.java test/tools/javac/modules/RequiresPublicTest.java
diffstat 6 files changed, 81 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Thu Apr 02 18:57:23 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Fri Apr 03 15:10:18 2015 -0700
@@ -611,8 +611,9 @@
         PackageSymbol owner = packageName.isEmpty()
                                 ? unnamedPackage
                                 : enterPackage(msym, packageName);
-        if (owner != unnamedPackage && owner.modle == null && msym != null)
+        if (owner != unnamedPackage && owner.modle == null && msym != null) {
             owner.modle = msym;
+        }
         cs = defineClass(Convert.shortName(flatName), owner);
         cs.classfile = classFile;
         classes.put(flatName, cs);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java	Thu Apr 02 18:57:23 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java	Fri Apr 03 15:10:18 2015 -0700
@@ -323,10 +323,15 @@
                 tree.packge = syms.unnamedPackage;
             }
 
-            // TODO: add proper checks
-            // should always be safe to install tree.modle if compatible
-            if (tree.modle != null && tree.modle.name != null)
-                tree.packge.modle = tree.modle;
+            // TODO: when javac supports multiple private packages in different
+            // modules, this will need to be updated.
+            if (tree.modle != null && tree.modle.name != null) {
+                if (tree.packge.modle == null) {
+                    tree.packge.modle = tree.modle;
+                } else if (tree.packge.modle != tree.modle) {
+                    log.error(pd, "package.in.other.module", tree.packge.modle);
+                }
+            }
 
             tree.packge.complete(); // Find all classes in package.
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Thu Apr 02 18:57:23 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Fri Apr 03 15:10:18 2015 -0700
@@ -161,11 +161,11 @@
             for (ModuleSymbol msym: rootModules) {
                 msym.complete();
             }
-
-            return (log.nerrors == startErrors);
         } finally {
             depth--;
         }
+
+        return (log.nerrors == startErrors);
     }
 
     public Completer getCompleter() {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Apr 02 18:57:23 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Apr 03 15:10:18 2015 -0700
@@ -2658,6 +2658,10 @@
     unexpected input after module declaration
 
 # 0: symbol
+compiler.err.package.in.other.module=\
+    package exists in another module: {0}
+
+# 0: symbol
 compiler.err.cyclic.requires=\
     cyclic dependence involving {0}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/modules/PackageConflictTest.java	Fri Apr 03 15:10:18 2015 -0700
@@ -0,0 +1,62 @@
+/*
+ * 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
+ * @summary test module/package conflicts
+ * @library /tools/lib
+ * @modules
+ *      jdk.compiler/com.sun.tools.javac.api
+ *      jdk.compiler/com.sun.tools.javac.main
+ * @build ToolBox ModuleTestBase
+ * @run main PackageConflictTest
+ */
+
+import java.nio.file.*;
+
+public class PackageConflictTest extends ModuleTestBase {
+    public static void main(String... args) throws Exception {
+        PackageConflictTest t = new PackageConflictTest();
+        t.runTests();
+    }
+
+    @Test
+    void testSimple(Path base) throws Exception {
+        Path src = base.resolve("src");
+        tb.writeJavaFiles(src,
+                "package java.util; public class MyList { }");
+        Path classes = base.resolve("classes");
+        Files.createDirectories(classes);
+
+        String log = tb.new JavacTask()
+                .options("-XDrawDiagnostics")
+                .outdir(classes.toString())
+                .files(findJavaFiles(src))
+                .run(ToolBox.Expect.FAIL)
+                .writeAll()
+                .getOutput(ToolBox.OutputKind.DIRECT);
+
+        if (!log.contains("MyList.java:1:13: compiler.err.package.in.other.module: java.base"))
+            throw new Exception("expected output not found");
+    }
+}
--- a/test/tools/javac/modules/RequiresPublicTest.java	Thu Apr 02 18:57:23 2015 -0700
+++ b/test/tools/javac/modules/RequiresPublicTest.java	Fri Apr 03 15:10:18 2015 -0700
@@ -33,7 +33,6 @@
 
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 
 public class RequiresPublicTest extends ModuleTestBase {
 
@@ -186,7 +185,7 @@
 
         Path src_m6 = src.resolve("m6");
         tb.writeJavaFiles(src_m6,
-                "module m6 { requires public m7; exports p7; }",
+                "module m6 { requires public m7; exports p6; }",
                 "package p6;\n"
                 + "public class C6 { }\n");
 
@@ -198,4 +197,4 @@
 
         return src;
     }
-}
\ No newline at end of file
+}