Example usage for com.fasterxml.jackson.databind.type TypeFactory constructType

List of usage examples for com.fasterxml.jackson.databind.type TypeFactory constructType

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.type TypeFactory constructType.

Prototype

public JavaType constructType(Type paramType) 

Source Link

Usage

From source file:org.apache.airavata.db.AbstractThriftDeserializer.java

/**
 * Generates a {@link JavaType} that matches the target Thrift field represented by the provided
 * {@code <E>} enumerated value.  If the field's type includes generics, the generics will
 * be added to the generated {@link JavaType} to support proper conversion.
 * @param thriftInstance The Thrift-generated class instance that will be converted to/from JSON.
 * @param field A {@code <E>} enumerated value that represents a field in a Thrift-based entity.
 * @return The {@link JavaType} representation of the type associated with the field.
 * @throws NoSuchFieldException if unable to determine the field's type.
 * @throws SecurityException if unable to determine the field's type.
 *///from  ww  w .  java 2s .  c om
protected JavaType generateValueType(final T thriftInstance, final E field)
        throws NoSuchFieldException, SecurityException {
    final TypeFactory typeFactory = TypeFactory.defaultInstance();

    final Field declaredField = thriftInstance.getClass().getDeclaredField(field.getFieldName());
    if (declaredField.getType().equals(declaredField.getGenericType())) {
        log.debug("Generating JavaType for type '{}'.", declaredField.getType());
        return typeFactory.constructType(declaredField.getType());
    } else {
        final ParameterizedType type = (ParameterizedType) declaredField.getGenericType();
        final Class<?>[] parameterizedTypes = new Class<?>[type.getActualTypeArguments().length];
        for (int i = 0; i < type.getActualTypeArguments().length; i++) {
            parameterizedTypes[i] = (Class<?>) type.getActualTypeArguments()[i];
        }
        log.debug("Generating JavaType for type '{}' with generics '{}'", declaredField.getType(),
                parameterizedTypes);
        return typeFactory.constructParametricType(declaredField.getType(), parameterizedTypes);
    }
}

From source file:io.swagger.inflector.utils.ReflectionUtils.java

public JavaType getTypeFromModel(String name, Model model, Map<String, Model> definitions) {
    TypeFactory tf = Json.mapper().getTypeFactory();

    if (model instanceof RefModel && "".equals(name)) {
        RefModel ref = (RefModel) model;
        name = ref.getSimpleRef();//from  www. j av a  2s. c om
    }
    if (config != null && config.getModelMapping(name) != null) {
        return tf.constructType(config.getModelMapping(name));
    }
    if (model.getVendorExtensions() != null
            && model.getVendorExtensions().get("x-swagger-router-model") != null) {
        String modelName = (String) model.getVendorExtensions().get("x-swagger-router-model");
        Class<?> cls = loadClass(modelName);
        if (cls != null) {
            return tf.constructType(cls);
        }
        if (config.getModelPackage() != null && modelName.indexOf(".") == -1) {
            modelName = config.getModelPackage() + "." + modelName;
        }
        cls = loadClass(modelName);
        if (cls != null) {
            return tf.constructType(cls);
        }
    }
    // load from default package
    if (!"".equals(name)) {
        String modelName = name;
        if (config.getModelPackage() != null && name.indexOf(".") == -1) {
            modelName = config.getModelPackage() + "." + modelName;
        }
        Class<?> cls = loadClass(modelName);
        if (cls != null) {
            return tf.constructType(cls);
        }
    }
    if (model instanceof ArrayModel) {
        ArrayModel am = (ArrayModel) model;
        Property inner = am.getItems();
        JavaType innerType = getTypeFromProperty(inner.getType(), inner.getFormat(), inner, definitions);
        if (innerType != null) {
            return tf.constructArrayType(innerType);
        } else {
            return tf.constructArrayType(JsonNode.class);
        }
    }
    if (model instanceof RefModel) {
        RefModel ref = (RefModel) model;
        Model inner = definitions.get(ref.getSimpleRef());
        if (inner != null) {
            return getTypeFromModel(name, inner, definitions);
        }
    }
    return tf.constructType(JsonNode.class);
}

From source file:org.hawkular.wildfly.agent.itest.util.AbstractITest.java

