changeset 4948:31edb164e288

8181925: Confusing error when unnamed module reads multiple modules exporting the same package. Summary: Adding special wording for package clash in modules read by the unnamed module.
author jlahoda
date Mon, 12 Jun 2017 22:30:57 +0200
parents 89a9c0aca926
children 046969a94992 e035894df61f
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 test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/PackageClashFromRequiresInUnnamed.java test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib1x/exported/Api1.java test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib1x/module-info.java test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib2x/exported/Api2.java test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib2x/module-info.java test/tools/javac/modules/PackageConflictTest.java
diffstat 8 files changed, 196 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Thu Jun 08 18:37:52 2017 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Mon Jun 12 22:30:57 2017 +0200
@@ -1574,8 +1574,13 @@
                                                             : null;
                     DiagnosticPosition pos = env != null ? env.tree.pos() : null;
                     try {
-                        log.error(pos, Errors.PackageClashFromRequires(msym, packageName,
-                                                                      previousModule, exportsFrom));
+                        if (msym.isUnnamed()) {
+                            log.error(pos, Errors.PackageClashFromRequiresInUnnamed(packageName,
+                                                                                    previousModule, exportsFrom));
+                        } else {
+                            log.error(pos, Errors.PackageClashFromRequires(msym, packageName,
+                                                                           previousModule, exportsFrom));
+                        }
                     } finally {
                         if (env != null)
                             log.useSource(origSource);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jun 08 18:37:52 2017 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Jun 12 22:30:57 2017 +0200
@@ -2990,6 +2990,10 @@
 compiler.err.package.clash.from.requires=\
     module {0} reads package {1} from both {2} and {3}
 
+# 0: name, 1: symbol, 2: symbol
+compiler.err.package.clash.from.requires.in.unnamed=\
+    the unnamed module reads package {0} from both {1} and {2}
+
 # 0: string
 compiler.err.module.not.found.in.module.source.path=\
     module {0} not found in module source path
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/PackageClashFromRequiresInUnnamed.java	Mon Jun 12 22:30:57 2017 +0200
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+// key: compiler.err.package.clash.from.requires.in.unnamed
+// options: --add-modules ALL-MODULE-PATH
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib1x/exported/Api1.java	Mon Jun 12 22:30:57 2017 +0200
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+package exported;
+
+public class Api1 {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib1x/module-info.java	Mon Jun 12 22:30:57 2017 +0200
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+module lib1x {
+     exports exported;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib2x/exported/Api2.java	Mon Jun 12 22:30:57 2017 +0200
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+package exported;
+
+public class Api2 {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/PackageClashFromRequiresInUnnamed/modulepath/lib2x/module-info.java	Mon Jun 12 22:30:57 2017 +0200
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+module lib2x {
+     exports exported;
+}
--- a/test/tools/javac/modules/PackageConflictTest.java	Thu Jun 08 18:37:52 2017 +0100
+++ b/test/tools/javac/modules/PackageConflictTest.java	Mon Jun 12 22:30:57 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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
@@ -277,4 +277,57 @@
             throw new Exception("expected output not found");
         }
     }
+
+    @Test
+    public void testConflictInDependenciesInUnnamed(Path base) throws Exception {
+        Path msp = base.resolve("module-path-source");
+        Path m1 = msp.resolve("m1x");
+        Path m2 = msp.resolve("m2x");
+        tb.writeJavaFiles(m1,
+                          "module m1x { exports test; }",
+                          "package test; public class A { }");
+        tb.writeJavaFiles(m2,
+                          "module m2x { exports test; }",
+                          "package test; public class B { }");
+        Path mp = base.resolve("module-path");
+        Files.createDirectories(mp);
+
+        new JavacTask(tb)
+            .options("--module-source-path", msp.toString())
+            .outdir(mp)
+            .files(findJavaFiles(msp))
+            .run()
+            .writeAll();
+
+        Path src = base.resolve("src");
+
+        tb.writeJavaFiles(src,
+                          "package impl; public class Impl { }");
+
+        Path out = base.resolve("out");
+        Files.createDirectories(out);
+
+        List<String> log = new JavacTask(tb)
+                       .options("-XDrawDiagnostic",
+                                "--module-path", mp.toString(),
+                                "--add-modules", "ALL-MODULE-PATH")
+                       .outdir(out)
+                       .files(findJavaFiles(src))
+                       .run(Task.Expect.FAIL)
+                       .writeAll()
+                       .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected1 =
+                Arrays.asList("error: the unnamed module reads package test from both m1x and m2x",
+                              "1 error");
+
+        List<String> expected2 =
+                Arrays.asList("error: the unnamed module reads package test from both m2x and m1x",
+                              "1 error");
+
+        if (!expected1.equals(log) && !expected2.equals(log)) {
+            throw new AssertionError("Unexpected output: " + log);
+        }
+    }
+
 }