1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.exolab.javasource;
44
45
46
47
48
49
50
51 public final class JNaming {
52
53
54
55
56
57 private static final String[] KEYWORDS = {
58 "abstract",
59 "boolean",
60 "break",
61 "byte",
62 "case",
63 "catch",
64 "char",
65 "class",
66 "const",
67 "continue",
68 "default",
69 "do",
70 "double",
71 "else",
72 "enum",
73 "extends",
74 "false",
75 "final",
76 "finally",
77 "float",
78 "for",
79 "goto",
80 "if",
81 "implements",
82 "import",
83 "instanceof",
84 "int",
85 "interface",
86 "long",
87 "native",
88 "new",
89 "null",
90 "package",
91 "private",
92 "protected",
93 "public",
94 "return",
95 "short",
96 "static",
97 "super",
98 "switch",
99 "synchronized",
100 "this",
101 "throw",
102 "throws",
103 "transient",
104 "true",
105 "try",
106 "void",
107 "volatile",
108 "while"
109 };
110
111
112
113
114 private static final String[] COLLECTIONS = {
115 "ArrayList",
116 "List",
117 "Set",
118 "Collection",
119 "Vector",
120 "Hashtable",
121 "Map",
122 "HashMap",
123 "HashSet",
124 "TreeSet",
125 "Enumeration",
126 "Iterator",
127 "ListIterator",
128 "SortedSet",
129 "SortedMap",
130 "Queue",
131 "EnumSet",
132 "EnumMap",
133 "IdentityHashMap",
134 "LinkedHashMap",
135 "LinkedHashSet",
136 "LinkedList",
137 "Stack",
138 "TreeMap",
139 "WeakHashMap"
140 };
141
142
143
144
145 private static final String[] JAVA_LANG = {
146
147 "Appendable",
148 "CharSequence",
149 "Cloneable",
150 "Comparable",
151 "Iterable",
152 "Readable",
153 "Runnable",
154
155 "Boolean",
156 "Byte",
157 "Character",
158 "Class",
159 "ClassLoader",
160 "Compiler",
161 "Double",
162 "Enum",
163 "Float",
164 "InheritableThreadLocal",
165 "Integer",
166 "Long",
167 "Math",
168 "Number",
169 "Object",
170 "Package",
171 "Process",
172 "ProcessBuilder",
173 "Runtime",
174 "RuntimePermission",
175 "SecurityManager",
176 "Short",
177 "StackTraceElement",
178 "StrictMath",
179 "String",
180 "StringBuffer",
181 "StringBuilder",
182 "System",
183 "Thread",
184 "ThreadGroup",
185 "ThreadLocal",
186 "Throwable",
187 "Void",
188
189 "ArithmeticException",
190 "ArrayIndexOutOfBoundsException",
191 "ArrayStoreException",
192 "ClassCastException",
193 "ClassNotFoundException",
194 "CloneNotSupportedException",
195 "EnumConstantNotPresentException",
196 "Exception",
197 "IllegalAccessException",
198 "IllegalArgumentException",
199 "IllegalMonitorStateException",
200 "IllegalStateException",
201 "IllegalThreadStateException",
202 "IndexOutOfBoundsException",
203 "InstantiationException",
204 "InterruptedException",
205 "NegativeArraySizeException",
206 "NoSuchFieldException",
207 "NoSuchMethodException",
208 "NullPointerException",
209 "NumberFormatException",
210 "RuntimeException",
211 "SecurityException",
212 "StringIndexOutOfBoundsException",
213 "TypeNotPresentException",
214 "UnsupportedOperationException",
215
216 "AbstractMethodError",
217 "AssertionError",
218 "ClassCircularityError",
219 "ClassFormatError",
220 "Error",
221 "ExceptionInInitializerError",
222 "IllegalAccessError",
223 "IncompatibleClassChangeError",
224 "InstantiationError",
225 "InternalError",
226 "LinkageError",
227 "NoClassDefFoundError",
228 "NoSuchFieldError",
229 "NoSuchMethodError",
230 "OutOfMemoryError",
231 "StackOverflowError",
232 "ThreadDeath",
233 "UnknownError",
234 "UnsatisfiedLinkError",
235 "UnsupportedClassVersionError",
236 "VerifyError",
237 "VirtualMachineError",
238
239 "Deprecated",
240 "Override",
241 "SuppressWarnings"
242 };
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 private static final String[] CASTOR_RESERVED = {
258 "Content",
259 "MenuItem",
260 };
261
262
263
264
265
266
267
268
269
270 private static final String[] WINDOWS_RESERVED = {
271 "CON",
272 "PRN",
273 "AUX",
274 "NUL",
275 "COM1",
276 "COM2",
277 "COM3",
278 "COM4",
279 "COM5",
280 "COM6",
281 "COM7",
282 "COM8",
283 "COM9",
284 "LPT1",
285 "LPT2",
286 "LPT3",
287 "LPT4",
288 "LPT5",
289 "LPT6",
290 "LPT7",
291 "LPT8",
292 "LPT9",
293 };
294
295
296
297
298
299
300 private JNaming() {
301 super();
302 }
303
304
305
306
307
308
309
310
311
312
313
314 public static boolean isKeyword(final String name) {
315 if (name == null) { return false; }
316 for (int i = 0; i < KEYWORDS.length; i++) {
317 if (KEYWORDS[i].equals(name)) { return true; }
318 }
319 return false;
320 }
321
322
323
324
325
326
327
328
329
330
331 public static boolean isParameterizedCollectionsObject(final String name) {
332 if (name == null) { return false; }
333 for (int i = 0; i < COLLECTIONS.length; i++) {
334 if (name.indexOf(COLLECTIONS[i]) != -1) { return true; }
335 }
336 return false;
337 }
338
339
340
341
342
343
344
345
346
347 public static boolean isInJavaLang(final String name) {
348 if (name == null) { return false; }
349 for (int i = 0; i < JAVA_LANG.length; i++) {
350 if (JAVA_LANG[i].equals(name)) {
351 return true;
352 }
353 }
354 return false;
355 }
356
357
358
359
360
361
362
363
364
365
366 public static boolean isReservedByCastor(final String name) {
367 if (name == null) { return false; }
368 for (int i = 0; i < CASTOR_RESERVED.length; i++) {
369 if (CASTOR_RESERVED[i].equals(name)) {
370 return true;
371 }
372 }
373 return false;
374 }
375
376
377
378
379
380
381
382
383
384
385 public static boolean isReservedByWindows(final String name) {
386 if (name == null) { return false; }
387 for (int i = 0; i < WINDOWS_RESERVED.length; i++) {
388 if (WINDOWS_RESERVED[i].equalsIgnoreCase(name)) {
389 return true;
390 }
391 }
392 return false;
393 }
394
395
396
397
398
399
400
401
402
403 public static boolean isValidJavaIdentifier(final String string) {
404 if ((string == null) || (string.length() == 0)) { return false; }
405
406 char[] chars = string.toCharArray();
407
408 if (isParameterizedCollectionsObject(string)) {
409 return true;
410 }
411
412
413 if (!Character.isJavaIdentifierStart(chars[0])) { return false; }
414
415 for (int i = 1; i < chars.length; i++) {
416 if (!Character.isJavaIdentifierPart(chars[i])) { return false; }
417 }
418 if (isKeyword(string)) { return false; }
419 return true;
420 }
421
422
423
424
425
426
427
428 public static String getPackageFromClassName(final String className) {
429 if (className == null) {
430 return null;
431 }
432 int idx = className.lastIndexOf('.');
433 if (idx > 0) {
434 return className.substring(0, idx);
435 }
436 return null;
437 }
438
439
440
441
442
443
444
445 public static String getLocalNameFromClassName(final String className) {
446 if (className == null) {
447 return null;
448 }
449 int idx = className.lastIndexOf('.');
450 if (idx > 0) {
451 return className.substring(idx + 1);
452 }
453 return className;
454 }
455
456
457 }