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 package org.exolab.javasource;
34
35
36
37
38
39
40
41 public final class JNaming {
42
43
44
45
46
47 private static final String[] KEYWORDS = {"abstract", "boolean", "break", "byte", "case", "catch",
48 "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends",
49 "false", "final", "finally", "float", "for", "goto", "if", "implements", "import",
50 "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private",
51 "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this",
52 "throw", "throws", "transient", "true", "try", "void", "volatile", "while"};
53
54
55
56
57 private static final String[] COLLECTIONS = {"ArrayList", "List", "Set", "Collection", "Vector",
58 "Hashtable", "Map", "HashMap", "HashSet", "TreeSet", "Enumeration", "Iterator",
59 "ListIterator", "SortedSet", "SortedMap", "Queue", "EnumSet", "EnumMap", "IdentityHashMap",
60 "LinkedHashMap", "LinkedHashSet", "LinkedList", "Stack", "TreeMap", "WeakHashMap"};
61
62
63
64
65 private static final String[] JAVA_LANG = {
66
67 "Appendable", "CharSequence", "Cloneable", "Comparable", "Iterable", "Readable", "Runnable",
68
69 "Boolean", "Byte", "Character", "Class", "ClassLoader", "Compiler", "Double", "Enum", "Float",
70 "InheritableThreadLocal", "Integer", "Long", "Math", "Number", "Object", "Package", "Process",
71 "ProcessBuilder", "Runtime", "RuntimePermission", "SecurityManager", "Short",
72 "StackTraceElement", "StrictMath", "String", "StringBuffer", "StringBuilder", "System",
73 "Thread", "ThreadGroup", "ThreadLocal", "Throwable", "Void",
74
75 "ArithmeticException", "ArrayIndexOutOfBoundsException", "ArrayStoreException",
76 "ClassCastException", "ClassNotFoundException", "CloneNotSupportedException",
77 "EnumConstantNotPresentException", "Exception", "IllegalAccessException",
78 "IllegalArgumentException", "IllegalMonitorStateException", "IllegalStateException",
79 "IllegalThreadStateException", "IndexOutOfBoundsException", "InstantiationException",
80 "InterruptedException", "NegativeArraySizeException", "NoSuchFieldException",
81 "NoSuchMethodException", "NullPointerException", "NumberFormatException", "RuntimeException",
82 "SecurityException", "StringIndexOutOfBoundsException", "TypeNotPresentException",
83 "UnsupportedOperationException",
84
85 "AbstractMethodError", "AssertionError", "ClassCircularityError", "ClassFormatError", "Error",
86 "ExceptionInInitializerError", "IllegalAccessError", "IncompatibleClassChangeError",
87 "InstantiationError", "InternalError", "LinkageError", "NoClassDefFoundError",
88 "NoSuchFieldError", "NoSuchMethodError", "OutOfMemoryError", "StackOverflowError",
89 "ThreadDeath", "UnknownError", "UnsatisfiedLinkError", "UnsupportedClassVersionError",
90 "VerifyError", "VirtualMachineError",
91
92 "Deprecated", "Override", "SuppressWarnings"};
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 private static final String[] CASTOR_RESERVED = {"Content", "MenuItem",};
108
109
110
111
112
113
114
115
116
117
118 private static final String[] WINDOWS_RESERVED =
119 {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8",
120 "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",};
121
122
123
124
125
126
127 private JNaming() {
128 super();
129 }
130
131
132
133
134
135
136
137
138
139
140
141 public static boolean isKeyword(final String name) {
142 if (name == null) {
143 return false;
144 }
145 for (String keyword : KEYWORDS) {
146 if (keyword.equals(name)) {
147 return true;
148 }
149 }
150 return false;
151 }
152
153
154
155
156
157
158
159
160
161 public static boolean isParameterizedCollectionsObject(final String name) {
162 if (name == null) {
163 return false;
164 }
165 for (String collection : COLLECTIONS) {
166 if (name.indexOf(collection) != -1) {
167 return true;
168 }
169 }
170 return false;
171 }
172
173
174
175
176
177
178
179
180
181 public static boolean isInJavaLang(final String name) {
182 if (name == null) {
183 return false;
184 }
185 for (String javaLang : JAVA_LANG) {
186 if (javaLang.equals(name)) {
187 return true;
188 }
189 }
190 return false;
191 }
192
193
194
195
196
197
198
199
200
201
202 public static boolean isReservedByCastor(final String name) {
203 if (name == null) {
204 return false;
205 }
206 for (String reserved : CASTOR_RESERVED) {
207 if (reserved.equals(name)) {
208 return true;
209 }
210 }
211 return false;
212 }
213
214
215
216
217
218
219
220
221
222 public static boolean isReservedByWindows(final String name) {
223 if (name == null) {
224 return false;
225 }
226 for (String windowsReserved : WINDOWS_RESERVED) {
227 if (windowsReserved.equalsIgnoreCase(name)) {
228 return true;
229 }
230 }
231 return false;
232 }
233
234
235
236
237
238
239
240 public static boolean isValidJavaIdentifier(final String string) {
241 if ((string == null) || (string.length() == 0)) {
242 return false;
243 }
244
245 char[] chars = string.toCharArray();
246
247 if (isParameterizedCollectionsObject(string)) {
248 return true;
249 }
250
251
252 if (!Character.isJavaIdentifierStart(chars[0])) {
253 return false;
254 }
255
256 for (char ch : chars) {
257 if (!Character.isJavaIdentifierPart(ch)) {
258 return false;
259 }
260 }
261 if (isKeyword(string)) {
262 return false;
263 }
264 return true;
265 }
266
267
268
269
270
271
272
273 public static String getPackageFromClassName(final String className) {
274 if (className == null) {
275 return null;
276 }
277 int idx = className.lastIndexOf('.');
278 if (idx > 0) {
279 return className.substring(0, idx);
280 }
281 return null;
282 }
283
284
285
286
287
288
289
290 public static String getLocalNameFromClassName(final String className) {
291 if (className == null) {
292 return null;
293 }
294 int idx = className.lastIndexOf('.');
295 if (idx > 0) {
296 return className.substring(idx + 1);
297 }
298 return className;
299 }
300
301
302 }