Avoid NPE in ObjectToObjectConverter

Bypass ObjectToObject conversion when source and object types are
identical and protect against a null source object.

Prior to this commit the TypeDescriptor was used to determine if
conversion was necessary.  This caused issues when comparing a
descriptor with annotations to one without.  The updated code
now compares using TypeDescriptor.getType().

The ObjectToObject converter will now no longer attempt to convert
null source objects.

Issue: SPR-9933
This commit is contained in:
Phillip Webb
2012-10-30 11:25:10 -07:00
parent 9055a7f810
commit 31331e6ad3
2 changed files with 26 additions and 1 deletions

View File

@@ -48,10 +48,17 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return !sourceType.equals(targetType) && hasValueOfMethodOrConstructor(targetType.getType(), sourceType.getType());
if (sourceType.getType().equals(targetType.getType())) {
// no conversion required
return false;
}
return hasValueOfMethodOrConstructor(targetType.getType(), sourceType.getType());
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
Class<?> sourceClass = sourceType.getType();
Class<?> targetClass = targetType.getType();
Method method = getValueOfMethodOn(targetClass, sourceClass);