Java Utililty Methods Object Type Case

List of utility methods to do Object Type Case

Description

The list of methods to do Object Type Case are organized into topic(s).

Method

Dcast(B b0, Class cls)
Casts an object to the class or interface represented by the specified Class object.
D d0 = null;
if (b0 != null) {
    if (!cls.isInstance(b0)) {
        throw new ClassCastException(
                String.format("Failed to cast from '%s' to '%s'", b0.getClass().getName(), cls.getName()));
    } else {
        d0 = (D) b0;
return d0;
intcast(byte b)
cast
if (b >= 0) {
    return b;
return 256 + b;
char[]cast(byte[] bytes)
Byte-to-byte copy of an array of bytes to an array of chars without conversion.
char[] chars = new char[bytes.length >> 1];
for (int i = 0; i < chars.length; i++) {
    int pos = i << 1;
    char c = (char) (((bytes[pos] & 0x00FF) << 8) + (bytes[pos + 1] & 0x00FF));
    chars[i] = c;
return chars;
Stringcast(Class c)
cast
if (c.isPrimitive()) {
    return "";
return "(" + c.getCanonicalName() + ")";
Tcast(Class c, Object o)

An implementation of Class#cast , which is unavailable prior to Java 5.0.

if (box(c).isInstance(o)) {
    @SuppressWarnings("unchecked")
    T result = (T) o;
    return result;
} else {
    throw new ClassCastException("Casting to " + c.getName() + " from " + o.getClass().getName());
Tcast(Class clazz, Object o, T def)
Try to cast an object to a specific class and returns a default value if it was impossible.
try {
    return clazz.cast(o);
} catch (ClassCastException e) {
    return def;
Tcast(Class clazz, Object obj)
cast
if (clazz == null)
    throw new NullPointerException();
if (obj == null || clazz.isAssignableFrom(obj.getClass())) {
    return (T) obj;
throw new ClassCastException(msgClassCastException(clazz, obj));
Tcast(Class clazz, Object object)
cast
if (clazz.isAssignableFrom(object.getClass())) {
    return clazz.cast(object);
throw new ClassCastException("Cannot cast class " + object.getClass().getName() + ", whose classloader is "
        + object.getClass().getClassLoader() + ", to class " + clazz + ", whose classloader is "
        + clazz.getClassLoader());
Tcast(Class targetClass, Object obj)
Casts an object to the class or interface represented by the targetClass Class object.
try {
    return targetClass.cast(obj);
} catch (ClassCastException e) {
    assert obj != null : "a null can always be cast, it should never throw a ClassCastException";
    throw new ClassCastException("Unable to cast " + obj.getClass() + " to " + targetClass);
Tcast(Class toType, Object value)
Converts the given value to the required type or throw a meaningful exception
if (toType == boolean.class) {
    return (T) cast(Boolean.class, value);
} else if (toType.isPrimitive()) {
    Class newType = convertPrimitiveTypeToWrapperType(toType);
    if (newType != toType) {
        return (T) cast(newType, value);
try {
    return toType.cast(value);
} catch (ClassCastException e) {
    throw new IllegalArgumentException(
            "Failed to convert: " + value + " to type: " + toType.getName() + " due to: " + e, e);