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

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

Introduction

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

Prototype

public boolean isArray() 

Source Link

Document

Is this type an array type?

Usage

From source file:cz.jirutka.validator.spring.support.RelaxedBooleanTypeConverterDecorator.java

public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
    return targetType.isAssignableTo(BOOLEAN_TYPE)
            && (sourceType.isAssignableTo(NUMBER_TYPE) || sourceType.isCollection() || sourceType.isArray())
            || decorated.canConvert(sourceType, targetType);
}

From source file:cz.jirutka.validator.spring.support.RelaxedBooleanTypeConverterDecorator.java

public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (targetType.isAssignableTo(BOOLEAN_TYPE)) {
        if (value instanceof Number) {
            return ((Number) value).intValue() != 0;
        }//from  w  ww .j a  v  a2 s  . c  o m
        if (sourceType.isCollection()) {
            return !((Collection) value).isEmpty();
        }
        if (sourceType.isArray()) {
            return ((Object[]) value).length != 0;
        }
        return value;

    } else {
        return decorated.convertValue(value, sourceType, targetType);
    }
}

From source file:ch.rasc.wampspring.method.MethodParameterConverter.java

public Object convert(MethodParameter parameter, Object argument) {
    if (argument == null) {
        if (parameter.getParameterType().getName().equals("java.util.Optional")) {
            return OptionalUnwrapper.empty();
        }/*from  w w w  .  j  av a  2  s . c o m*/

        return null;
    }

    Class<?> sourceClass = argument.getClass();
    Class<?> targetClass = parameter.getParameterType();

    TypeDescriptor td = new TypeDescriptor(parameter);

    if (targetClass.isAssignableFrom(sourceClass)) {
        return convertListElements(td, argument);
    }

    if (this.conversionService.canConvert(sourceClass, targetClass)) {
        try {
            return convertListElements(td, this.conversionService.convert(argument, targetClass));
        } catch (Exception e) {

            TypeFactory typeFactory = this.objectMapper.getTypeFactory();
            if (td.isCollection()) {
                JavaType type = CollectionType.construct(td.getType(),
                        typeFactory.constructType(td.getElementTypeDescriptor().getType()));
                return this.objectMapper.convertValue(argument, type);
            } else if (td.isArray()) {
                JavaType type = typeFactory.constructArrayType(td.getElementTypeDescriptor().getType());
                return this.objectMapper.convertValue(argument, type);
            }

            throw e;
        }
    }
    return this.objectMapper.convertValue(argument, targetClass);
}