8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.internal.module; 27 28 public final class Checks { 29 30 private Checks() { } 31 32 private static void fail(String what, String id, int i) { 33 throw new IllegalArgumentException(id 34 + ": Invalid " + what + ": " 35 + " Illegal character" 36 + " at index " + i); 37 } 38 39 /** 40 * Returns {@code true} if the given identifier is a legal Java identifier. 41 */ 42 public static boolean isJavaIdentifier(String id) { 43 int n = id.length(); 44 if (n == 0) 45 return false; 46 if (!Character.isJavaIdentifierStart(id.codePointAt(0))) 47 return false; 48 int cp = id.codePointAt(0); 49 int i = Character.charCount(cp); 50 for (; i < n; i += Character.charCount(cp)) { 51 cp = id.codePointAt(i); 52 if (!Character.isJavaIdentifierPart(cp) && id.charAt(i) != '.') 53 return false; 54 } 55 if (cp == '.') 56 return false; 57 58 return true; 59 } 60 61 /** 62 * Checks if a given identifier is a legal Java identifier. 63 */ 64 public static String requireJavaIdentifier(String what, String id) { 65 if (id == null) 66 throw new IllegalArgumentException("Null " + what); 67 int n = id.length(); 68 if (n == 0) 69 throw new IllegalArgumentException("Empty " + what); 70 if (!Character.isJavaIdentifierStart(id.codePointAt(0))) 71 fail(what, id, 0); 72 int cp = id.codePointAt(0); 73 int i = Character.charCount(cp); 74 int last = 0; 75 for (; i < n; i += Character.charCount(cp)) { 76 cp = id.codePointAt(i); 77 if (!Character.isJavaIdentifierPart(cp) && id.charAt(i) != '.') 78 fail(what, id, i); 79 last = i; 80 } 81 if (cp == '.') 82 fail(what, id, last); 83 84 return id; 85 } 86 87 public static String requireModuleName(String id) { 88 return requireJavaIdentifier("module name", id); 89 } 90 91 public static String requirePackageName(String id) { 92 return requireJavaIdentifier("package name", id); 93 } 94 95 public static String requireServiceTypeName(String id) { 96 return requireJavaIdentifier("service type name", id); 97 } 98 99 public static String requireServiceProviderName(String id) { 100 return requireJavaIdentifier("service provider name", id); 101 } 102 103 } | 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.internal.module; 27 28 /** 29 * Utility class for checking module name and binary names. 30 */ 31 32 public final class Checks { 33 34 private Checks() { } 35 36 /** 37 * Checks a name to ensure that it's a legal module name. 38 * 39 * @throws IllegalArgumentException if name is null or not a legal 40 * module name 41 */ 42 public static String requireModuleName(String name) { 43 if (name == null) 44 throw new IllegalArgumentException("Null module name"); 45 int next; 46 int off = 0; 47 while ((next = name.indexOf('.', off)) != -1) { 48 if (isJavaIdentifier(name, off, (next - off)) == -1) { 49 String id = name.substring(off, next); 50 throw new IllegalArgumentException(name + ": Invalid module name" 51 + ": '" + id + "' is not a Java identifier"); 52 } 53 off = next+1; 54 } 55 int last = isJavaIdentifier(name, off, name.length() - off); 56 if (last == -1) { 57 String id = name.substring(off); 58 throw new IllegalArgumentException(name + ": Invalid module name" 59 + ": '" + id + "' is not a Java identifier"); 60 } 61 //if (!Character.isJavaIdentifierStart(last)) 62 // throw new IllegalArgumentException(name + ": Module name ends in digit"); 63 return name; 64 } 65 66 /** 67 * Returns {@code true} if the given name is a legal module name. 68 */ 69 public static boolean isModuleName(String name) { 70 int next; 71 int off = 0; 72 while ((next = name.indexOf('.', off)) != -1) { 73 if (isJavaIdentifier(name, off, (next - off)) == -1) 74 return false; 75 off = next+1; 76 } 77 int last = isJavaIdentifier(name, off, name.length() - off); 78 if (last == -1) 79 return false; 80 //if (!Character.isJavaIdentifierStart(last)) 81 // return false; 82 return true; 83 } 84 85 /** 86 * Checks a name to ensure that it's a legal package name. 87 * 88 * @throws IllegalArgumentException if name is null or not a legal 89 * package name 90 */ 91 public static String requirePackageName(String name) { 92 return requireBinaryName("package name", name); 93 } 94 95 /** 96 * Checks a name to ensure that it's a legal type name. 97 * 98 * @throws IllegalArgumentException if name is null or not a legal 99 * type name 100 */ 101 public static String requireServiceTypeName(String name) { 102 return requireBinaryName("service type name", name); 103 } 104 105 /** 106 * Checks a name to ensure that it's a legal type name. 107 * 108 * @throws IllegalArgumentException if name is null or not a legal 109 * type name 110 */ 111 public static String requireServiceProviderName(String name) { 112 return requireBinaryName("service provider name", name); 113 } 114 115 /** 116 * Returns {@code true} if the given name is a legal binary name. 117 */ 118 public static boolean isJavaIdentifier(String name) { 119 return isBinaryName(name); 120 } 121 122 /** 123 * Returns {@code true} if the given name is a legal binary name. 124 */ 125 public static boolean isBinaryName(String name) { 126 int next; 127 int off = 0; 128 while ((next = name.indexOf('.', off)) != -1) { 129 if (isJavaIdentifier(name, off, (next - off)) == -1) 130 return false; 131 off = next+1; 132 } 133 int count = name.length() - off; 134 return (isJavaIdentifier(name, off, count) != -1); 135 } 136 137 /** 138 * Checks if the given name is a legal binary name. 139 * 140 * @throws IllegalArgumentException if name is null or not a legal 141 * binary name 142 */ 143 public static String requireBinaryName(String what, String name) { 144 if (name == null) 145 throw new IllegalArgumentException("Null " + what); 146 int next; 147 int off = 0; 148 while ((next = name.indexOf('.', off)) != -1) { 149 if (isJavaIdentifier(name, off, (next - off)) == -1) { 150 String id = name.substring(off, next); 151 throw new IllegalArgumentException(name + ": Invalid " + what 152 + ": '" + id + "' is not a Java identifier"); 153 } 154 off = next + 1; 155 } 156 if (isJavaIdentifier(name, off, name.length() - off) == -1) { 157 String id = name.substring(off, name.length()); 158 throw new IllegalArgumentException(name + ": Invalid " + what 159 + ": '" + id + "' is not a Java identifier"); 160 } 161 return name; 162 } 163 164 /** 165 * Returns {@code true} if the last character of the given name is legal 166 * as the last character of a module name. 167 * 168 * @throws IllegalArgumentException if name is empty 169 */ 170 public static boolean hasLegalModuleNameLastCharacter(String name) { 171 if (name.isEmpty()) 172 throw new IllegalArgumentException("name is empty"); 173 int len = name.length(); 174 if (isASCIIString(name)) { 175 char c = name.charAt(len-1); 176 return Character.isJavaIdentifierStart(c); 177 } else { 178 int i = 0; 179 int cp = -1; 180 while (i < len) { 181 cp = name.codePointAt(i); 182 i += Character.charCount(cp); 183 } 184 return Character.isJavaIdentifierStart(cp); 185 } 186 } 187 188 /** 189 * Returns true if the given string only contains ASCII characters. 190 */ 191 private static boolean isASCIIString(String s) { 192 int i = 0; 193 while (i < s.length()) { 194 int c = s.charAt(i); 195 if (c > 0x7F) 196 return false; 197 i++; 198 } 199 return true; 200 } 201 202 /** 203 * Checks if a char sequence is a legal Java identifier, returning the code 204 * point of the last character if legal or {@code -1} if not legal. 205 */ 206 private static int isJavaIdentifier(CharSequence cs, int offset, int count) { 207 if (count == 0) 208 return -1; 209 int first = Character.codePointAt(cs, offset); 210 if (!Character.isJavaIdentifierStart(first)) 211 return -1; 212 213 int cp = first; 214 int i = Character.charCount(first); 215 while (i < count) { 216 cp = Character.codePointAt(cs, offset+i); 217 if (!Character.isJavaIdentifierPart(cp)) 218 return -1; 219 i += Character.charCount(cp); 220 } 221 222 return cp; 223 } 224 } |