List of utility methods to do Object Type Case
D | cast(B b0, Class 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; |
int | cast(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; |
String | cast(Class c) cast if (c.isPrimitive()) { return ""; return "(" + c.getCanonicalName() + ")"; |
T | cast(Class extends T> 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()); |
T | cast(Class 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; |
T | cast(Class cast if (clazz == null) throw new NullPointerException(); if (obj == null || clazz.isAssignableFrom(obj.getClass())) { return (T) obj; throw new ClassCastException(msgClassCastException(clazz, obj)); |
T | cast(Class 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()); |
T | cast(Class 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); |
T | cast(Class 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); |