< prev index next >

src/java.base/share/classes/jdk/internal/module/Checks.java

Print this page




 163             throw new IllegalArgumentException("Null " + what);
 164         int next;
 165         int off = 0;
 166         while ((next = name.indexOf('.', off)) != -1) {
 167             if (isJavaIdentifier(name, off, (next - off)) == -1) {
 168                 String id = name.substring(off, next);
 169                 throw new IllegalArgumentException(name + ": Invalid " + what
 170                         + ": '" + id + "' is not a Java identifier");
 171             }
 172             off = next + 1;
 173         }
 174         if (isJavaIdentifier(name, off, name.length() - off) == -1) {
 175             String id = name.substring(off, name.length());
 176             throw new IllegalArgumentException(name + ": Invalid " + what
 177                     + ": '" + id + "' is not a Java identifier");
 178         }
 179         return name;
 180     }
 181 
 182     /**
 183      * Returns {@code true} if the last character of the given name is legal
 184      * as the last character of a module name.
 185      *
 186      * @throws IllegalArgumentException if name is empty
 187      */
 188     public static boolean hasLegalModuleNameLastCharacter(String name) {
 189         if (name.isEmpty())
 190             throw new IllegalArgumentException("name is empty");
 191         int len = name.length();
 192         if (isASCIIString(name)) {
 193             char c = name.charAt(len-1);
 194             return Character.isJavaIdentifierStart(c);
 195         } else {
 196             int i = 0;
 197             int cp = -1;
 198             while (i < len) {
 199                 cp = name.codePointAt(i);
 200                 i += Character.charCount(cp);
 201             }
 202             return Character.isJavaIdentifierStart(cp);
 203         }

 204     }
 205 
 206     /**
 207      * Returns true if the given string only contains ASCII characters.
 208      */
 209     private static boolean isASCIIString(String s) {
 210         int i = 0;
 211         while (i < s.length()) {
 212             int c = s.charAt(i);
 213             if (c > 0x7F)
 214                 return false;
 215             i++;








 216         }


 217         return true;


 218     }
 219 
 220     /**
 221      * Checks if a char sequence is a legal Java identifier, returning the code
 222      * point of the last character if legal or {@code -1} if not legal.
 223      */
 224     private static int isJavaIdentifier(CharSequence cs, int offset, int count) {
 225         if (count == 0)
 226             return -1;
 227         int first = Character.codePointAt(cs, offset);
 228         if (!Character.isJavaIdentifierStart(first))
 229             return -1;
 230 
 231         int cp = first;
 232         int i = Character.charCount(first);
 233         while (i < count) {
 234             cp = Character.codePointAt(cs, offset+i);
 235             if (!Character.isJavaIdentifierPart(cp))
 236                 return -1;
 237             i += Character.charCount(cp);


 163             throw new IllegalArgumentException("Null " + what);
 164         int next;
 165         int off = 0;
 166         while ((next = name.indexOf('.', off)) != -1) {
 167             if (isJavaIdentifier(name, off, (next - off)) == -1) {
 168                 String id = name.substring(off, next);
 169                 throw new IllegalArgumentException(name + ": Invalid " + what
 170                         + ": '" + id + "' is not a Java identifier");
 171             }
 172             off = next + 1;
 173         }
 174         if (isJavaIdentifier(name, off, name.length() - off) == -1) {
 175             String id = name.substring(off, name.length());
 176             throw new IllegalArgumentException(name + ": Invalid " + what
 177                     + ": '" + id + "' is not a Java identifier");
 178         }
 179         return name;
 180     }
 181 
 182     /**
 183      * Returns {@code true} if a given legal module name contains an identifier
 184      * that doesn't end with a Java letter.


 185      */
 186     public static boolean hasJavaIdentifierWithTrailingDigit(String name) {
 187         // quick scan to allow names that are just ASCII without digits
 188         boolean needToParse = false;





 189         int i = 0;
 190         while (i < name.length()) {
 191             int c = name.charAt(i);
 192             if (c > 0x7F || (c >= '0' && c <= '9')) {
 193                 needToParse = true;
 194                 break;

 195             }
 196             i++;
 197         }
 198         if (!needToParse)








 199             return false;
 200 
 201         // slow path
 202         int next;
 203         int off = 0;
 204         while ((next = name.indexOf('.', off)) != -1) {
 205             int last = isJavaIdentifier(name, off, (next - off));
 206             if (!Character.isJavaIdentifierStart(last))
 207                 return true;
 208             off = next+1;
 209         }
 210         int last = isJavaIdentifier(name, off, name.length() - off);
 211         if (!Character.isJavaIdentifierStart(last))
 212             return true;
 213         return false;
 214 
 215     }
 216 
 217     /**
 218      * Checks if a char sequence is a legal Java identifier, returning the code
 219      * point of the last character if legal or {@code -1} if not legal.
 220      */
 221     private static int isJavaIdentifier(CharSequence cs, int offset, int count) {
 222         if (count == 0)
 223             return -1;
 224         int first = Character.codePointAt(cs, offset);
 225         if (!Character.isJavaIdentifierStart(first))
 226             return -1;
 227 
 228         int cp = first;
 229         int i = Character.charCount(first);
 230         while (i < count) {
 231             cp = Character.codePointAt(cs, offset+i);
 232             if (!Character.isJavaIdentifierPart(cp))
 233                 return -1;
 234             i += Character.charCount(cp);
< prev index next >