1 /* 2 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 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 | 1 /* 2 * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 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, package, and class 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 return name; 62 } 63 64 /** 65 * Returns {@code true} if the given name is a legal module name. 66 */ 67 public static boolean isModuleName(String name) { 68 int next; 69 int off = 0; 70 while ((next = name.indexOf('.', off)) != -1) { 71 if (isJavaIdentifier(name, off, (next - off)) == -1) 72 return false; 73 off = next+1; 74 } 75 int last = isJavaIdentifier(name, off, name.length() - off); 76 if (last == -1) 77 return false; 78 return true; 79 } 80 81 /** 82 * Checks a name to ensure that it's a legal package name. 83 * 84 * @throws IllegalArgumentException if name is null or not a legal 85 * package name 86 */ 87 public static String requirePackageName(String name) { 88 return requireTypeName("package name", name); 89 } 90 91 /** 92 * Returns {@code true} if the given name is a legal package name. 93 */ 94 public static boolean isPackageName(String name) { 95 return isTypeName(name); 96 } 97 98 /** 99 * Checks a name to ensure that it's a legal qualified class name 100 * 101 * @throws IllegalArgumentException if name is null or not a legal 102 * qualified class name 103 */ 104 public static String requireServiceTypeName(String name) { 105 return requireQualifiedClassName("service type name", name); 106 } 107 108 /** 109 * Checks a name to ensure that it's a legal qualified class name. 110 * 111 * @throws IllegalArgumentException if name is null or not a legal 112 * qualified class name 113 */ 114 public static String requireServiceProviderName(String name) { 115 return requireQualifiedClassName("service provider name", name); 116 } 117 118 /** 119 * Checks a name to ensure that it's a legal qualified class name in 120 * a named package. 121 * 122 * @throws IllegalArgumentException if name is null or not a legal 123 * qualified class name in a named package 124 */ 125 public static String requireQualifiedClassName(String what, String name) { 126 requireTypeName(what, name); 127 if (name.indexOf('.') == -1) 128 throw new IllegalArgumentException(name + ": is not a qualified name of" 129 + " a Java class in a named package"); 130 return name; 131 } 132 133 /** 134 * Returns {@code true} if the given name is a legal class name. 135 */ 136 public static boolean isClassName(String name) { 137 return isTypeName(name); 138 } 139 140 /** 141 * Returns {@code true} if the given name is a legal type name. 142 */ 143 private static boolean isTypeName(String name) { 144 int next; 145 int off = 0; 146 while ((next = name.indexOf('.', off)) != -1) { 147 if (isJavaIdentifier(name, off, (next - off)) == -1) 148 return false; 149 off = next+1; 150 } 151 int count = name.length() - off; 152 return (isJavaIdentifier(name, off, count) != -1); 153 } 154 155 /** 156 * Checks if the given name is a legal type name. 157 * 158 * @throws IllegalArgumentException if name is null or not a legal 159 * type name 160 */ 161 private static String requireTypeName(String what, String name) { 162 if (name == null) 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 |