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:com.ericbarnhill.arrayMath.MathArrayFloat1D.java

protected MathArrayFloat1D divide(Complex g) {
    throw new ClassCastException("Cannot divide Complex number to double array");
}

From source file:jp.terasoluna.fw.util.ConvertUtil.java

/**
 * <code>T</code>???//from  w w w  .j  a  v  a2  s. c o m
 * <p>
 * <ul>
 *  <li><code>allowsNull</code>?<code>false</code>??
 *        <code>obj</code>?<code>null</code> - 
 *  <li><code>allowsNull</code>?<code>true</code>??
 *        <code>obj</code>?<code>null</code> - <code>null</code>?
 *  <li><code>obj</code>?<code>clazz</code> - ??????
 *  <li><code>obj</code>?<code>clazz</code>????
 *        - <code>ConvertUtils</code>?????????
 * </ul>
 * </p>
 * 
 * @param <T> ??
 * @param obj 
 * @param clazz ??
 * @param allowsNull <code>obj</code>?<code>null</code>?
 *      ??????
 * @return ??
 * @throws IllegalArgumentException <code>clazz</code>?
 *      <code>null</code>??
 *      <code>allowsNull</code>?<code>false</code>??
 *        <code>obj</code>?<code>null</code>??
 *      ?????
 */
@SuppressWarnings("unchecked")
public static <T> T convert(Object obj, Class<T> clazz, boolean allowsNull) throws IllegalArgumentException {

    if (clazz == null) {
        throw new IllegalArgumentException("Argument 'clazz' (" + Object.class.getName() + ") is null");
    }

    if (obj == null) {
        if (!allowsNull) {
            String message = "Unable to cast 'null' to '" + clazz.getName() + "'";
            throw new IllegalArgumentException(message, new ClassCastException(message));
        }
        return null;
    }

    if (clazz.isAssignableFrom(obj.getClass())) {
        return (T) obj;
    }

    Object result = null;
    try {
        result = ConvertUtils.convert(obj.toString(), clazz);
    } catch (ConversionException e) {
        throw new IllegalArgumentException(e);
    }
    return (T) result;
}

From source file:com.ericbarnhill.arrayMath.MathArrayFloat2D.java

protected MathArrayFloat2D divide(Complex g) {
    throw new ClassCastException("Cannot divide Complex number to double array");
}

From source file:com.ericbarnhill.arrayMath.MathArrayFloat3D.java

protected MathArrayFloat3D divide(Complex g) {
    throw new ClassCastException("Cannot divide Complex number to double array");
}

From source file:com.codelanx.codelanxlib.config.Config.java

/**
 * Attempts to return the {@link Config} value as a casted type. If the
 * value cannot be casted it will attempt to return the default value. If
 * the default value is inappropriate for the class, the method will
 * throw a {@link ClassCastException}. This exception is also throwing if
 * the type used for the members contained within the collection are
 * incorrect//  w  ww  .  j  av a 2 s  . c  om
 * 
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param <K> The type of the keys for this map
 * @param <V> The type of the values for this map
 * @param <M> The type of the map
 * @param map The class object of the map type to use
 * @param key The class object of the key types
 * @param value The class object of the value types
 * @return A casted value, or {@code null} if unable to cast. If the passed
 *         class parameter is of a primitive type or autoboxed primitive,
 *         then a casted value of -1 is returned, or {@code false} for
 *         booleans. If the passed class parameter is for {@link String},
 *         then {@link Object#toString()} is called on the value instead
 */
@SuppressWarnings("rawtypes")
default public <K, V, M extends Map<K, V>> M as(Class<? extends Map> map, Class<K> key, Class<V> value) {
    Map<?, ?> m = this.as(map);
    for (Map.Entry<?, ?> ent : m.entrySet()) {
        if (!key.isInstance(ent.getKey()) || !value.isInstance(ent.getValue())) {
            throw new ClassCastException("Inappropriate generic types for map");
        }
    }
    return (M) m;
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Convert a json array to a list of booleans.
 * if arr is null, then an empty List<Boolean> instance is returned.
 * /*from   w w w .java 2 s . c om*/
 * This is more safe than JsonArray.getValuesAs() as this method
 * has built-in type checking and will throw a ClassCastException if 
 * the type is incorrect or null.
 * 
 * @param arr array
 * @return a list
 * @throws ClassCastException if any element in arr is not an boolean
 */
public static List<Boolean> jsonArrayToBooleanList(final JsonArray arr) {
    final List<Boolean> out = new ArrayList<>();
    if (arr == null)
        return out;

    for (int i = 0; i < arr.size(); i++) {
        final Boolean b = arr.getBoolean(i);
        if (b == null) {
            throw new ClassCastException(
                    "Element at position " + String.valueOf(i) + " is null - Boolean required");
        }

        out.add(b);

        out.add(arr.getBoolean(i));
    }

    return out;
}

From source file:com.scooter1556.sms.android.fragment.AudioDirectoryFragment.java

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {//w w  w  . j  a v  a2s  .com
        mediaElementListener = (MediaElementFragment.MediaElementListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implement MediaElementListener");
    }
}

From source file:ezbake.data.elastic.thrift.TermsStatsValue.java

@Override
protected void checkType(_Fields setField, Object value) throws ClassCastException {
    switch (setField) {
    case VALUE_FIELD:
        if (value instanceof String) {
            break;
        }/*w  ww . ja  v  a2s. c  o m*/
        throw new ClassCastException("Was expecting value of type String for field 'valueField', but got "
                + value.getClass().getSimpleName());
    case VALUE_SCRIPT:
        if (value instanceof ValueScript) {
            break;
        }
        throw new ClassCastException("Was expecting value of type ValueScript for field 'valueScript', but got "
                + value.getClass().getSimpleName());
    default:
        throw new IllegalArgumentException("Unknown field id " + setField);
    }
}

From source file:ezbake.data.elastic.thrift.GeoSortValue.java

@Override
protected void checkType(_Fields setField, Object value) throws ClassCastException {
    switch (setField) {
    case GEO_HASH:
        if (value instanceof String) {
            break;
        }/*ww w . j a  v  a 2 s .  com*/
        throw new ClassCastException("Was expecting value of type String for field 'geoHash', but got "
                + value.getClass().getSimpleName());
    case COORDINATE:
        if (value instanceof ezbake.base.thrift.Coordinate) {
            break;
        }
        throw new ClassCastException(
                "Was expecting value of type ezbake.base.thrift.Coordinate for field 'coordinate', but got "
                        + value.getClass().getSimpleName());
    default:
        throw new IllegalArgumentException("Unknown field id " + setField);
    }
}

From source file:ezbake.data.elastic.thrift.MissingSort.java

@Override
protected void checkType(_Fields setField, Object value) throws ClassCastException {
    switch (setField) {
    case BASIC://from w ww . ja  v  a  2  s.c  o m
        if (value instanceof MissingOrder) {
            break;
        }
        throw new ClassCastException("Was expecting value of type MissingOrder for field 'basic', but got "
                + value.getClass().getSimpleName());
    case CUSTOM:
        if (value instanceof String) {
            break;
        }
        throw new ClassCastException("Was expecting value of type String for field 'custom', but got "
                + value.getClass().getSimpleName());
    default:
        throw new IllegalArgumentException("Unknown field id " + setField);
    }
}