1 16 package org.apache.commons.lang; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 36 public class ClassUtils { 37 38 41 public static final char PACKAGE_SEPARATOR_CHAR = '.'; 42 43 46 public static final String PACKAGE_SEPARATOR = String.valueOf(PACKAGE_SEPARATOR_CHAR); 47 48 51 public static final char INNER_CLASS_SEPARATOR_CHAR = '$'; 52 53 56 public static final String INNER_CLASS_SEPARATOR = String.valueOf(INNER_CLASS_SEPARATOR_CHAR); 57 58 61 private static Map primitiveWrapperMap = new HashMap (); 62 static { 63 primitiveWrapperMap.put(Boolean.TYPE, Boolean .class); 64 primitiveWrapperMap.put(Byte.TYPE, Byte .class); 65 primitiveWrapperMap.put(Character.TYPE, Character .class); 66 primitiveWrapperMap.put(Short.TYPE, Short .class); 67 primitiveWrapperMap.put(Integer.TYPE, Integer .class); 68 primitiveWrapperMap.put(Long.TYPE, Long .class); 69 primitiveWrapperMap.put(Double.TYPE, Double .class); 70 primitiveWrapperMap.put(Float.TYPE, Float .class); 71 } 72 73 81 public ClassUtils() { 82 } 83 84 93 public static String getShortClassName(Object object, String valueIfNull) { 94 if (object == null) { 95 return valueIfNull; 96 } 97 return getShortClassName(object.getClass().getName()); 98 } 99 100 106 public static String getShortClassName(Class cls) { 107 if (cls == null) { 108 return StringUtils.EMPTY; 109 } 110 return getShortClassName(cls.getName()); 111 } 112 113 121 public static String getShortClassName(String className) { 122 if (className == null) { 123 return StringUtils.EMPTY; 124 } 125 if (className.length() == 0) { 126 return StringUtils.EMPTY; 127 } 128 char[] chars = className.toCharArray(); 129 int lastDot = 0; 130 for (int i = 0; i < chars.length; i++) { 131 if (chars[i] == PACKAGE_SEPARATOR_CHAR) { 132 lastDot = i + 1; 133 } else if (chars[i] == INNER_CLASS_SEPARATOR_CHAR) { chars[i] = PACKAGE_SEPARATOR_CHAR; 135 } 136 } 137 return new String (chars, lastDot, chars.length - lastDot); 138 } 139 140 149 public static String getPackageName(Object object, String valueIfNull) { 150 if (object == null) { 151 return valueIfNull; 152 } 153 return getPackageName(object.getClass().getName()); 154 } 155 156 162 public static String getPackageName(Class cls) { 163 if (cls == null) { 164 return StringUtils.EMPTY; 165 } 166 return getPackageName(cls.getName()); 167 } 168 169 178 public static String getPackageName(String className) { 179 if (className == null) { 180 return StringUtils.EMPTY; 181 } 182 int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR); 183 if (i == -1) { 184 return StringUtils.EMPTY; 185 } 186 return className.substring(0, i); 187 } 188 189 198 public static List getAllSuperclasses(Class cls) { 199 if (cls == null) { 200 return null; 201 } 202 List classes = new ArrayList (); 203 Class superclass = cls.getSuperclass(); 204 while (superclass != null) { 205 classes.add(superclass); 206 superclass = superclass.getSuperclass(); 207 } 208 return classes; 209 } 210 211 224 public static List getAllInterfaces(Class cls) { 225 if (cls == null) { 226 return null; 227 } 228 List list = new ArrayList (); 229 while (cls != null) { 230 Class [] interfaces = cls.getInterfaces(); 231 for (int i = 0; i < interfaces.length; i++) { 232 if (list.contains(interfaces[i]) == false) { 233 list.add(interfaces[i]); 234 } 235 List superInterfaces = getAllInterfaces(interfaces[i]); 236 for (Iterator it = superInterfaces.iterator(); it.hasNext();) { 237 Class intface = (Class ) it.next(); 238 if (list.contains(intface) == false) { 239 list.add(intface); 240 } 241 } 242 } 243 cls = cls.getSuperclass(); 244 } 245 return list; 246 } 247 248 262 public static List convertClassNamesToClasses(List classNames) { 263 if (classNames == null) { 264 return null; 265 } 266 List classes = new ArrayList (classNames.size()); 267 for (Iterator it = classNames.iterator(); it.hasNext();) { 268 String className = (String ) it.next(); 269 try { 270 classes.add(Class.forName(className)); 271 } catch (Exception ex) { 272 classes.add(null); 273 } 274 } 275 return classes; 276 } 277 278 290 public static List convertClassesToClassNames(List classes) { 291 if (classes == null) { 292 return null; 293 } 294 List classNames = new ArrayList (classes.size()); 295 for (Iterator it = classes.iterator(); it.hasNext();) { 296 Class cls = (Class ) it.next(); 297 if (cls == null) { 298 classNames.add(null); 299 } else { 300 classNames.add(cls.getName()); 301 } 302 } 303 return classNames; 304 } 305 306 339 public static boolean isAssignable(Class [] classArray, Class [] toClassArray) { 340 if (ArrayUtils.isSameLength(classArray, toClassArray) == false) { 341 return false; 342 } 343 if (classArray == null) { 344 classArray = ArrayUtils.EMPTY_CLASS_ARRAY; 345 } 346 if (toClassArray == null) { 347 toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY; 348 } 349 for (int i = 0; i < classArray.length; i++) { 350 if (isAssignable(classArray[i], toClassArray[i]) == false) { 351 return false; 352 } 353 } 354 return true; 355 } 356 357 383 public static boolean isAssignable(Class cls, Class toClass) { 384 if (toClass == null) { 385 return false; 386 } 387 if (cls == null) { 389 return !(toClass.isPrimitive()); 390 } 391 if (cls.equals(toClass)) { 392 return true; 393 } 394 if (cls.isPrimitive()) { 395 if (toClass.isPrimitive() == false) { 396 return false; 397 } 398 if (Integer.TYPE.equals(cls)) { 399 return Long.TYPE.equals(toClass) 400 || Float.TYPE.equals(toClass) 401 || Double.TYPE.equals(toClass); 402 } 403 if (Long.TYPE.equals(cls)) { 404 return Float.TYPE.equals(toClass) 405 || Double.TYPE.equals(toClass); 406 } 407 if (Boolean.TYPE.equals(cls)) { 408 return false; 409 } 410 if (Double.TYPE.equals(cls)) { 411 return false; 412 } 413 if (Float.TYPE.equals(cls)) { 414 return Double.TYPE.equals(toClass); 415 } 416 if (Character.TYPE.equals(cls)) { 417 return Integer.TYPE.equals(toClass) 418 || Long.TYPE.equals(toClass) 419 || Float.TYPE.equals(toClass) 420 || Double.TYPE.equals(toClass); 421 } 422 if (Short.TYPE.equals(cls)) { 423 return Integer.TYPE.equals(toClass) 424 || Long.TYPE.equals(toClass) 425 || Float.TYPE.equals(toClass) 426 || Double.TYPE.equals(toClass); 427 } 428 if (Byte.TYPE.equals(cls)) { 429 return Short.TYPE.equals(toClass) 430 || Integer.TYPE.equals(toClass) 431 || Long.TYPE.equals(toClass) 432 || Float.TYPE.equals(toClass) 433 || Double.TYPE.equals(toClass); 434 } 435 return false; 437 } 438 return toClass.isAssignableFrom(cls); 439 } 440 441 450 public static Class primitiveToWrapper(Class cls) { 451 Class convertedClass = cls; 452 if (cls != null && cls.isPrimitive()) { 453 convertedClass = (Class ) primitiveWrapperMap.get(cls); 454 } 455 return convertedClass; 456 } 457 458 468 public static Class [] primitivesToWrappers(Class [] classes) { 469 if (classes == null) { 470 return null; 471 } 472 473 if (classes.length == 0) { 474 return classes; 475 } 476 477 Class [] convertedClasses = new Class [classes.length]; 478 for (int i=0; i < classes.length; i++) { 479 convertedClasses[i] = primitiveToWrapper( classes[i] ); 480 } 481 return convertedClasses; 482 } 483 484 493 public static boolean isInnerClass(Class cls) { 494 if (cls == null) { 495 return false; 496 } 497 return cls.getName().indexOf(INNER_CLASS_SEPARATOR_CHAR) >= 0; 498 } 499 500 } 501 | Popular Tags |