src/java.base/share/classes/jdk/internal/module/Checks.java
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
equal deleted inserted replaced
53017:e10a1f7aaa13 53018:8bf9268df0e2
    61         }
    61         }
    62         return name;
    62         return name;
    63     }
    63     }
    64 
    64 
    65     /**
    65     /**
    66      * Returns {@code true} if the given name is a legal module name.
       
    67      */
       
    68     public static boolean isModuleName(String name) {
       
    69         int next;
       
    70         int off = 0;
       
    71         while ((next = name.indexOf('.', off)) != -1) {
       
    72             String id = name.substring(off, next);
       
    73             if (!isJavaIdentifier(id))
       
    74                 return false;
       
    75             off = next+1;
       
    76         }
       
    77         String last = name.substring(off);
       
    78         return isJavaIdentifier(last);
       
    79     }
       
    80 
       
    81     /**
       
    82      * Checks a name to ensure that it's a legal package name.
    66      * Checks a name to ensure that it's a legal package name.
    83      *
    67      *
    84      * @throws IllegalArgumentException if name is null or not a legal
    68      * @throws IllegalArgumentException if name is null or not a legal
    85      *         package name
    69      *         package name
    86      */
    70      */
   179         }
   163         }
   180         return name;
   164         return name;
   181     }
   165     }
   182 
   166 
   183     /**
   167     /**
   184      * Returns true if the given char sequence is a legal Java identifier,
   168      * Returns true if the given string is a legal Java identifier,
   185      * otherwise false.
   169      * otherwise false.
   186      */
   170      */
   187     private static boolean isJavaIdentifier(CharSequence cs) {
   171     private static boolean isJavaIdentifier(String str) {
   188         if (cs.length() == 0 || RESERVED.contains(cs))
   172         if (str.isEmpty() || RESERVED.contains(str))
   189             return false;
   173             return false;
   190 
   174 
   191         int first = Character.codePointAt(cs, 0);
   175         int first = Character.codePointAt(str, 0);
   192         if (!Character.isJavaIdentifierStart(first))
   176         if (!Character.isJavaIdentifierStart(first))
   193             return false;
   177             return false;
   194 
   178 
   195         int i = Character.charCount(first);
   179         int i = Character.charCount(first);
   196         while (i < cs.length()) {
   180         while (i < str.length()) {
   197             int cp = Character.codePointAt(cs, i);
   181             int cp = Character.codePointAt(str, i);
   198             if (!Character.isJavaIdentifierPart(cp))
   182             if (!Character.isJavaIdentifierPart(cp))
   199                 return false;
   183                 return false;
   200             i += Character.charCount(cp);
   184             i += Character.charCount(cp);
   201         }
   185         }
   202 
   186