public static Object castToType(Object object, Class type) { if (object == null) return null; if (type == Object.class) return object; final Class aClass = object.getClass(); if (type == aClass) return object; if (type.isAssignableFrom(aClass)) return object; if (ReflectionCache.isArray(type)) return asArray(object, type); if (type.isEnum()) { return ShortTypeHandling.castToEnum(object, type); } else if (Collection.class.isAssignableFrom(type)) { return continueCastOnCollection(object, type); } else if (type == String.class) { return InvokerHelper.toString(object); } else if (type == Character.class) { return ShortTypeHandling.castToChar(object); } else if (type == Boolean.class) { return castToBoolean(object); } else if (type == Class.class) { return ShortTypeHandling.castToClass(object); } else if (type.isPrimitive()) { return castToPrimitive(object, type); } return continueCastOnNumber(object, type); }