8176572: Javac does not enforce module name restrictions
authorsadayapalam
Wed, 05 Apr 2017 14:34:15 +0530
changeset 44568 3d870d326626
parent 44567 126285be00b9
child 44569 b1accf8b2aed
8176572: Javac does not enforce module name restrictions Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/modules/PoorChoiceForModuleNameTest.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Tue Apr 04 23:04:39 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Wed Apr 05 14:34:15 2017 +0530
@@ -2107,10 +2107,32 @@
         Name moduleName = tree.sym.name;
         Assert.checkNonNull(moduleName);
         if (lint.isEnabled(LintCategory.MODULE)) {
-            String moduleNameString = moduleName.toString();
-            int nameLength = moduleNameString.length();
-            if (nameLength > 0 && Character.isDigit(moduleNameString.charAt(nameLength - 1))) {
-                log.warning(Lint.LintCategory.MODULE, tree.qualId.pos(), Warnings.PoorChoiceForModuleName(moduleName));
+            JCExpression qualId = tree.qualId;
+            while (qualId != null) {
+                Name componentName;
+                DiagnosticPosition pos;
+                switch (qualId.getTag()) {
+                    case SELECT:
+                        JCFieldAccess selectNode = ((JCFieldAccess) qualId);
+                        componentName = selectNode.name;
+                        pos = selectNode.pos();
+                        qualId = selectNode.selected;
+                        break;
+                    case IDENT:
+                        componentName = ((JCIdent) qualId).name;
+                        pos = qualId.pos();
+                        qualId = null;
+                        break;
+                    default:
+                        throw new AssertionError("Unexpected qualified identifier: " + qualId.toString());
+                }
+                if (componentName != null) {
+                    String moduleNameComponentString = componentName.toString();
+                    int nameLength = moduleNameComponentString.length();
+                    if (nameLength > 0 && Character.isDigit(moduleNameComponentString.charAt(nameLength - 1))) {
+                        log.warning(Lint.LintCategory.MODULE, pos, Warnings.PoorChoiceForModuleName(componentName));
+                    }
+                }
             }
         }
     }
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Apr 04 23:04:39 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Apr 05 14:34:15 2017 +0530
@@ -1537,7 +1537,7 @@
 
 # 0: name
 compiler.warn.poor.choice.for.module.name=\
-    module name {0} should avoid terminal digits
+    module name component {0} should avoid terminal digits
 
 # 0: string
 compiler.warn.incubating.modules=\
--- a/langtools/test/tools/javac/modules/PoorChoiceForModuleNameTest.java	Tue Apr 04 23:04:39 2017 -0700
+++ b/langtools/test/tools/javac/modules/PoorChoiceForModuleNameTest.java	Wed Apr 05 14:34:15 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8160181
+ * @bug 8160181 8176572
  * @summary Add lint warning for digits in module names
  * @library /tools/lib
  * @modules
@@ -63,6 +63,22 @@
         Path src_m3 = src.resolve("mango100");
         tb.writeJavaFiles(src_m3, "@SuppressWarnings(\"module\") module mango100 { }");
 
+        // Check that there is no warning at use site.
+        Path src_m4 = src.resolve("mangouser");
+        tb.writeJavaFiles(src_m4, "module mangouser { requires mango19; }");
+
+        // Check that we warn about component names ending in digit also
+        Path src_m5 = src.resolve("mango1000.mangofruit.mangomodule");
+        tb.writeJavaFiles(src_m5, "module mango1000.mangofruit.mangomodule { }");
+
+        // Check that we warn about component names ending in digit also
+        Path src_m6 = src.resolve("mangofruit.mango1000.mangomodule");
+        tb.writeJavaFiles(src_m6, "module mangofruit.mango1000.mangomodule { }");
+
+        // Check that we warn about component names ending in digit also
+        Path src_m7 = src.resolve("mangomodule.mangofruit.mango1000");
+        tb.writeJavaFiles(src_m7, "module mangomodule.mangofruit.mango1000 { }");
+
         Path classes = base.resolve("classes");
         tb.createDirectories(classes);
 
@@ -78,9 +94,12 @@
                 .getOutput(Task.OutputKind.DIRECT);
 
         if (!log.contains("module-info.java:1:8: compiler.warn.poor.choice.for.module.name: mango19") ||
+            !log.contains("module-info.java:1:8: compiler.warn.poor.choice.for.module.name: mango1000") ||
+            !log.contains("module-info.java:1:18: compiler.warn.poor.choice.for.module.name: mango1000") ||
+            !log.contains("module-info.java:1:30: compiler.warn.poor.choice.for.module.name: mango1000") ||
             !log.contains("- compiler.err.warnings.and.werror") ||
             !log.contains("1 error") ||
-            !log.contains("1 warning"))
+            !log.contains("4 warning"))
             throw new Exception("expected output not found: " + log);
     }
 }