46 throw new IllegalArgumentException("Null module name");
47 int next;
48 int off = 0;
49 while ((next = name.indexOf('.', off)) != -1) {
50 String id = name.substring(off, next);
51 if (!isJavaIdentifier(id)) {
52 throw new IllegalArgumentException(name + ": Invalid module name"
53 + ": '" + id + "' is not a Java identifier");
54 }
55 off = next+1;
56 }
57 String last = name.substring(off);
58 if (!isJavaIdentifier(last)) {
59 throw new IllegalArgumentException(name + ": Invalid module name"
60 + ": '" + last + "' is not a Java identifier");
61 }
62 return name;
63 }
64
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.
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
164 throw new IllegalArgumentException("Null " + what);
165 int next;
166 int off = 0;
167 while ((next = name.indexOf('.', off)) != -1) {
168 String id = name.substring(off, next);
169 if (!isJavaIdentifier(id)) {
170 throw new IllegalArgumentException(name + ": Invalid " + what
171 + ": '" + id + "' is not a Java identifier");
172 }
173 off = next + 1;
174 }
175 String last = name.substring(off);
176 if (!isJavaIdentifier(last)) {
177 throw new IllegalArgumentException(name + ": Invalid " + what
178 + ": '" + last + "' is not a Java identifier");
179 }
180 return name;
181 }
182
183 /**
184 * Returns true if the given char sequence is a legal Java identifier,
185 * otherwise false.
186 */
187 private static boolean isJavaIdentifier(CharSequence cs) {
188 if (cs.length() == 0 || RESERVED.contains(cs))
189 return false;
190
191 int first = Character.codePointAt(cs, 0);
192 if (!Character.isJavaIdentifierStart(first))
193 return false;
194
195 int i = Character.charCount(first);
196 while (i < cs.length()) {
197 int cp = Character.codePointAt(cs, i);
198 if (!Character.isJavaIdentifierPart(cp))
199 return false;
200 i += Character.charCount(cp);
201 }
202
203 return true;
204 }
205
206 // keywords, boolean and null literals, not allowed in identifiers
207 private static final Set<String> RESERVED = Set.of(
208 "abstract",
209 "assert",
210 "boolean",
211 "break",
212 "byte",
213 "case",
214 "catch",
215 "char",
216 "class",
217 "const",
|
46 throw new IllegalArgumentException("Null module name");
47 int next;
48 int off = 0;
49 while ((next = name.indexOf('.', off)) != -1) {
50 String id = name.substring(off, next);
51 if (!isJavaIdentifier(id)) {
52 throw new IllegalArgumentException(name + ": Invalid module name"
53 + ": '" + id + "' is not a Java identifier");
54 }
55 off = next+1;
56 }
57 String last = name.substring(off);
58 if (!isJavaIdentifier(last)) {
59 throw new IllegalArgumentException(name + ": Invalid module name"
60 + ": '" + last + "' is not a Java identifier");
61 }
62 return name;
63 }
64
65 /**
66 * Checks a name to ensure that it's a legal package name.
67 *
68 * @throws IllegalArgumentException if name is null or not a legal
69 * package name
70 */
71 public static String requirePackageName(String name) {
72 return requireTypeName("package name", name);
73 }
74
75 /**
76 * Returns {@code true} if the given name is a legal package name.
77 */
78 public static boolean isPackageName(String name) {
79 return isTypeName(name);
80 }
81
82 /**
83 * Checks a name to ensure that it's a legal qualified class name
84 *
85 * @throws IllegalArgumentException if name is null or not a legal
148 throw new IllegalArgumentException("Null " + what);
149 int next;
150 int off = 0;
151 while ((next = name.indexOf('.', off)) != -1) {
152 String id = name.substring(off, next);
153 if (!isJavaIdentifier(id)) {
154 throw new IllegalArgumentException(name + ": Invalid " + what
155 + ": '" + id + "' is not a Java identifier");
156 }
157 off = next + 1;
158 }
159 String last = name.substring(off);
160 if (!isJavaIdentifier(last)) {
161 throw new IllegalArgumentException(name + ": Invalid " + what
162 + ": '" + last + "' is not a Java identifier");
163 }
164 return name;
165 }
166
167 /**
168 * Returns true if the given string is a legal Java identifier,
169 * otherwise false.
170 */
171 private static boolean isJavaIdentifier(String str) {
172 if (str.isEmpty() || RESERVED.contains(str))
173 return false;
174
175 int first = Character.codePointAt(str, 0);
176 if (!Character.isJavaIdentifierStart(first))
177 return false;
178
179 int i = Character.charCount(first);
180 while (i < str.length()) {
181 int cp = Character.codePointAt(str, i);
182 if (!Character.isJavaIdentifierPart(cp))
183 return false;
184 i += Character.charCount(cp);
185 }
186
187 return true;
188 }
189
190 // keywords, boolean and null literals, not allowed in identifiers
191 private static final Set<String> RESERVED = Set.of(
192 "abstract",
193 "assert",
194 "boolean",
195 "break",
196 "byte",
197 "case",
198 "catch",
199 "char",
200 "class",
201 "const",
|