KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > MetaDataUtil


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.util;
23
24 import java.util.HashSet JavaDoc;
25
26 /**
27  * Utilities for handling meta data
28  *
29  * Based on Strings from common (should jbossmx use common?)
30  *
31  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
32  * @author <a HREF="Scott.Stark@jboss.org">Scott Stark</a>
33  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
34  * @version $Revision: 37459 $
35  */

36 public final class MetaDataUtil
37 {
38    // Constants -----------------------------------------------------
39

40    public static final String JavaDoc BOOLEAN_TYPE_NAME = Boolean.TYPE.getName();
41    public static final String JavaDoc BYTE_TYPE_NAME = Byte.TYPE.getName();
42    public static final String JavaDoc CHAR_TYPE_NAME = Character.TYPE.getName();
43    public static final String JavaDoc DOUBLE_TYPE_NAME = Double.TYPE.getName();
44    public static final String JavaDoc FLOAT_TYPE_NAME = Float.TYPE.getName();
45    public static final String JavaDoc INT_TYPE_NAME = Integer.TYPE.getName();
46    public static final String JavaDoc LONG_TYPE_NAME = Long.TYPE.getName();
47    public static final String JavaDoc SHORT_TYPE_NAME = Short.TYPE.getName();
48    public static final String JavaDoc VOID_TYPE_NAME = Void.TYPE.getName();
49
50    private static final HashSet JavaDoc reserved = new HashSet JavaDoc();
51
52    static
53    {
54       reserved.add("assert");
55       reserved.add("abstract");
56       reserved.add("boolean");
57       reserved.add("break");
58       reserved.add("byte");
59       reserved.add("case");
60       reserved.add("catch");
61       reserved.add("char");
62       reserved.add("class");
63       reserved.add("const");
64       reserved.add("continue");
65       reserved.add("default");
66       reserved.add("do");
67       reserved.add("double");
68       reserved.add("else");
69       reserved.add("extends");
70       reserved.add("false");
71       reserved.add("final");
72       reserved.add("finally");
73       reserved.add("float");
74       reserved.add("for");
75       reserved.add("goto");
76       reserved.add("if");
77       reserved.add("implements");
78       reserved.add("import");
79       reserved.add("instanceof");
80       reserved.add("int");
81       reserved.add("interface");
82       reserved.add("long");
83       reserved.add("native");
84       reserved.add("new");
85       reserved.add("null");
86       reserved.add("package");
87       reserved.add("private");
88       reserved.add("protected");
89       reserved.add("public");
90       reserved.add("return");
91       reserved.add("short");
92       reserved.add("static");
93       reserved.add("strictfp");
94       reserved.add("super");
95       reserved.add("switch");
96       reserved.add("synchronized");
97       reserved.add("this");
98       reserved.add("throw");
99       reserved.add("throws");
100       reserved.add("transient");
101       reserved.add("true");
102       reserved.add("try");
103       reserved.add("void");
104       reserved.add("volatile");
105       reserved.add("while");
106    }
107
108    // Static --------------------------------------------------------
109

110    /**
111     * Tests whether the passed string is a valid java identifier
112     *
113     * @param string the string to test
114     * @return true when it is valid
115     */

116    public static final boolean isValidJavaIdentifier(String JavaDoc string)
117    {
118       // Null or empty
119
if (string == null || string.length() == 0)
120          return false;
121
122       final char[] chars = string.toCharArray();
123
124       // Invalid start character
125
if (Character.isJavaIdentifierStart(chars[0]) == false)
126          return false;
127
128       // Invalid part character
129
for (int i = 1; i < chars.length; ++i)
130       {
131          if (Character.isJavaIdentifierPart(chars[i]) == false)
132             return false;
133       }
134
135         if (reserved.contains(string))
136            return false;
137
138       // Yippee!
139
return true;
140    }
141
142    /**
143     * Tests whether the passed string is a valid java type
144     *
145     * @param string the string to test
146     * @return true when it is valid
147     */

148    public static final boolean isValidJavaType(String JavaDoc string)
149    {
150       // Null or empty
151
if (string == null || string.length() == 0)
152          return false;
153
154       // Looks like an array
155
if (string.charAt(0) == '[')
156       {
157          String JavaDoc baseClassName = getBaseClassName(string);
158          // But it is not valid
159
if (baseClassName == null)
160             return false;
161
162          string = baseClassName;
163       }
164
165       // Check for a primitive
166
if (isPrimitive(string))
167          return true;
168
169       final char[] chars = string.toCharArray();
170
171       int start = 0;
172
173       for (int i = 0; i < chars.length; ++i)
174       {
175          // Found a dot
176
if (chars[i] == '.')
177          {
178             // But it as the start or straight after a previous dot
179
if (i == start)
180                return false;
181
182             // Is what is before the dot a valid identifier?
183
if (isValidJavaIdentifier(string.substring(start, i)) == false)
184                return false;
185
186             start = i+1;
187          }
188       }
189
190       // Check the trailing characters
191
if (start < chars.length &&
192           isValidJavaIdentifier(string.substring(start, chars.length)) == false)
193          return false;
194
195       // Yippee!
196
return true;
197    }
198
199    /**
200     * Gets the base class name, either the passed class name
201     * or the underlying class name if it is an array.<p>
202     *
203     * NOTE: The class is not check for validity.<p>
204     *
205     * Null is returned when the array declaration is invalid.
206     *
207     * @param string the string to test
208     * @return the underlying class name or null
209     */

210    public static String JavaDoc getBaseClassName(String JavaDoc className)
211    {
212       final int length = className.length();
213       final int last = length - 1;
214       int i = 0;
215
216       // Eat the array dimensions
217
while (i < length && className.charAt(i) == '[')
218          ++i;
219
220       // It looks like an array
221
if (i > 0)
222       {
223          // But is it valid
224
char type = className.charAt(i);
225          // Primitive array
226
if (type == 'B' || type == 'C' || type == 'D' || type == 'F' ||
227              type == 'I' || type == 'J' || type == 'S' || type == 'Z' || type == 'V')
228          {
229             if (i != last)
230                return null;
231             return className.substring(last, length);
232          }
233          // Object Array
234
else if (className.charAt(i) != 'L' ||
235              i >= last-1 ||
236              className.charAt(last) != ';')
237             return null;
238
239          // Potentially valid array, class name might be rubbish
240
return className.substring(i+1, last);
241       }
242
243       // Not an array
244
return className;
245    }
246
247    /**
248     * Checks whether a string is primitive
249     *
250     * @param string the string to test
251     * @return true if it is primitive
252     */

253    public static boolean isPrimitive(String JavaDoc string)
254    {
255       if (string.equals(INT_TYPE_NAME))
256          return true;
257       if (string.equals(LONG_TYPE_NAME))
258          return true;
259       if (string.equals(BOOLEAN_TYPE_NAME))
260          return true;
261       if (string.equals(BYTE_TYPE_NAME))
262          return true;
263       if (string.equals(CHAR_TYPE_NAME))
264          return true;
265       if (string.equals(SHORT_TYPE_NAME))
266          return true;
267       if (string.equals(FLOAT_TYPE_NAME))
268          return true;
269       if (string.equals(DOUBLE_TYPE_NAME))
270          return true;
271       if (string.equals(VOID_TYPE_NAME))
272          return true;
273       return false;
274    }
275 }
Popular Tags