changeset 2983:840578290306

8077962: NPE if module name declaration differs from directory name
author jjg
date Thu, 16 Apr 2015 15:18:05 -0700
parents 05331b4fc718
children 8d701bdbfa63
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/modules/MultiModuleModeTest.java
diffstat 3 files changed, 35 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Fri Apr 10 09:49:03 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Thu Apr 16 15:18:05 2015 -0700
@@ -241,7 +241,16 @@
                     Location locn = getModuleLocation(tree);
                     if (locn != null) {
                         Name name = names.fromString(fileManager.inferModuleName(locn));
-                        ModuleSymbol msym = syms.enterModule(name);
+                        ModuleSymbol msym;
+                        if (tree.defs.head.hasTag(MODULEDEF)) {
+                            JCModuleDecl decl = (JCModuleDecl) tree.defs.head;
+                            msym = decl.sym;
+                            if (msym.name != name) {
+                                log.error(decl.qualId, "module.name.mismatch", msym.name, name);
+                            }
+                        } else {
+                            msym = syms.enterModule(name);
+                        }
                         if (msym.sourceLocation == null) {
                             msym.sourceLocation = locn;
                             if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Apr 10 09:49:03 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Apr 16 15:18:05 2015 -0700
@@ -2651,6 +2651,10 @@
 compiler.err.cant.determine.module=\
     cannot determine module on module source path
 
+# 0: name, 1: name
+compiler.err.module.name.mismatch=\
+    module name {0} does not match expected name {1}
+
 compiler.err.module.decl.sb.in.module-info.java=\
     module declarations should be in a file named module-info.java
 
--- a/test/tools/javac/modules/MultiModuleModeTest.java	Fri Apr 10 09:49:03 2015 -0700
+++ b/test/tools/javac/modules/MultiModuleModeTest.java	Thu Apr 16 15:18:05 2015 -0700
@@ -91,6 +91,27 @@
     }
 
     @Test
+    void testModuleNameMismatch(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path src_m1 = src.resolve("m1");
+        tb.writeJavaFiles(src_m1, "module m2 { }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        String log = tb.new JavacTask()
+                .options("-XDrawDiagnostics",
+                        "-modulesourcepath", src.toString())
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(ToolBox.Expect.FAIL)
+                .writeAll()
+                .getOutput(ToolBox.OutputKind.DIRECT);
+
+        if (!log.contains("module-info.java:1:8: compiler.err.module.name.mismatch: m2, m1"))
+            throw new Exception("expected output not found");
+    }
+
+    @Test
     void testImplicitModuleSource(Path base) throws Exception {
         Path src = base.resolve("src");
         tb.writeJavaFiles(src.resolve("m1"), "module m1 { }");