ConversionExecutor now checks for assignment compatible types (SWF-337).

This commit is contained in:
Erwin Vervaet
2007-06-13 15:30:35 +00:00
parent 1cfc17dee8
commit e727aef1b5
4 changed files with 95 additions and 2 deletions

View File

@@ -103,6 +103,20 @@ public class ConversionException extends NestedRuntimeException {
this.targetClass = null; // not available
}
/**
* Creates a new conversion exception.
* @param sourceClass the source type
* @param value the value we tried to convert
* @param targetClass the target type
* @param message a descriptive message
*/
public ConversionException(Class sourceClass, Object value, Class targetClass, String message) {
super(message);
this.sourceClass = sourceClass;
this.value = value;
this.targetClass = targetClass;
}
/**
* Returns the source type.
*/

View File

@@ -98,8 +98,13 @@ public class ConversionExecutor {
* behavior of the converter
*/
public Object execute(Object source, ConversionContext context) throws ConversionException {
if (source != null) {
Assert.isInstanceOf(sourceClass, source, "Not of source type: ");
if (getTargetClass().isInstance(source)) {
// source is already assignment compatible with target class
return source;
}
if (source != null && !getSourceClass().isInstance(source)) {
throw new ConversionException(getSourceClass(), source, getTargetClass(),
"Source object '" + source + "' is expected to be a " + getSourceClass());
}
return converter.convert(source, targetClass, context);
}