ConversionService fully supports conversion from String to MediaType now (through 'valueOf'; SPR-7282); revised exception handling in ObjectToObjectConverter, avoiding InvocationTargetExceptions

This commit is contained in:
Juergen Hoeller
2010-06-14 23:23:49 +00:00
parent 902938e95f
commit 96b1dc9db4
4 changed files with 78 additions and 76 deletions

View File

@@ -40,7 +40,7 @@ public final class ConversionFailedException extends ConversionException {
* @param cause the cause of the conversion failure
*/
public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
super("Unable to convert value " + value + " from type '" + sourceType.getName() +
super("Unable to convert value \"" + value + "\" from type '" + sourceType.getName() +
"' to type '" + targetType.getName() + "'", cause);
this.sourceType = sourceType;
this.targetType = targetType;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,37 +54,27 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Class<?> sourceClass = sourceType.getObjectType();
Class<?> targetClass = targetType.getObjectType();
Object target;
Method method = getValueOfMethodOn(targetClass, sourceClass);
if (method != null) {
ReflectionUtils.makeAccessible(method);
target = ReflectionUtils.invokeMethod(method, null, source);
}
else {
Constructor<?> constructor = getConstructor(targetClass, sourceClass);
if (constructor != null) {
try {
target = constructor.newInstance(source);
}
catch (IllegalArgumentException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
catch (InstantiationException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
catch (IllegalAccessException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
catch (InvocationTargetException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
try {
if (method != null) {
ReflectionUtils.makeAccessible(method);
return method.invoke(null, source);
}
else {
throw new IllegalStateException("No static valueOf(" + sourceClass.getName() +
") method or Constructor(" + sourceClass.getName() + ") exists on " + targetClass.getName());
Constructor<?> constructor = getConstructor(targetClass, sourceClass);
if (constructor != null) {
return constructor.newInstance(source);
}
}
}
return target;
catch (InvocationTargetException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex.getTargetException());
}
catch (Throwable ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
throw new IllegalStateException("No static valueOf(" + sourceClass.getName() +
") method or Constructor(" + sourceClass.getName() + ") exists on " + targetClass.getName());
}