Example usage for org.springframework.core.convert TypeDescriptor getResolvableType

List of usage examples for org.springframework.core.convert TypeDescriptor getResolvableType

Introduction

In this page you can find the example usage for org.springframework.core.convert TypeDescriptor getResolvableType.

Prototype

public ResolvableType getResolvableType() 

Source Link

Document

Return the underlying ResolvableType .

Usage

From source file:tv.arte.resteventapi.core.querying.convertion.QueryOpParamGenericConverter.java

/**
 * {@inheritDoc}/*  w w w.j a v a 2s.  c  om*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

    Object result = null;

    if (sourceType.getResolvableType().resolve().equals(String.class)
            && targetType.getResolvableType().resolve().equals(QueryOpParamValue.class)) {
        QueryOpParamValue queryOpParamValue = new QueryOpParamValue();

        ResolvableType genericResolvableType = targetType.getResolvableType().getGeneric(0);
        TypeDescriptor finalTargerTypeDescriptor = null;

        if (ResolvableType.NONE.equals(genericResolvableType)) {
            finalTargerTypeDescriptor = TypeDescriptor.valueOf(String.class);
        } else {
            finalTargerTypeDescriptor = new RestEventApiTypeDescriptor(genericResolvableType, null,
                    genericResolvableType.resolve().getAnnotations());
        }

        int queryOpSeparatorPosition = this.queryOpValueSeparatorPosition((String) source);
        QueryOp queryOp = this.extractQueryOp((String) source);
        String valuePart = this.extractValueString(queryOpSeparatorPosition, queryOp, (String) source);

        queryOpParamValue.setQueryOp(queryOp);
        queryOpParamValue.setValue(conversionService.convert(valuePart, sourceType, finalTargerTypeDescriptor));

        result = queryOpParamValue;
    } else if (sourceType.getResolvableType().resolve().equals(QueryOpParamValue.class)
            && targetType.getResolvableType().resolve().equals(String.class)) {
        QueryOpParamValue valToVonvert = (QueryOpParamValue) source;

        if (valToVonvert.getValue() != null) {
            String valueStr = conversionService.convert(valToVonvert.getValue(), String.class);
            if (StringUtils.isNotBlank(valueStr)) {
                result = this.assembleQueryOpValue(valToVonvert.getQueryOp(), valueStr);
            }
        }
    } else {
        throw new RestEventApiRuntimeException("Uncompatible conversion");
    }

    return result;
}