javadoc
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@971 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
5b0964b6d2
commit
5ed30b559a
|
|
@ -20,6 +20,10 @@ import org.springframework.core.convert.ConversionExecutor;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for converters that convert to and from collection types (arrays and java.util.Collection types)
|
||||||
|
* @author Keith Donald
|
||||||
|
*/
|
||||||
abstract class AbstractCollectionConverter implements ConversionExecutor {
|
abstract class AbstractCollectionConverter implements ConversionExecutor {
|
||||||
|
|
||||||
private ConversionService conversionService;
|
private ConversionService conversionService;
|
||||||
|
|
@ -43,10 +47,16 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The collection type to convert to.
|
||||||
|
*/
|
||||||
protected Class<?> getTargetCollectionType() {
|
protected Class<?> getTargetCollectionType() {
|
||||||
return targetCollectionType.getType();
|
return targetCollectionType.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of elements in the target collection.
|
||||||
|
*/
|
||||||
protected Class<?> getTargetElementType() {
|
protected Class<?> getTargetElementType() {
|
||||||
return targetCollectionType.getElementType();
|
return targetCollectionType.getElementType();
|
||||||
}
|
}
|
||||||
|
|
@ -55,6 +65,10 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
|
||||||
return conversionService;
|
return conversionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The converter to use to convert elements to the {@link #getTargetElementType()}.
|
||||||
|
* Returns {@link NoOpConversionExecutor#INSTANCE} if no converter could be eagerly resolved from type descriptor metadata.
|
||||||
|
*/
|
||||||
protected ConversionExecutor getElementConverter() {
|
protected ConversionExecutor getElementConverter() {
|
||||||
return elementConverter;
|
return elementConverter;
|
||||||
}
|
}
|
||||||
|
|
@ -67,6 +81,12 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override to perform collection conversion
|
||||||
|
* @param sourceCollection the source collection to convert from, an instance of sourceCollectionType, which must be either an array or java.util.Collection type
|
||||||
|
* @return the converted target collection, an instance of targetCollectionType
|
||||||
|
* @throws Exception an exception occurred during the conversion
|
||||||
|
*/
|
||||||
protected abstract Object doExecute(Object sourceCollection) throws Exception;
|
protected abstract Object doExecute(Object sourceCollection) throws Exception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -19,19 +19,12 @@ import java.lang.reflect.Array;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionExecutor;
|
import org.springframework.core.convert.ConversionExecutor;
|
||||||
import org.springframework.core.convert.ConversionService;
|
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Special converter that converts from a source array to a target collection. Supports the selection of an
|
* Special converter that converts from a source array to a target collection. Supports the selection of an
|
||||||
* "approximate" collection implementation when a target collection interface such as <code>List.class</code> is
|
* "approximate" collection implementation when a target collection interface such as <code>List.class</code> is
|
||||||
* specified. Supports type conversion of array elements when a concrete parameterized collection class is provided,
|
* specified. Supports type conversion of array elements when a parameterized target collection type descriptor is provided.
|
||||||
* such as <code>IntegerList<Integer>.class</code>.
|
|
||||||
*
|
|
||||||
* Note that type erasure prevents arbitrary access to generic collection element type information at runtime,
|
|
||||||
* preventing the ability to convert elements for collections declared as properties.
|
|
||||||
*
|
|
||||||
* Mainly used internally by {@link ConversionService} implementations.
|
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -23,23 +23,31 @@ import java.util.Set;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Util code shared by collection converters extending from {@link AbstractCollectionConverter}.
|
||||||
|
*/
|
||||||
class CollectionConversionUtils {
|
class CollectionConversionUtils {
|
||||||
|
|
||||||
public static Class<?> getImpl(Class<?> targetClass) {
|
/**
|
||||||
if (targetClass.isInterface()) {
|
* Get the java.util.Collection implementation class that should be used for the given target collection type.
|
||||||
if (List.class.equals(targetClass)) {
|
* @param targetCollectionType the target collection type, may be an interface
|
||||||
|
* @return the collection impl to use
|
||||||
|
*/
|
||||||
|
public static Class<?> getImpl(Class<?> targetCollectionType) {
|
||||||
|
if (targetCollectionType.isInterface()) {
|
||||||
|
if (List.class.equals(targetCollectionType)) {
|
||||||
return ArrayList.class;
|
return ArrayList.class;
|
||||||
} else if (Set.class.equals(targetClass)) {
|
} else if (Set.class.equals(targetCollectionType)) {
|
||||||
return LinkedHashSet.class;
|
return LinkedHashSet.class;
|
||||||
} else if (SortedSet.class.equals(targetClass)) {
|
} else if (SortedSet.class.equals(targetCollectionType)) {
|
||||||
return TreeSet.class;
|
return TreeSet.class;
|
||||||
} else if (Collection.class.equals(targetClass)) {
|
} else if (Collection.class.equals(targetCollectionType)) {
|
||||||
return ArrayList.class;
|
return ArrayList.class;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Unsupported collection interface [" + targetClass.getName() + "]");
|
throw new IllegalArgumentException("Unsupported collection interface [" + targetCollectionType.getName() + "]");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return targetClass;
|
return targetCollectionType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,19 +20,10 @@ import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionExecutor;
|
import org.springframework.core.convert.ConversionExecutor;
|
||||||
import org.springframework.core.convert.ConversionService;
|
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Special converter that converts from a source array to a target collection. Supports the selection of an
|
* Special converter that converts from target collection to a source array.
|
||||||
* "approximate" collection implementation when a target collection interface such as <code>List.class</code> is
|
|
||||||
* specified. Supports type conversion of array elements when a concrete parameterized collection class is provided,
|
|
||||||
* such as <code>IntegerList<Integer>.class</code>.
|
|
||||||
*
|
|
||||||
* Note that type erasure prevents arbitrary access to generic collection element type information at runtime,
|
|
||||||
* preventing the ability to convert elements for collections declared as properties.
|
|
||||||
*
|
|
||||||
* Mainly used internally by {@link ConversionService} implementations.
|
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,9 @@ package org.springframework.core.convert.service;
|
||||||
import org.springframework.core.convert.ConversionExecutionException;
|
import org.springframework.core.convert.ConversionExecutionException;
|
||||||
import org.springframework.core.convert.ConversionExecutor;
|
import org.springframework.core.convert.ConversionExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Conversion executor that does nothing. Access singleton at {@link #INSTANCE}.s
|
||||||
|
*/
|
||||||
class NoOpConversionExecutor implements ConversionExecutor {
|
class NoOpConversionExecutor implements ConversionExecutor {
|
||||||
|
|
||||||
public static final ConversionExecutor INSTANCE = new NoOpConversionExecutor();
|
public static final ConversionExecutor INSTANCE = new NoOpConversionExecutor();
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,10 @@ package org.springframework.core.convert.service;
|
||||||
|
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A converter that does nothing. Access singleton at {@link #INSTANCE}.
|
||||||
|
* @author Keith Donald
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
class NoOpConverter implements Converter {
|
class NoOpConverter implements Converter {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,10 @@ package org.springframework.core.convert.service;
|
||||||
|
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A converter that reverses another converter.
|
||||||
|
* @author Keith Donald
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
class ReverseConverter implements Converter {
|
class ReverseConverter implements Converter {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,10 @@ package org.springframework.core.convert.service;
|
||||||
import org.springframework.core.convert.converter.SuperConverter;
|
import org.springframework.core.convert.converter.SuperConverter;
|
||||||
import org.springframework.core.convert.converter.SuperTwoWayConverter;
|
import org.springframework.core.convert.converter.SuperTwoWayConverter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A super converter that reverses another super converter.
|
||||||
|
* @author Keith Donald
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
class ReverseSuperConverter implements SuperConverter {
|
class ReverseSuperConverter implements SuperConverter {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,10 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.core.style.ToStringCreator;
|
import org.springframework.core.style.ToStringCreator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default conversion executor implementation for converters.
|
||||||
|
* @author Keith Donald
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
class StaticConversionExecutor implements ConversionExecutor {
|
class StaticConversionExecutor implements ConversionExecutor {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,10 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.core.convert.converter.SuperConverter;
|
import org.springframework.core.convert.converter.SuperConverter;
|
||||||
import org.springframework.core.style.ToStringCreator;
|
import org.springframework.core.style.ToStringCreator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default conversion executor implementation for super converters.
|
||||||
|
* @author Keith Donald
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
class StaticSuperConversionExecutor implements ConversionExecutor {
|
class StaticSuperConversionExecutor implements ConversionExecutor {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue