1 6 7 package org.jfox.ioc.util; 8 9 import java.lang.reflect.Array ; 10 import java.lang.reflect.Constructor ; 11 import java.lang.reflect.InvocationTargetException ; 12 import java.lang.reflect.Method ; 13 import java.lang.reflect.Field ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.StringTokenizer ; 21 22 25 26 public class Classes { 27 28 public static final char INNER_CLASS_SEPARATOR_CHAR = '$'; 29 public final static Class [] EMPTY_CLASS_ARRAY = new Class [0]; 30 31 34 public static final String INNER_CLASS_SEPARATOR = String.valueOf(INNER_CLASS_SEPARATOR_CHAR); 35 36 39 private static Map primitiveClassToWrapper = new HashMap (); 40 41 static { 42 primitiveClassToWrapper.put(Boolean.TYPE, Boolean .class); 43 primitiveClassToWrapper.put(Byte.TYPE, Byte .class); 44 primitiveClassToWrapper.put(Character.TYPE, Character .class); 45 primitiveClassToWrapper.put(Short.TYPE, Short .class); 46 primitiveClassToWrapper.put(Integer.TYPE, Integer .class); 47 primitiveClassToWrapper.put(Long.TYPE, Long .class); 48 primitiveClassToWrapper.put(Double.TYPE, Double .class); 49 primitiveClassToWrapper.put(Float.TYPE, Float .class); 50 } 51 52 private static Map primitiveStringToClass = new HashMap (); 53 54 static { 55 primitiveStringToClass.put(Boolean.TYPE.getName(), Boolean.TYPE); 56 primitiveStringToClass.put(Byte.TYPE.getName(), Byte.TYPE); 57 primitiveStringToClass.put(Character.TYPE.getName(), Character.TYPE); 58 primitiveStringToClass.put(Short.TYPE.getName(), Short.TYPE); 59 primitiveStringToClass.put(Integer.TYPE.getName(), Integer.TYPE); 60 primitiveStringToClass.put(Long.TYPE.getName(), Long.TYPE); 61 primitiveStringToClass.put(Double.TYPE.getName(), Double.TYPE); 62 primitiveStringToClass.put(Float.TYPE.getName(), Float.TYPE); 63 } 64 65 73 private Classes() { 74 } 75 76 83 public static List getAllSuperclasses(Class cls) { 84 if(cls == null) { 85 return null; 86 } 87 List classes = new ArrayList (); 88 Class superclass = cls.getSuperclass(); 89 while(superclass != null) { 90 classes.add(superclass); 91 superclass = superclass.getSuperclass(); 92 } 93 return classes; 94 } 95 96 109 public static List <Class > getAllInterfaces(Class cls) { 110 if(cls == null) { 111 return null; 112 } 113 List <Class > list = new ArrayList <Class >(); 114 while(cls != null) { 115 Class [] interfaces = cls.getInterfaces(); 116 for(int i = 0; i < interfaces.length; i++) { 117 if(list.contains(interfaces[i]) == false) { 118 list.add(interfaces[i]); 119 } 120 List superInterfaces = getAllInterfaces(interfaces[i]); 121 for(Iterator it = superInterfaces.iterator(); it.hasNext();) { 122 Class intface = (Class ) it.next(); 123 if(list.contains(intface) == false) { 124 list.add(intface); 125 } 126 } 127 } 128 cls = cls.getSuperclass(); 129 } 130 return list; 131 } 132 133 134 165 public static boolean isAssignable(Class [] classArray, Class [] toClassArray) { 166 if(classArray.length != toClassArray.length) { 167 return false; 168 } 169 if(classArray == null) { 170 classArray = EMPTY_CLASS_ARRAY; 171 } 172 if(toClassArray == null) { 173 toClassArray = EMPTY_CLASS_ARRAY; 174 } 175 for(int i = 0; i < classArray.length; i++) { 176 if(isAssignable(classArray[i], toClassArray[i]) == false) { 177 return false; 178 } 179 } 180 return true; 181 } 182 183 209 public static boolean isAssignable(Class cls, Class toClass) { 210 if(toClass == null) { 211 return false; 212 } 213 if(cls == null) { 215 return !(toClass.isPrimitive()); 216 } 217 if(cls.equals(toClass)) { 218 return true; 219 } 220 if(cls.isPrimitive()) { 221 if(toClass.isPrimitive() == false) { 222 return false; 223 } 224 if(Integer.TYPE.equals(cls)) { 225 return Long.TYPE.equals(toClass) 226 || Float.TYPE.equals(toClass) 227 || Double.TYPE.equals(toClass); 228 } 229 if(Long.TYPE.equals(cls)) { 230 return Float.TYPE.equals(toClass) 231 || Double.TYPE.equals(toClass); 232 } 233 if(Boolean.TYPE.equals(cls)) { 234 return false; 235 } 236 if(Double.TYPE.equals(cls)) { 237 return false; 238 } 239 if(Float.TYPE.equals(cls)) { 240 return Double.TYPE.equals(toClass); 241 } 242 if(Character.TYPE.equals(cls)) { 243 return Integer.TYPE.equals(toClass) 244 || Long.TYPE.equals(toClass) 245 || Float.TYPE.equals(toClass) 246 || Double.TYPE.equals(toClass); 247 } 248 if(Short.TYPE.equals(cls)) { 249 return Integer.TYPE.equals(toClass) 250 || Long.TYPE.equals(toClass) 251 || Float.TYPE.equals(toClass) 252 || Double.TYPE.equals(toClass); 253 } 254 if(Byte.TYPE.equals(cls)) { 255 return Short.TYPE.equals(toClass) 256 || Integer.TYPE.equals(toClass) 257 || Long.TYPE.equals(toClass) 258 || Float.TYPE.equals(toClass) 259 || Double.TYPE.equals(toClass); 260 } 261 return false; 263 } 264 return toClass.isAssignableFrom(cls); 265 } 266 267 275 public static Class primitiveToWrapper(Class cls) { 276 Class convertedClass = cls; 277 if(cls != null && cls.isPrimitive()) { 278 convertedClass = (Class ) primitiveClassToWrapper.get(cls); 279 } 280 return convertedClass; 281 } 282 283 291 public static Object newObject(Class clz, String value) throws NoSuchMethodException , 292 InstantiationException , 293 IllegalAccessException , 294 InvocationTargetException { 295 Object obj = value; 296 if(clz.isPrimitive()) { 297 if(clz.equals(boolean.class) || clz.equals(Boolean .class)) { 298 obj = Boolean.valueOf(value); 299 } 300 else if(clz.equals(char.class) || clz.equals(Character .class)) { 301 obj = new Character (value.charAt(0)); 302 } 303 else if(clz.equals(byte.class) || clz.equals(Byte .class)) { 304 obj = Byte.valueOf(value); 305 } 306 else if(clz.equals(short.class) || clz.equals(Short .class)) { 307 obj = Short.valueOf(value); 308 } 309 else if(clz.equals(int.class) || clz.equals(Integer .class)) { 310 obj = Integer.valueOf(value); 311 } 312 else if(clz.equals(long.class) || clz.equals(Long .class)) { 313 obj = Long.valueOf(value); 314 } 315 else if(clz.equals(float.class) || clz.equals(Float .class)) { 316 obj = Float.valueOf(value); 317 } 318 else if(clz.equals(double.class) || clz.equals(Double .class)) { 319 obj = Double.valueOf(value); 320 } 321 322 } 323 else if(clz.isArray()) { 324 Class originClass = clz.getComponentType(); 325 StringTokenizer st = new StringTokenizer (value); 326 int length = st.countTokens(); 327 Object array = Array.newInstance(originClass, length); 328 for(int i = 0; st.hasMoreTokens(); i++) { 329 Object param = newObject(originClass, st.nextToken()); 330 Array.set(array, i, param); 331 } 332 obj = array; 333 } 334 else { 335 obj = clz.getConstructor(new Class []{String .class}).newInstance(new Object []{value}); 336 } 337 return obj; 338 } 339 340 347 public static boolean isInnerClass(Class cls) { 348 if(cls == null) { 349 return false; 350 } 351 return (cls.getName().indexOf(INNER_CLASS_SEPARATOR_CHAR) >= 0); 352 } 353 354 362 public static Method getMethod(Class clz, String methodName, Class [] expectedTypes) throws NoSuchMethodException { 363 Method method = null; 364 try { 365 method = clz.getMethod(methodName, expectedTypes); 366 } 367 catch(NoSuchMethodException e) { 368 Method [] methods = clz.getMethods(); 369 for(int i = 0; i < methods.length; i++) { 370 Method _method = methods[i]; 371 if(_method.getName().equals(methodName)) { 372 if(isAssignable(expectedTypes, _method.getParameterTypes())) { 373 if(method == null) { 374 method = _method; 375 } 376 else { 377 if(Classes.isAssignable(_method.getParameterTypes(), method.getParameterTypes())) { 379 method = _method; 380 } 381 } 382 383 } 384 } 385 } 386 if(method == null) throw e; 387 } 388 return method; 389 } 390 391 public static Constructor getConstructor(Class clz, Class [] expectedTypes) throws NoSuchMethodException { 392 Constructor constructor = null; 393 if(expectedTypes == null) { 394 expectedTypes = Classes.EMPTY_CLASS_ARRAY; 395 } 396 try { 397 constructor = clz.getConstructor(expectedTypes); 398 } 399 catch(NoSuchMethodException e) { 400 Constructor [] constructors = clz.getConstructors(); 401 for(int i = 0; i < constructors.length; i++) { 402 Constructor constr = constructors[i]; 403 if(Classes.isAssignable(expectedTypes, constr.getParameterTypes())) { 404 if(constructor == null) { 405 constructor = constr; 406 } 407 else { 408 if(Classes.isAssignable(constr.getParameterTypes(), constructor.getParameterTypes())) { 409 constructor = constr; 410 } 411 } 412 } 413 } 414 if(constructor == null) throw e; 415 } 416 return constructor; 417 } 418 419 427 public static Field getDecaredField(Class clz, String name) throws NoSuchFieldException { 428 Class superClass = clz; 429 Field field = null; 430 while((field == null) && !superClass.equals(Object .class)) { 431 try { 432 field = superClass.getDeclaredField(name); 433 } 434 catch(NoSuchFieldException e) { 435 436 } 437 superClass = superClass.getSuperclass(); 438 } 439 if(field == null) { 440 throw new NoSuchFieldException ("No such field " + name + " in " + clz); 441 } 442 return field; 443 } 444 445 452 public static Field [] getDecaredFields(Class clz) { 453 List <Field > fields = new ArrayList <Field >(); 454 455 Class superClass = clz; 456 while(!superClass.equals(Object .class)) { 457 fields.addAll(Arrays.asList(superClass.getDeclaredFields())); 458 superClass = superClass.getSuperclass(); 459 } 460 return fields.toArray(new Field [fields.size()]); 461 } 462 463 469 public static Class getNarrowestClass(Class [] types) { 470 if(types == null || types.length == 0) { 471 throw new IllegalArgumentException ("types is null or empty."); 472 } 473 Class clz = types[0]; 474 for(int i = 1; i < types.length; i++) { 475 if(clz.isAssignableFrom(types[i])) { 476 clz = types[i]; 477 } 478 else if(!types[i].isAssignableFrom(clz)) { 479 throw new IllegalArgumentException (types[i].getName() + " not has assignable relation with " + clz.getName()); 480 } 481 } 482 return clz; 483 } 484 485 public static boolean isPrimitiveClass(String primitiveClassName) { 486 return primitiveStringToClass.containsKey(primitiveClassName); 487 } 488 489 public static Class loadPrimitiveClass(String primitiveClassName) { 490 if(isPrimitiveClass(primitiveClassName)) { 491 return (Class ) primitiveStringToClass.get(primitiveClassName); 492 } 493 return null; 494 } 495 496 512 513 public static String classNameWithoutPackage(Class objectClass){ 514 return classNameWithoutPackage(objectClass.getName()); 515 516 } 517 518 public static String classNameWithoutPackage(String fullClassName){ 519 return fullClassName.substring(fullClassName.lastIndexOf(".")+1, fullClassName.length()); 520 } 521 522 public static List <Method > introspectMethods(Class clz){ 523 Method [] _methods = clz.getMethods(); 524 return Arrays.asList(_methods); 525 } 526 527 public static void main(String [] args) throws Exception { 528 Object array = newObject(new String []{}.getClass(), "a b cc "); 529 System.out.println(array); 530 } 531 532 } 533 534 | Popular Tags |