protected ResourceType getResourceType(String path, int attemptCount, long attemptDelay) throws Throwable {
    String url = baseInvUri + path;
    Throwable e = null;//from  w w  w. jav a2  s.c o m
    for (int i = 0; i < attemptCount; i++) {
        try {
            String body = getWithRetries(url, attemptCount, attemptDelay);
            TypeFactory tf = mapper.getTypeFactory();
            JavaType javaType = tf.constructType(ResourceType.class);
            JsonNode node = mapper.readTree(body);
            ResourceType result = mapper.readValue(node.traverse(), javaType);
            return result;
            // System.out.println(body);
        } catch (Throwable t) {
            /* some initial attempts may fail */
            e = t;
            System.out.println("URL [" + url + "] not ready yet on " + (i + 1) + " of " + attemptCount
                    + " attempts, about to retry after " + attemptDelay + " ms");
        }
        Thread.sleep(attemptDelay);
    }
    if (e != null) {
        throw e;
    } else {
        throw new AssertionError("Could not get [" + url + "]");
    }
}

From source file:org.hawkular.wildfly.agent.itest.util.AbstractITest.java

protected DataEntity getDataEntity(String path, int attemptCount, long attemptDelay) throws Throwable {
    String url = baseInvUri + path;
    Throwable e = null;//  w w w . j a v a  2  s. co m
    for (int i = 0; i < attemptCount; i++) {
        try {
            String body = getWithRetries(url, attemptCount, attemptDelay);
            TypeFactory tf = mapper.getTypeFactory();
            JavaType javaType = tf.constructType(DataEntity.class);
            JsonNode node = mapper.readTree(body);
            DataEntity result = mapper.readValue(node.traverse(), javaType);
            return result;
            // System.out.println(body);
        } catch (Throwable t) {
            /* some initial attempts may fail */
            e = t;
            System.out.println("URL [" + url + "] not ready yet on " + (i + 1) + " of " + attemptCount
                    + " attempts, about to retry after " + attemptDelay + " ms");
        }
        Thread.sleep(attemptDelay);
    }
    if (e != null) {
        throw e;
    } else {
        throw new AssertionError("Could not get [" + url + "]");
    }
}

From source file:ch.ralscha.extdirectspring.util.ParametersResolver.java

private Object convertValue(Object value, ParameterInfo methodParameter) {
    if (value != null) {
        Class<?> rawType = methodParameter.getType();
        if (rawType.equals(value.getClass())) {
            return value;
        } else if (this.conversionService.canConvert(TypeDescriptor.forObject(value),
                methodParameter.getTypeDescriptor())) {

            try {
                return this.conversionService.convert(value, TypeDescriptor.forObject(value),
                        methodParameter.getTypeDescriptor());
            } catch (ConversionFailedException e) {
                // ignore this exception for collections and arrays.
                // try to convert the value with jackson
                TypeFactory typeFactory = this.jsonHandler.getMapper().getTypeFactory();
                if (methodParameter.getTypeDescriptor().isCollection()) {

                    JavaType elemType = typeFactory.constructType(
                            methodParameter.getTypeDescriptor().getElementTypeDescriptor().getType());
                    TypeVariable<?>[] vars = rawType.getTypeParameters();
                    TypeBindings bindings;
                    if ((vars == null) || (vars.length != 1)) {
                        bindings = TypeBindings.emptyBindings();
                    } else {
                        bindings = TypeBindings.create(rawType, elemType);
                    }// www  .  j a  v a 2s.  c  o  m
                    JavaType superClass = null;
                    Class<?> parent = rawType.getSuperclass();
                    if (parent != null) {
                        superClass = TypeFactory.unknownType();
                    }

                    JavaType type = CollectionType.construct(rawType, bindings, superClass, null, elemType);
                    return this.jsonHandler.convertValue(value, type);
                } else if (methodParameter.getTypeDescriptor().isArray()) {
                    JavaType type = typeFactory.constructArrayType(
                            methodParameter.getTypeDescriptor().getElementTypeDescriptor().getType());
                    return this.jsonHandler.convertValue(value, type);
                }

                throw e;
            }
        } else {
            return this.jsonHandler.convertValue(value, rawType);
        }

    } else if (methodParameter.isJavaUtilOptional()) {
        return javaUtilOptionalEmpty;
    }

    return null;
}

From source file:org.candlepin.swagger.CandlepinSwaggerModelConverter.java

protected JavaType getInnerType(String innerType) {
    try {//www.ja  va  2 s.  c o  m
        Class<?> innerClass = Class.forName(innerType);
        if (innerClass != null) {
            TypeFactory tf = pMapper.getTypeFactory();
            return tf.constructType(innerClass);
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.springframework.http.codec.json.Jackson2CodecSupport.java

protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
    TypeFactory typeFactory = this.objectMapper.getTypeFactory();
    return typeFactory.constructType(GenericTypeResolver.resolveType(type, contextClass));
}