Example usage for com.fasterxml.jackson.databind JavaType hasRawClass

List of usage examples for com.fasterxml.jackson.databind JavaType hasRawClass

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JavaType hasRawClass.

Prototype

public abstract boolean hasRawClass(Class<?> clz);

Source Link

Usage

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

/**
 * Inject./*from  w  w  w  . java  2  s .c  om*/
 *
 * @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:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.wdtk.MapDeserializerModifier.java

private boolean isMapOfStringAndListOfStatements(JavaType type) {
    if (!type.containedType(0).hasRawClass(String.class)) {
        return false;
    }//from  w w w  .  j  a v a  2  s  .c om

    JavaType valueType = type.containedType(1);

    if (!valueType.hasRawClass(List.class)) {
        return false;
    }

    JavaType listType = valueType.containedType(0);
    if (!listType.hasRawClass(JacksonStatement.class)) {
        return false;
    }

    return true;
}

From source file:com.almende.eve.agent.Agent.java

@Override
@Access(AccessType.UNAVAILABLE)//from   ww  w  .ja v  a 2 s  . c  om
public final <T> void sendAsync(final URI url, final JSONRequest request, final AsyncCallback<T> callback,
        final JavaType type) throws IOException {

    // Create a callback to retrieve a JSONResponse and extract the result
    // or error from this. This is double nested, mostly because of the type
    // conversions required on the result.
    final AsyncCallback<JSONResponse> responseCallback = new AsyncCallback<JSONResponse>() {
        @SuppressWarnings("unchecked")
        @Override
        public void onSuccess(final JSONResponse response) {
            if (callback == null) {
                final Exception err = response.getError();
                if (err != null) {
                    LOG.warning("async RPC call failed, and no callback handler available:"
                            + err.getLocalizedMessage());
                }
            } else {
                final Exception err = response.getError();
                if (err != null) {
                    callback.onFailure(err);
                }
                if (type != null && !type.hasRawClass(Void.class)) {
                    try {
                        final T res = (T) TypeUtil.inject(response.getResult(), type);
                        callback.onSuccess(res);
                    } catch (final ClassCastException cce) {
                        callback.onFailure(
                                new JSONRPCException("Incorrect return type received for JSON-RPC call:"
                                        + request.getMethod() + "@" + url, cce));
                    }

                } else {
                    callback.onSuccess(null);
                }
            }
        }

        @Override
        public void onFailure(final Exception exception) {
            if (callback == null) {
                LOG.warning("async RPC call failed and no callback handler available:"
                        + exception.getLocalizedMessage());
            } else {
                callback.onFailure(exception);
            }
        }
    };

    send(request, url, responseCallback, null);
}