Example usage for java.lang ClassCastException initCause

List of usage examples for java.lang ClassCastException initCause

Introduction

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

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:com.almende.util.TypeUtil.java

/**
 * Inject.// www. ja v  a 2s.  c o  m
 *
 * @param <T> the generic type
 * @param value the value
 * @param fullType the full type
 * @return the t
 */
@SuppressWarnings("unchecked")
public static <T> T inject(final Object value, final JavaType fullType) {
    if (value == null) {
        return null;
    }
    if (fullType.hasRawClass(Void.class)) {
        return null;
    }
    final ObjectMapper mapper = JOM.getInstance();
    if (value instanceof JsonNode) {
        if (((JsonNode) value).isNull()) {
            return null;
        }
        try {
            return mapper.convertValue(value, fullType);
        } catch (final Exception e) {
            ClassCastException cce = new ClassCastException(
                    "Failed to convert value:" + value + " -----> " + fullType);
            cce.initCause(e);
            throw cce;
        }
    }
    if (fullType.getRawClass().isAssignableFrom(value.getClass())) {
        return (T) value;
    } else {
        throw new ClassCastException(value.getClass().getCanonicalName() + " can't be converted to: "
                + fullType.getRawClass().getCanonicalName());
    }
}

From source file:com.couchbase.client.java.document.json.JsonObject.java

/**
 * Constructs a {@link JsonObject} from a {@link Map Map&lt;String, ?&gt;}.
 *
 * This is only possible if the given Map is well formed, that is it contains non null
 * keys, and all values are of a supported type.
 *
 * A null input Map or null key will lead to a {@link NullPointerException} being thrown.
 * If any unsupported value is present in the Map, an {@link IllegalArgumentException}
 * will be thrown.//w  ww .  ja  v a  2 s . c  om
 *
 * *Sub Maps and Lists*
 * If possible, Maps and Lists contained in mapData will be converted to JsonObject and
 * JsonArray respectively. However, same restrictions apply. Any non-convertible collection
 * will raise a {@link ClassCastException}. If the sub-conversion raises an exception (like an
 * IllegalArgumentException) then it is put as cause for the ClassCastException.
 *
 * @param mapData the Map to convert to a JsonObject
 * @return the resulting JsonObject
 * @throws IllegalArgumentException in case one or more unsupported values are present
 * @throws NullPointerException in case a null map is provided or if it contains a null key
 * @throws ClassCastException if map contains a sub-Map or sub-List not supported (see above)
 */
public static JsonObject from(Map<String, ?> mapData) {
    if (mapData == null) {
        throw new NullPointerException("Null input Map unsupported");
    } else if (mapData.isEmpty()) {
        return JsonObject.empty();
    }

    JsonObject result = new JsonObject(mapData.size());
    for (Map.Entry<String, ?> entry : mapData.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();

        if (value == JsonValue.NULL) {
            value = null;
        }

        if (key == null) {
            throw new NullPointerException("The key is not allowed to be null");
        } else if (value instanceof Map) {
            try {
                JsonObject sub = JsonObject.from((Map<String, ?>) value);
                result.put(key, sub);
            } catch (ClassCastException e) {
                throw e;
            } catch (Exception e) {
                ClassCastException c = new ClassCastException(
                        "Couldn't convert sub-Map " + key + " to JsonObject");
                c.initCause(e);
                throw c;
            }
        } else if (value instanceof List) {
            try {
                JsonArray sub = JsonArray.from((List<?>) value);
                result.put(key, sub);
            } catch (Exception e) {
                //no risk of a direct ClassCastException here
                ClassCastException c = new ClassCastException(
                        "Couldn't convert sub-List " + key + " to JsonArray");
                c.initCause(e);
                throw c;
            }
        } else if (!checkType(value)) {
            throw new IllegalArgumentException("Unsupported type for JsonObject: " + value.getClass());
        } else {
            result.put(key, value);
        }
    }
    return result;
}

From source file:edu.wisc.commons.httpclient.HttpParamsBean.java

private <T> T getTypedParameter(String name, T defaultValue, Class<T> type, Function<Object, T> parser) {
    final Object value = delegate.getParameter(name);

    //If null return default value
    if (value == null) {
        return defaultValue;
    }/*ww w.  j av a  2 s . co m*/

    //If already the right type just cast and return
    final Class<? extends Object> valueType = value.getClass();
    if (type.isAssignableFrom(valueType)) {
        return type.cast(value);
    }

    //Try parsing the value to the desired type
    try {
        return parser.apply(value);
    } catch (Exception e) {
        final ClassCastException cce = new ClassCastException(
                "Cannot convert '" + value + "' of type " + valueType.getName() + " to " + type.getName());
        cce.initCause(e);
        throw cce;
    }
}

From source file:com.ntsync.android.sync.activities.VerifyPaymentProgressDialog.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {// w  w  w  . ja  v a  2  s  .com
        resultListener = (VerifyPaymentDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        ClassCastException ex = new ClassCastException(
                activity.toString() + " must implement VerifyPaymentDialogListener");
        ex.initCause(e);
        throw ex;
    }
}

From source file:com.ntsync.android.sync.activities.RegistrateProgressDialog.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {// w w  w  .  j  a va  2s. c o  m
        resultListener = (RegistrateDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        ClassCastException ex = new ClassCastException(
                activity.toString() + " must implement RegistrateDialogListener");
        ex.initCause(e);
        throw ex;
    }
}

From source file:com.ntsync.android.sync.activities.LoginProgressDialog.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*from  ww w  .ja  va 2 s.  c om*/
        resultListener = (LoginDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        ClassCastException ex = new ClassCastException(
                activity.toString() + " must implement LoginDialogListener");
        ex.initCause(e);
        throw ex;
    }
}

From source file:com.ntsync.android.sync.activities.ImportProgressDialog.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*  ww  w . j  av a2  s .  c o  m*/
        resultListener = (ImportDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        ClassCastException ex = new ClassCastException(
                activity.toString() + " must implement ImportDialogListener");
        ex.initCause(e);
        throw ex;
    }
}

From source file:com.ntsync.android.sync.activities.CreatePwdProgressDialog.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {//  w  ww  . ja va 2  s. c om
        resultListener = (CreatePwdDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        ClassCastException ex = new ClassCastException(
                activity.toString() + " must implement CreatePwdDialogListener");
        ex.initCause(e);
        throw ex;
    }
}