Java Examples for org.springframework.core.convert.ConverterNotFoundException
The following java examples will help you to understand the usage of org.springframework.core.convert.ConverterNotFoundException. These source code samples are taken from different open source projects.
Example 1
Project: spring-framework-master File: GenericConversionServiceTests.java View source code |
@Test public void adaptedCollectionTypesFromSameSourceType() throws Exception { conversionService.addConverter(new MyStringToStringCollectionConverter()); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection")))); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection")))); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))); try { conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))); fail("Should have thrown ConverterNotFoundException"); } catch (ConverterNotFoundException ex) { } }
Example 2
Project: camel-master File: SpringTypeConverter.java View source code |
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
// do not attempt to convert Camel types
if (type.getCanonicalName().startsWith("org.apache")) {
return null;
}
// do not attempt to convert List -> Map. Ognl expression may use this converter as a fallback expecting null
if (type.isAssignableFrom(Map.class) && (value.getClass().isArray() || value instanceof Collection)) {
return null;
}
TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);
for (ConversionService conversionService : conversionServices) {
if (conversionService.canConvert(sourceType, targetType)) {
try {
return (T) conversionService.convert(value, sourceType, targetType);
} catch (ConversionFailedException e) {
if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
return null;
} else {
throw new TypeConversionException(value, type, e);
}
}
}
}
return null;
}
Example 3
Project: molgenis-master File: DataConverter.java View source code |
public static String toString(Object source) {
if (source == null)
return null;
if (source instanceof String)
return (String) source;
if (source instanceof Entity) {
Object labelValue = ((Entity) source).getLabelValue();
return labelValue != null ? labelValue.toString() : null;
}
if (source instanceof List) {
StringBuilder sb = new StringBuilder();
for (Object obj : (List<?>) source) {
if (sb.length() > 0)
sb.append(",");
sb.append(toString(obj));
}
return sb.toString();
}
if (getConversionService() == null)
return source.toString();
try {
return convert(source, String.class);
} catch (ConverterNotFoundException e) {
return source.toString();
}
}
Example 4
Project: jdal-master File: DirectFieldAccessor.java View source code |
@Override
public void setPropertyValue(String propertyName, Object newValue) throws BeansException {
Field field = this.fieldMap.get(propertyName);
if (field == null) {
throw new NotWritablePropertyException(this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist");
}
Object oldValue = null;
try {
ReflectionUtils.makeAccessible(field);
oldValue = field.get(this.target);
// jlm - Adapt to simpleTypeConverter
Object convertedValue = this.typeConverterDelegate.convertIfNecessary(newValue, field.getType());
field.set(this.target, convertedValue);
} catch (ConverterNotFoundException ex) {
PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
throw new ConversionNotSupportedException(pce, field.getType(), ex);
} catch (ConversionException ex) {
PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
throw new TypeMismatchException(pce, field.getType(), ex);
} catch (IllegalStateException ex) {
PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
throw new ConversionNotSupportedException(pce, field.getType(), ex);
} catch (IllegalArgumentException ex) {
PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
throw new TypeMismatchException(pce, field.getType(), ex);
} catch (IllegalAccessException ex) {
throw new InvalidPropertyException(this.target.getClass(), propertyName, "Field is not accessible", ex);
}
}
Example 5
Project: spring-android-master File: GenericConversionServiceTests.java View source code |
public void testGenericConverterDelegatingBackToConversionServiceConverterNotFound() { conversionService.addConverter(new ObjectToArrayConverter(conversionService)); assertFalse(conversionService.canConvert(String.class, Integer[].class)); try { conversionService.convert("3,4,5", Integer[].class); fail("expected ConverterNotFoundException"); } catch (ConverterNotFoundException ex) { } }
Example 6
Project: spring-xd-master File: DefaultTupleTests.java View source code |
@Test
public void testGetStringThatFails() {
Tuple tuple = TupleBuilder.tuple().of("up", "down", "charm", 2, "top", 2.0f, "black", Color.black);
thrown.expect(ConverterNotFoundException.class);
thrown.expectMessage("No converter found capable of converting from type [java.awt.Color] to type " + "[java.lang.String]");
assertThat(tuple.getString("black"), equalTo("omg"));
}
Example 7
Project: spring-data-cassandra-master File: MappingCassandraConverterUnitTests.java View source code |
// DATACASS-260
@Test
public void insertEnumDoesNotMapToOrdinalBeforeSpring43() {
assumeTrue(Version.parse(SpringVersion.getVersion()).isLessThan(VERSION_4_3));
expectedException.expect(ConverterNotFoundException.class);
UnsupportedEnumToOrdinalMapping unsupportedEnumToOrdinalMapping = new UnsupportedEnumToOrdinalMapping();
unsupportedEnumToOrdinalMapping.setAsOrdinal(Condition.MINT);
Insert insert = QueryBuilder.insertInto("table");
mappingCassandraConverter.write(unsupportedEnumToOrdinalMapping, insert);
}
Example 8
Project: spring-integration-master File: MessagingMethodInvokerHelper.java View source code |
@SuppressWarnings("unchecked")
private T invokeHandlerMethod(HandlerMethod handlerMethod, ParametersWrapper parameters) throws Exception {
try {
return (T) handlerMethod.invoke(parameters);
} catch (MethodArgumentResolutionExceptionMessageConversionException | IllegalStateException | e) {
if (e instanceof MessageConversionException) {
if (e.getCause() instanceof ConversionFailedException && !(e.getCause().getCause() instanceof ConverterNotFoundException)) {
throw e;
}
} else if (e instanceof IllegalStateException) {
if (!(e.getCause() instanceof IllegalArgumentException) || !e.getStackTrace()[0].getClassName().equals(InvocableHandlerMethod.class.getName()) || (!"argument type mismatch".equals(e.getCause().getMessage()) && !e.getCause().getMessage().startsWith("java.lang.ClassCastException@"))) {
throw e;
}
}
Expression expression = handlerMethod.expression;
if (++handlerMethod.failedAttempts >= FAILED_ATTEMPTS_THRESHOLD) {
handlerMethod.spelOnly = true;
if (logger.isInfoEnabled()) {
logger.info("Failed to invoke [ " + handlerMethod.invocableHandlerMethod + "] with provided arguments [ " + parameters + " ]. \n" + "Falling back to SpEL invocation for expression [ " + expression.getExpressionString() + " ]");
}
}
return invokeExpression(expression, parameters);
}
}
Example 9
Project: spring-boot-master File: BinderConversionService.java View source code |
private <T> T callAdditionalConversionService(Function<ConversionService, T> call, RuntimeException cause) {
try {
return call.apply(this.additionalConversionService);
} catch (ConverterNotFoundException ex) {
throw (cause != null ? cause : ex);
}
}
Example 10
Project: spring-data-neo4j-master File: ConversionServiceTests.java View source code |
@Test(expected = ConverterNotFoundException.class)
public void shouldThrowExceptionIfSuitableConverterIsNotFound() {
this.conversionService.addConverterFactory(new SpringMonetaryAmountToNumberConverterFactory());
PensionPlan pension = new PensionPlan(new MonetaryAmount(20_000, 00), "Ashes Assets LLP");
pension.setJavaElement(new JavaElement());
this.pensionRepository.save(pension);
}
Example 11
Project: graphity-core-master File: ConversionServiceTests.java View source code |
@Test(expected = ConverterNotFoundException.class)
public void shouldThrowExceptionIfSuitableConverterIsNotFound() {
this.conversionService.addConverterFactory(new SpringMonetaryAmountToNumberConverterFactory());
PensionPlan pension = new PensionPlan(new MonetaryAmount(20_000, 00), "Ashes Assets LLP");
pension.setJavaElement(new JavaElement());
this.pensionRepository.save(pension);
}