polish
This commit is contained in:
parent
374ea05028
commit
53f3d0b755
|
|
@ -24,6 +24,7 @@ import org.springframework.core.NestedRuntimeException;
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
public abstract class ConversionException extends NestedRuntimeException {
|
public abstract class ConversionException extends NestedRuntimeException {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ package org.springframework.core.convert;
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
public final class ConversionFailedException extends ConversionException {
|
public final class ConversionFailedException extends ConversionException {
|
||||||
|
|
||||||
private final TypeDescriptor sourceType;
|
private final TypeDescriptor sourceType;
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ package org.springframework.core.convert;
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
public final class ConverterNotFoundException extends ConversionException {
|
public final class ConverterNotFoundException extends ConversionException {
|
||||||
|
|
||||||
private final TypeDescriptor sourceType;
|
private final TypeDescriptor sourceType;
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ final class ArrayToCollectionConverter implements GenericConverter {
|
||||||
this.conversionService = conversionService;
|
this.conversionService = conversionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ final class CollectionToArrayConverter implements GenericConverter {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Collection sourceCollection = (Collection) source;
|
Collection<?> sourceCollection = (Collection<?>) source;
|
||||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||||
if (sourceElementType == TypeDescriptor.NULL) {
|
if (sourceElementType == TypeDescriptor.NULL) {
|
||||||
sourceElementType = getElementType(sourceCollection);
|
sourceElementType = getElementType(sourceCollection);
|
||||||
|
|
@ -53,7 +53,7 @@ final class CollectionToArrayConverter implements GenericConverter {
|
||||||
Object array = Array.newInstance(targetElementType.getType(), sourceCollection.size());
|
Object array = Array.newInstance(targetElementType.getType(), sourceCollection.size());
|
||||||
int i = 0;
|
int i = 0;
|
||||||
if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetElementType)) {
|
if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetElementType)) {
|
||||||
for (Iterator it = sourceCollection.iterator(); it.hasNext(); i++) {
|
for (Iterator<?> it = sourceCollection.iterator(); it.hasNext(); i++) {
|
||||||
Array.set(array, i, it.next());
|
Array.set(array, i, it.next());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ final class CollectionToArrayConverter implements GenericConverter {
|
||||||
if (converter == null) {
|
if (converter == null) {
|
||||||
throw new ConverterNotFoundException(sourceElementType, targetElementType);
|
throw new ConverterNotFoundException(sourceElementType, targetElementType);
|
||||||
}
|
}
|
||||||
for (Iterator it = sourceCollection.iterator(); it.hasNext(); i++) {
|
for (Iterator<?> it = sourceCollection.iterator(); it.hasNext(); i++) {
|
||||||
Object sourceElement = it.next();
|
Object sourceElement = it.next();
|
||||||
Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetElementType);
|
Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetElementType);
|
||||||
Array.set(array, i, targetElement);
|
Array.set(array, i, targetElement);
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ final class CollectionToCollectionConverter implements GenericConverter {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Collection sourceCollection = (Collection) source;
|
Collection<?> sourceCollection = (Collection<?>) source;
|
||||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||||
if (sourceElementType == TypeDescriptor.NULL) {
|
if (sourceElementType == TypeDescriptor.NULL) {
|
||||||
sourceElementType = getElementType(sourceCollection);
|
sourceElementType = getElementType(sourceCollection);
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ final class CollectionToMapConverter implements GenericConverter {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Collection sourceCollection = (Collection) source;
|
Collection<?> sourceCollection = (Collection<?>) source;
|
||||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||||
if (sourceElementType == TypeDescriptor.NULL) {
|
if (sourceElementType == TypeDescriptor.NULL) {
|
||||||
sourceElementType = getElementType(sourceCollection);
|
sourceElementType = getElementType(sourceCollection);
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ final class CollectionToObjectConverter implements GenericConverter {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Collection sourceCollection = (Collection) source;
|
Collection<?> sourceCollection = (Collection<?>) source;
|
||||||
if (sourceCollection.size() == 0) {
|
if (sourceCollection.size() == 0) {
|
||||||
if (targetType.typeEquals(String.class)) {
|
if (targetType.typeEquals(String.class)) {
|
||||||
return "";
|
return "";
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ final class ConversionUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TypeDescriptor getElementType(Collection collection) {
|
public static TypeDescriptor getElementType(Collection<?> collection) {
|
||||||
for (Object element : collection) {
|
for (Object element : collection) {
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
return TypeDescriptor.valueOf(element.getClass());
|
return TypeDescriptor.valueOf(element.getClass());
|
||||||
|
|
@ -45,11 +45,12 @@ final class ConversionUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List asList(Object array) {
|
public static List<?> asList(Object array) {
|
||||||
return array != null ? new ArrayList(array) : null;
|
return array != null ? new ArrayList(array) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ArrayList extends AbstractList implements RandomAccess, java.io.Serializable {
|
@SuppressWarnings("serial")
|
||||||
|
private static class ArrayList extends AbstractList<Object> implements RandomAccess, java.io.Serializable {
|
||||||
|
|
||||||
private Object array;
|
private Object array;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ final class MapToCollectionConverter implements GenericConverter {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Map sourceMap = (Map) source;
|
Map<?, ?> sourceMap = (Map<?, ?>) source;
|
||||||
TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor();
|
TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor();
|
||||||
TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor();
|
TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor();
|
||||||
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
|
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
|
||||||
|
|
@ -58,7 +58,7 @@ final class MapToCollectionConverter implements GenericConverter {
|
||||||
targetElementType, keysCompatible, valuesCompatible, this.conversionService);
|
targetElementType, keysCompatible, valuesCompatible, this.conversionService);
|
||||||
if (targetElementType.getType().equals(String.class)) {
|
if (targetElementType.getType().equals(String.class)) {
|
||||||
for (Object entry : sourceMap.entrySet()) {
|
for (Object entry : sourceMap.entrySet()) {
|
||||||
Map.Entry mapEntry = (Map.Entry) entry;
|
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||||
String property = converter.convertKey(mapEntry.getKey()) + "="
|
String property = converter.convertKey(mapEntry.getKey()) + "="
|
||||||
+ converter.convertValue(mapEntry.getValue());
|
+ converter.convertValue(mapEntry.getValue());
|
||||||
target.add(property);
|
target.add(property);
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ final class MapToMapConverter implements GenericConverter {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Map sourceMap = (Map) source;
|
Map<?, ?> sourceMap = (Map<?, ?>) source;
|
||||||
TypeDescriptor targetKeyType = targetType.getMapKeyTypeDescriptor();
|
TypeDescriptor targetKeyType = targetType.getMapKeyTypeDescriptor();
|
||||||
TypeDescriptor targetValueType = targetType.getMapValueTypeDescriptor();
|
TypeDescriptor targetValueType = targetType.getMapValueTypeDescriptor();
|
||||||
if (targetKeyType == TypeDescriptor.NULL && targetValueType == TypeDescriptor.NULL) {
|
if (targetKeyType == TypeDescriptor.NULL && targetValueType == TypeDescriptor.NULL) {
|
||||||
|
|
@ -77,11 +77,11 @@ final class MapToMapConverter implements GenericConverter {
|
||||||
return targetMap;
|
return targetMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypeDescriptor[] getMapEntryTypes(Map sourceMap) {
|
private TypeDescriptor[] getMapEntryTypes(Map<?, ?> sourceMap) {
|
||||||
Class keyType = null;
|
Class<?> keyType = null;
|
||||||
Class valueType = null;
|
Class<?> valueType = null;
|
||||||
for (Object entry : sourceMap.entrySet()) {
|
for (Object entry : sourceMap.entrySet()) {
|
||||||
Map.Entry mapEntry = (Map.Entry) entry;
|
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||||
Object key = mapEntry.getKey();
|
Object key = mapEntry.getKey();
|
||||||
if (keyType == null && key != null) {
|
if (keyType == null && key != null) {
|
||||||
keyType = key.getClass();
|
keyType = key.getClass();
|
||||||
|
|
@ -98,7 +98,7 @@ final class MapToMapConverter implements GenericConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private Map compatibleMapWithoutEntryConversion(Map source, TypeDescriptor targetType) {
|
private Map<?, ?> compatibleMapWithoutEntryConversion(Map<?, ?> source, TypeDescriptor targetType) {
|
||||||
if (targetType.getType().isAssignableFrom(source.getClass())) {
|
if (targetType.getType().isAssignableFrom(source.getClass())) {
|
||||||
return source;
|
return source;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ final class MapToObjectConverter implements GenericConverter {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Map sourceMap = (Map) source;
|
Map<?, ?> sourceMap = (Map<?, ?>) source;
|
||||||
if (sourceMap.size() == 0) {
|
if (sourceMap.size() == 0) {
|
||||||
if (targetType.typeEquals(String.class)) {
|
if (targetType.typeEquals(String.class)) {
|
||||||
return "";
|
return "";
|
||||||
|
|
@ -63,7 +63,7 @@ final class MapToObjectConverter implements GenericConverter {
|
||||||
StringBuilder string = new StringBuilder();
|
StringBuilder string = new StringBuilder();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object entry : sourceMap.entrySet()) {
|
for (Object entry : sourceMap.entrySet()) {
|
||||||
Map.Entry mapEntry = (Map.Entry) entry;
|
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
string.append(DELIMITER);
|
string.append(DELIMITER);
|
||||||
}
|
}
|
||||||
|
|
@ -78,7 +78,7 @@ final class MapToObjectConverter implements GenericConverter {
|
||||||
StringBuilder string = new StringBuilder();
|
StringBuilder string = new StringBuilder();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object entry : sourceMap.entrySet()) {
|
for (Object entry : sourceMap.entrySet()) {
|
||||||
Map.Entry mapEntry = (Map.Entry) entry;
|
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
string.append(DELIMITER);
|
string.append(DELIMITER);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ final class ObjectToCollectionConverter implements GenericConverter {
|
||||||
this.conversionService = conversionService;
|
this.conversionService = conversionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue