Example usage for org.springframework.core.convert ConversionFailedException ConversionFailedException

List of usage examples for org.springframework.core.convert ConversionFailedException ConversionFailedException

Introduction

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

Prototype

public ConversionFailedException(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType,
        @Nullable Object value, Throwable cause) 

Source Link

Document

Create a new conversion exception.

Usage

From source file:com.travelport.restneohack.model.domain.EmailAddressConverter.java

@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (source instanceof EmailAddress) {
        return ((EmailAddress) source).getEmail();
    }//from   w ww.ja v a 2 s.com
    if (source instanceof String) {
        return new EmailAddress((String) source);
    }
    throw new ConversionFailedException(sourceType, targetType, source, null);
}

From source file:com._4dconcept.springframework.data.marklogic.core.convert.MappingMarklogicConverter.java

@Override
public <R> R read(Class<R> returnType, MarklogicContentHolder holder) {
    ResultItem resultItem = (ResultItem) holder.getContent();
    if (String.class.equals(returnType)) {
        return returnType.cast(resultItem.asString());
    }//www .  java  2 s .  c  o  m

    R result = null;
    if (returnType.isPrimitive()) {
        try {
            Method method = MarklogicTypeUtils.primitiveMap.get(returnType).getMethod("valueOf", String.class);
            @SuppressWarnings("unchecked")
            R obj = (R) method.invoke(null, resultItem.asString());
            result = obj;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            LOGGER.debug("Unable to generate primitive value for type " + returnType.getName());
        }
    }

    if (result != null) {
        return result;
    }

    ConversionService conversionService = getConversionService();

    if (conversionService.canConvert(resultItem.getClass(), returnType)) {
        R convert = conversionService.convert(resultItem, returnType);

        if (convert == null) {
            throw new ConversionFailedException(TypeDescriptor.forObject(resultItem),
                    TypeDescriptor.valueOf(returnType), resultItem, new NullPointerException());
        }

        return convert;
    } else {
        throw new ConverterNotFoundException(TypeDescriptor.forObject(resultItem),
                TypeDescriptor.valueOf(returnType));
    }
}

From source file:com._4dconcept.springframework.data.marklogic.core.convert.MappingMarklogicConverter.java

@Override
public void write(Object source, MarklogicContentHolder holder) {
    TypeDescriptor sourceDescriptor = TypeDescriptor.forObject(source);
    TypeDescriptor targetDescriptor = TypeDescriptor.valueOf(String.class);

    if (getConversionService().canConvert(sourceDescriptor, targetDescriptor)) {
        String content = getConversionService().convert(source, String.class);

        if (content == null) {
            throw new ConversionFailedException(sourceDescriptor, targetDescriptor, source,
                    new NullPointerException("Conversion result is not e"));
        } else {/*w w  w. j ava2  s  .c o  m*/
            holder.setContent(content);
        }
    } else {
        throw new ConverterNotFoundException(sourceDescriptor, targetDescriptor);
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testNullNestedTypeDescriptorWithBadConversionService() {
    Foo foo = new Foo();
    JuffrouSpringBeanWrapper wrapper = new JuffrouSpringBeanWrapper(foo);
    wrapper.setConversionService(new GenericConversionService() {
        @Override/*from  www. j  ava  2  s  .  c  o m*/
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    wrapper.setAutoGrowNestedPaths(true);
    wrapper.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", foo.listOfMaps.get(0).get("luckyNumber"));
}

From source file:com._4dconcept.springframework.data.marklogic.core.MarklogicTemplate.java

private MarklogicIdentifier resolveMarklogicIdentifier(Object id, MarklogicPersistentProperty idProperty) {
    if (MarklogicTypeUtils.isSimpleType(idProperty.getType())) {
        return new MarklogicIdentifier() {
            @Override/*from   w w w  .  j a  va 2s.co  m*/
            public QName qname() {
                return idProperty.getQName();
            }

            @Override
            public String value() {
                return id.toString();
            }
        };
    }

    ConversionService conversionService = marklogicConverter.getConversionService();
    if (conversionService.canConvert(idProperty.getType(), MarklogicIdentifier.class)) {
        MarklogicIdentifier convert = conversionService.convert(id, MarklogicIdentifier.class);
        if (convert == null) {
            throw new ConversionFailedException(TypeDescriptor.forObject(id),
                    TypeDescriptor.valueOf(MarklogicIdentifier.class), id,
                    new NullPointerException("Conversion result is not expected to be null"));
        }

        return convert;
    }

    throw new MappingException("Unexpected identifier type " + idProperty.getClass());
}

From source file:org.springframework.beans.AbstractPropertyAccessorTests.java

@Test
public void setPropertyIntermediateListIsNullWithBadConversionService() {
    Foo target = new Foo();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setConversionService(new GenericConversionService() {
        @Override//from w ww.  j  a va2s. co m
        public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType,
                TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    accessor.setAutoGrowNestedPaths(true);
    accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", target.listOfMaps.get(0).get("luckyNumber"));
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testNullNestedTypeDescriptorWithBadConversionService() {
    Foo foo = new Foo();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(foo);
    wrapper.setConversionService(new GenericConversionService() {
        @Override//from  w ww.j  a  va 2  s  . c o  m
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    wrapper.setAutoGrowNestedPaths(true);
    wrapper.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", foo.listOfMaps.get(0).get("luckyNumber"));
}

From source file:org.springframework.core.convert.support.GenericConversionService.java

/**
 * Template method to convert a null source.
 * <p>Default implementation returns <code>null</code>.
 * Throws a {@link ConversionFailedException} if the targetType is a primitive type,
 * as <code>null</code> cannot be assigned to a primitive type.
 * Subclasses may override to return custom null objects for specific target types.
 * @param sourceType the sourceType to convert from
 * @param targetType the targetType to convert to
 * @return the converted null object/*from   w w w. j a va 2 s  . c  o m*/
 */
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (targetType.isPrimitive()) {
        throw new ConversionFailedException(sourceType, targetType, null,
                new IllegalArgumentException("A null value cannot be assigned to a primitive type"));
    }
    return null;
}