Example usage for java.lang ClassCastException ClassCastException

List of usage examples for java.lang ClassCastException ClassCastException

Introduction

In this page you can find the example usage for java.lang ClassCastException ClassCastException.

Prototype

public ClassCastException(String s) 

Source Link

Document

Constructs a ClassCastException with the specified detail message.

Usage

From source file:Main.java

public static <T> T castObjectOrThrow(Object o, Class<T> clazz) {
    try {/*from   w w w .ja va2  s.com*/
        return clazz.cast(o);
    } catch (ClassCastException e) {
        throw new ClassCastException(o.toString() + " must implement " + clazz.getSimpleName());
    }
}

From source file:Main.java

/**
 * @param file// ww w .  j a v  a  2  s .  c  o  m
 * @return
 */
public static int getNumFilesInFolder(File file) {
    if (file == null) {
        throw new NullPointerException("file cannot be null");
    }
    if (!file.exists()) {
        throw new NullPointerException("file does not exist");
    }
    if (file.isFile()) {
        throw new ClassCastException("file is not a directory");
    }
    return getSubfilesNumberInFolder(file, false, true);
}

From source file:MathUtils.java

public static BigDecimal getBigDecimal(Object value) {
    BigDecimal ret = null;/*w ww .  j  a va 2s .c  o m*/
    if (value != null) {
        if (value instanceof BigDecimal) {
            ret = (BigDecimal) value;
        } else if (value instanceof String) {
            ret = new BigDecimal((String) value);
        } else if (value instanceof BigInteger) {
            ret = new BigDecimal((BigInteger) value);
        } else if (value instanceof Number) {
            ret = new BigDecimal(((Number) value).doubleValue());
        } else {
            throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
                    + " into a BigDecimal.");
        }
    }
    return ret;
}

From source file:ArrayExpander.java

public static Object merge(Object array1, Object array2) {
    if (array1 == null) {
        return null;
    }//from   w w w  . j  av  a 2s .co  m
    if (array2 == null) {
        return array1;
    }
    Class c = array1.getClass();
    if (c.isArray() && array2.getClass().isArray()) {
        Class cc = c.getComponentType();
        Object newArray = Array.newInstance(cc, Array.getLength(array1) + Array.getLength(array2));
        System.arraycopy(array1, 0, newArray, 0, Array.getLength(array1));
        System.arraycopy(array2, 0, newArray, Array.getLength(array1), Array.getLength(array2));
        return newArray;
    } else {
        throw new ClassCastException("need  array");
    }

}

From source file:Main.java

/**
 * @param file/*from w ww  .j  ava  2  s  .  c o m*/
 * @param includeHiddleFiles
 * @param includeFolder
 * @return
 */
public static int getNumFilesInFolder(File file, boolean includeHiddleFiles, boolean includeFolder) {
    if (file == null) {
        throw new NullPointerException("file cannot be null");
    }
    if (!file.exists()) {
        throw new NullPointerException("file does not exist");
    }
    if (file.isFile()) {
        throw new ClassCastException("file is not a directory");
    }
    return getSubfilesNumberInFolder(file, includeHiddleFiles, includeFolder);
}

From source file:Main.java

/**
 * Check the type of list contents (for the situations where a simple upcast
 * is not enough.) As Java generics are implemented via erasure,
 * unfortunately the target class has to be passed as a parameter.
 * <p>// w  w  w.  ja  v  a  2 s . c  om
 * Basically, you can convert a list to a typed list via this method, using:
 * <pre>
 *   List someList = ...; // from somewhere else
 *   List&lt;MyType&gt; = typecheck(someList, MyType.class);
 * </pre>
 * The method will raise a class cast exception, if the input list contains
 * any element, that is not instanceof MyType or null.
 * @param T      the target type
 * @param l      the input list
 * @param tClass T's class
 * @return l as a parameterized type
 * 
 * @deprecated In the most common use case this is unnecessary. In the second
 *       common use case this is a sign that you have not understood generics.
 */
public static <T> List<T> typecheck(List<?> l, Class<T> tClass) {
    if (l == null)
        return null;

    for (Object o : l)
        if (o != null && !tClass.isInstance(o))
            throw new ClassCastException("ClassCast from " + o.getClass() + " to " + tClass);

    return (List<T>) l;
}

From source file:ArrayExpander.java

public static Object expand(Object array, int newSize) {
    if (array == null) {
        return null;
    }/*from   w  w w . ja  v a2  s .  co  m*/
    Class c = array.getClass();
    if (c.isArray()) {
        int len = Array.getLength(array);
        if (len >= newSize) {
            return array;
        } else {
            Class cc = c.getComponentType();
            Object newArray = Array.newInstance(cc, newSize);
            System.arraycopy(array, 0, newArray, 0, len);
            return newArray;
        }
    } else {
        throw new ClassCastException("need  array");
    }
}

From source file:edu.umich.flowfence.client.Sealed.java

public static <T> Sealed<T> wrap(IHandle handle, Class<T> refClass) {
    try {// w ww  . ja  va 2  s  .  c  o m
        ParamInfo pi = handle.getParamInfo();
        Class<?> declaredClass = pi.getType(refClass.getClassLoader());
        if (!ClassUtils.isAssignable(declaredClass, refClass, true)) {
            throw new ClassCastException("Can't assign " + declaredClass + " to reference of type " + refClass);
        }
        return new Sealed<>(handle);
    } catch (Exception e) {
        ParceledThrowable.throwUnchecked(e);
        return null;
    }
}

From source file:Person.java

public int compareTo(Object anotherPerson) throws ClassCastException {
    if (!(anotherPerson instanceof Person)) {
        throw new ClassCastException("A Person object expected.");
    }// w  w  w . j  a  v  a 2 s .  c o m
    int anotherPersonAge = ((Person) anotherPerson).getAge();
    return this.age - anotherPersonAge;
}

From source file:ArrayExpander.java

public static Object expandAtHead(Object array, int newSize) {
    if (array == null) {
        return null;
    }/*from w w w . ja va  2  s. c  o  m*/
    Class c = array.getClass();
    if (c.isArray()) {
        int len = Array.getLength(array);
        if (len >= newSize) {
            return array;
        } else {
            Class cc = c.getComponentType();
            Object newArray = Array.newInstance(cc, newSize);
            System.arraycopy(array, 0, newArray, newSize - len, len);
            return newArray;
        }
    } else {
        throw new ClassCastException("need  array");
    }
}