collection to object
This commit is contained in:
parent
67c02f6c35
commit
cb54869726
|
|
@ -15,6 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.core.convert.support;
|
package org.springframework.core.convert.support;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
class ArrayToObjectGenericConverter implements GenericConverter {
|
class ArrayToObjectGenericConverter implements GenericConverter {
|
||||||
|
|
@ -26,7 +28,18 @@ class ArrayToObjectGenericConverter implements GenericConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
throw new UnsupportedOperationException("Not yet implemented");
|
int length = Array.getLength(source);
|
||||||
|
if (length == 0) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||||
|
if (sourceElementType.isAssignableTo(targetType)) {
|
||||||
|
return Array.get(source, 0);
|
||||||
|
} else {
|
||||||
|
GenericConverter converter = conversionService.getConverter(sourceElementType, targetType);
|
||||||
|
return converter.convert(Array.get(source, 0), sourceElementType, targetType);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.core.convert.support;
|
package org.springframework.core.convert.support;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
class CollectionToObjectGenericConverter implements GenericConverter {
|
class CollectionToObjectGenericConverter implements GenericConverter {
|
||||||
|
|
@ -24,9 +26,19 @@ class CollectionToObjectGenericConverter implements GenericConverter {
|
||||||
public CollectionToObjectGenericConverter(GenericConversionService conversionService) {
|
public CollectionToObjectGenericConverter(GenericConversionService conversionService) {
|
||||||
this.conversionService = conversionService;
|
this.conversionService = conversionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
|
||||||
throw new UnsupportedOperationException("Not yet implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
Collection sourceCollection = (Collection) source;
|
||||||
|
if (sourceCollection.size() == 0) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||||
|
if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetType)) {
|
||||||
|
return sourceCollection.iterator().next();
|
||||||
|
} else {
|
||||||
|
GenericConverter converter = conversionService.getConverter(sourceElementType, targetType);
|
||||||
|
return converter.convert(sourceCollection.iterator().next(), sourceElementType, targetType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue