1 22 package org.jboss.mx.util; 23 24 import java.util.HashSet ; 25 26 36 public final class MetaDataUtil 37 { 38 40 public static final String BOOLEAN_TYPE_NAME = Boolean.TYPE.getName(); 41 public static final String BYTE_TYPE_NAME = Byte.TYPE.getName(); 42 public static final String CHAR_TYPE_NAME = Character.TYPE.getName(); 43 public static final String DOUBLE_TYPE_NAME = Double.TYPE.getName(); 44 public static final String FLOAT_TYPE_NAME = Float.TYPE.getName(); 45 public static final String INT_TYPE_NAME = Integer.TYPE.getName(); 46 public static final String LONG_TYPE_NAME = Long.TYPE.getName(); 47 public static final String SHORT_TYPE_NAME = Short.TYPE.getName(); 48 public static final String VOID_TYPE_NAME = Void.TYPE.getName(); 49 50 private static final HashSet reserved = new HashSet (); 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 110 116 public static final boolean isValidJavaIdentifier(String string) 117 { 118 if (string == null || string.length() == 0) 120 return false; 121 122 final char[] chars = string.toCharArray(); 123 124 if (Character.isJavaIdentifierStart(chars[0]) == false) 126 return false; 127 128 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 return true; 140 } 141 142 148 public static final boolean isValidJavaType(String string) 149 { 150 if (string == null || string.length() == 0) 152 return false; 153 154 if (string.charAt(0) == '[') 156 { 157 String baseClassName = getBaseClassName(string); 158 if (baseClassName == null) 160 return false; 161 162 string = baseClassName; 163 } 164 165 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 if (chars[i] == '.') 177 { 178 if (i == start) 180 return false; 181 182 if (isValidJavaIdentifier(string.substring(start, i)) == false) 184 return false; 185 186 start = i+1; 187 } 188 } 189 190 if (start < chars.length && 192 isValidJavaIdentifier(string.substring(start, chars.length)) == false) 193 return false; 194 195 return true; 197 } 198 199 210 public static String getBaseClassName(String className) 211 { 212 final int length = className.length(); 213 final int last = length - 1; 214 int i = 0; 215 216 while (i < length && className.charAt(i) == '[') 218 ++i; 219 220 if (i > 0) 222 { 223 char type = className.charAt(i); 225 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 else if (className.charAt(i) != 'L' || 235 i >= last-1 || 236 className.charAt(last) != ';') 237 return null; 238 239 return className.substring(i+1, last); 241 } 242 243 return className; 245 } 246 247 253 public static boolean isPrimitive(String 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 |