Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
6f74369c
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6f74369c
编写于
5月 15, 2009
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
polish
上级
acf17381
变更
24
显示空白变更内容
内联
并排
Showing
24 changed file
with
232 addition
and
219 deletion
+232
-219
org.springframework.core/src/main/java/org/springframework/core/convert/BindingPoint.java
...n/java/org/springframework/core/convert/BindingPoint.java
+9
-9
org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java
...ringframework/core/convert/ConversionFailedException.java
+3
-3
org.springframework.core/src/main/java/org/springframework/core/convert/TypeConverter.java
.../java/org/springframework/core/convert/TypeConverter.java
+23
-7
org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java
...ngframework/core/convert/converter/ConverterRegistry.java
+12
-0
org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java
...ork/core/convert/support/AbstractCollectionConverter.java
+9
-9
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java
...rg/springframework/core/convert/support/ArrayToArray.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java
...ringframework/core/convert/support/ArrayToCollection.java
+3
-3
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArray.java
...ringframework/core/convert/support/CollectionToArray.java
+4
-4
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollection.java
...ramework/core/convert/support/CollectionToCollection.java
+4
-4
org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionExecutor.java
...ingframework/core/convert/support/ConversionExecutor.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultTypeConverter.java
...gframework/core/convert/support/DefaultTypeConverter.java
+5
-3
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java
...gframework/core/convert/support/GenericTypeConverter.java
+28
-8
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java
...va/org/springframework/core/convert/support/MapToMap.java
+13
-13
org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java
...ramework/core/convert/support/NoOpConversionExecutor.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java
.../springframework/core/convert/support/ObjectToString.java
+1
-1
org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java
...mework/core/convert/support/StaticConversionExecutor.java
+8
-8
org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java
...org/springframework/core/convert/TypeDescriptorTests.java
+5
-5
org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToArrayTests.java
...ringframework/core/convert/support/ArrayToArrayTests.java
+4
-4
org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToCollectionTests.java
...ramework/core/convert/support/ArrayToCollectionTests.java
+12
-12
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToArrayTests.java
...ramework/core/convert/support/CollectionToArrayTests.java
+9
-9
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionTests.java
...ork/core/convert/support/CollectionToCollectionTests.java
+17
-17
org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultTypeConverterTests.java
...ework/core/convert/support/DefaultTypeConverterTests.java
+2
-2
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericTypeConverterTests.java
...ework/core/convert/support/GenericTypeConverterTests.java
+43
-80
org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapTests.java
...g/springframework/core/convert/support/MapToMapTests.java
+12
-12
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/
TypeDescriptor
.java
→
org.springframework.core/src/main/java/org/springframework/core/convert/
BindingPoint
.java
浏览文件 @
6f74369c
...
...
@@ -32,12 +32,12 @@ import org.springframework.util.Assert;
* @author Keith Donald
* @author Andy Clement
*/
public
class
TypeDescriptor
<
T
>
{
public
class
BindingPoint
<
T
>
{
/**
* Constant value typeDescriptor for the type of a null value
*/
public
final
static
TypeDescriptor
NULL_TYPE_DESCRIPTOR
=
new
TypeDescriptor
((
Class
<?>)
null
);
public
final
static
BindingPoint
NULL_TYPE_DESCRIPTOR
=
new
BindingPoint
((
Class
<?>)
null
);
private
MethodParameter
methodParameter
;
...
...
@@ -52,7 +52,7 @@ public class TypeDescriptor<T> {
* a Map or collection, where no additional binding metadata is available.
* @param type the actual type
*/
public
TypeDescriptor
(
Class
<?>
type
)
{
public
BindingPoint
(
Class
<?>
type
)
{
this
.
type
=
type
;
}
...
...
@@ -61,7 +61,7 @@ public class TypeDescriptor<T> {
* from a method parameter, such as a setter method argument.
* @param methodParameter the MethodParameter to wrap
*/
public
TypeDescriptor
(
MethodParameter
methodParameter
)
{
public
BindingPoint
(
MethodParameter
methodParameter
)
{
Assert
.
notNull
(
methodParameter
,
"MethodParameter must not be null"
);
this
.
methodParameter
=
methodParameter
;
}
...
...
@@ -70,7 +70,7 @@ public class TypeDescriptor<T> {
* Create a new descriptor for a field. Use this constructor when a bound value originates from a field.
* @param field the field to wrap
*/
public
TypeDescriptor
(
Field
field
)
{
public
BindingPoint
(
Field
field
)
{
Assert
.
notNull
(
field
,
"Field must not be null"
);
this
.
field
=
field
;
}
...
...
@@ -248,7 +248,7 @@ public class TypeDescriptor<T> {
* @param targetType the target type
* @return true if this type is assignable to the target
*/
public
boolean
isAssignableTo
(
TypeDescriptor
targetType
)
{
public
boolean
isAssignableTo
(
BindingPoint
targetType
)
{
return
targetType
.
getType
().
isAssignableFrom
(
getType
());
}
...
...
@@ -257,9 +257,9 @@ public class TypeDescriptor<T> {
* @param type the class
* @return the type descriptor
*/
public
static
TypeDescriptor
valueOf
(
Class
<?
extends
Object
>
type
)
{
public
static
<
T
>
BindingPoint
<
T
>
valueOf
(
Class
<
T
>
type
)
{
// TODO needs a cache for common type descriptors
return
new
TypeDescriptor
(
type
);
return
new
BindingPoint
<
T
>
(
type
);
}
/**
...
...
@@ -267,7 +267,7 @@ public class TypeDescriptor<T> {
* @param object the object
* @return the type descriptor
*/
public
static
TypeDescriptor
forObject
(
Object
object
)
{
public
static
BindingPoint
forObject
(
Object
object
)
{
if
(
object
==
null
)
{
return
NULL_TYPE_DESCRIPTOR
;
}
else
{
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java
→
org.springframework.core/src/main/java/org/springframework/core/convert/Conversion
Failed
Exception.java
浏览文件 @
6f74369c
...
...
@@ -22,7 +22,7 @@ import org.springframework.core.style.StylerUtils;
*
* @author Keith Donald
*/
public
class
ConversionException
extends
ConvertException
{
public
class
Conversion
Failed
Exception
extends
ConvertException
{
private
transient
Object
value
;
...
...
@@ -37,7 +37,7 @@ public class ConversionException extends ConvertException {
* @param targetType the value's target type
* @param cause the cause of the conversion failure
*/
public
ConversionException
(
Object
value
,
Class
<?>
sourceType
,
Class
<?>
targetType
,
Throwable
cause
)
{
public
Conversion
Failed
Exception
(
Object
value
,
Class
<?>
sourceType
,
Class
<?>
targetType
,
Throwable
cause
)
{
super
(
defaultMessage
(
value
,
sourceType
,
targetType
,
cause
),
cause
);
this
.
value
=
value
;
this
.
sourceType
=
sourceType
;
...
...
@@ -51,7 +51,7 @@ public class ConversionException extends ConvertException {
* @param targetType the value's target type
* @param message a descriptive message of what went wrong.
*/
public
ConversionException
(
Object
value
,
Class
<?>
sourceType
,
Class
<?>
targetType
,
String
message
)
{
public
Conversion
Failed
Exception
(
Object
value
,
Class
<?>
sourceType
,
Class
<?>
targetType
,
String
message
)
{
super
(
message
);
this
.
value
=
value
;
this
.
sourceType
=
sourceType
;
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/TypeConverter.java
浏览文件 @
6f74369c
...
...
@@ -18,7 +18,7 @@ package org.springframework.core.convert;
/**
* A service interface for type conversion. This is the entry point into the convert system.
* <p>
* Call {@link #convert(Object,
TypeDescriptor
)} to perform a thread-safe type conversion using
* Call {@link #convert(Object,
Class
)} to perform a thread-safe type conversion using
* this system.
*
* @author Keith Donald
...
...
@@ -28,19 +28,35 @@ public interface TypeConverter {
/**
* Returns true if objects of sourceType can be converted to targetType.
* @param source the source to convert from (may be null)
* @param targetType
context about
the target type to convert to
* @param targetType the target type to convert to
* @return true if a conversion can be performed, false if not
*/
boolean
canConvert
(
Class
<?>
sourceType
,
TypeDescriptor
<?>
targetType
);
boolean
canConvert
(
Class
<?>
sourceType
,
Class
<?>
targetType
);
/**
* Returns true if objects of sourceType can be converted to the type of the binding point.
* @param source the source to convert from (may be null)
* @param point context about the target type to convert to
* @return true if a conversion can be performed, false if not
*/
boolean
canConvert
(
Class
<?>
sourceType
,
BindingPoint
<?>
point
);
/**
* Convert the source to targetType.
* @param source the source to convert from (may be null)
* @param targetType context about the target type to convert to
* @return the converted object, an instance of {@link TypeDescriptor#getType()}</code>, or <code>null</code> if a null source
* was provided
* @param targetType the target type to convert to
* @return the converted object, an instance of targetType, or <code>null</code> if a null source was provided
* @throws ConvertException if an exception occurred
*/
<
S
,
T
>
T
convert
(
S
source
,
Class
<
T
>
targetType
);
/**
* Convert the source to type T needed by the binding point.
* @param source the source to convert from (may be null)
* @param point a binding point where a conversion is required
* @return the converted object, an instance of {@link BindingPoint#getType()}</code>, or <code>null</code> if a null source was provided
* @throws ConvertException if an exception occurred
*/
<
S
,
T
>
T
convert
(
S
source
,
TypeDescriptor
<
T
>
targetType
);
<
S
,
T
>
T
convert
(
S
source
,
BindingPoint
<
T
>
point
);
}
\ No newline at end of file
org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java
0 → 100644
浏览文件 @
6f74369c
package
org.springframework.core.convert.converter
;
public
interface
ConverterRegistry
{
void
addConverter
(
Converter
<?,
?>
converter
);
void
addConverterFactory
(
ConverterFactory
<?,
?>
converter
);
void
removeConverter
(
Converter
<?,
?>
converter
);
void
removeConverterFactory
(
Converter
<?,
?>
converter
);
}
\ No newline at end of file
org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java
浏览文件 @
6f74369c
...
...
@@ -15,8 +15,8 @@
*/
package
org.springframework.core.convert.support
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.Conversion
Failed
Exception
;
import
org.springframework.core.convert.
BindingPoint
;
/**
* Base class for converters that convert to and from collection types (arrays and java.util.Collection types)
...
...
@@ -24,22 +24,22 @@ import org.springframework.core.convert.TypeDescriptor;
*/
abstract
class
AbstractCollectionConverter
implements
ConversionExecutor
{
private
Generic
ConversionService
conversionService
;
private
Generic
TypeConverter
conversionService
;
private
ConversionExecutor
elementConverter
;
private
TypeDescriptor
sourceCollectionType
;
private
BindingPoint
sourceCollectionType
;
private
TypeDescriptor
targetCollectionType
;
private
BindingPoint
targetCollectionType
;
public
AbstractCollectionConverter
(
TypeDescriptor
sourceCollectionType
,
TypeDescriptor
targetCollectionType
,
GenericConversionService
conversionService
)
{
public
AbstractCollectionConverter
(
BindingPoint
sourceCollectionType
,
BindingPoint
targetCollectionType
,
GenericTypeConverter
conversionService
)
{
this
.
conversionService
=
conversionService
;
this
.
sourceCollectionType
=
sourceCollectionType
;
this
.
targetCollectionType
=
targetCollectionType
;
Class
<?>
sourceElementType
=
sourceCollectionType
.
getElementType
();
Class
<?>
targetElementType
=
targetCollectionType
.
getElementType
();
if
(
sourceElementType
!=
null
&&
targetElementType
!=
null
)
{
elementConverter
=
conversionService
.
getConversionExecutor
(
sourceElementType
,
TypeDescriptor
.
valueOf
(
targetElementType
));
elementConverter
=
conversionService
.
getConversionExecutor
(
sourceElementType
,
BindingPoint
.
valueOf
(
targetElementType
));
}
else
{
elementConverter
=
NoOpConversionExecutor
.
INSTANCE
;
}
...
...
@@ -59,7 +59,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
return
targetCollectionType
.
getElementType
();
}
protected
Generic
ConversionService
getConversionService
()
{
protected
Generic
TypeConverter
getConversionService
()
{
return
conversionService
;
}
...
...
@@ -75,7 +75,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
try
{
return
doExecute
(
source
);
}
catch
(
Exception
e
)
{
throw
new
ConversionException
(
source
,
sourceCollectionType
.
getType
(),
targetCollectionType
.
getType
(),
e
);
throw
new
Conversion
Failed
Exception
(
source
,
sourceCollectionType
.
getType
(),
targetCollectionType
.
getType
(),
e
);
}
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java
浏览文件 @
6f74369c
...
...
@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import
java.lang.reflect.Array
;
import
org.springframework.core.convert.TypeConverter
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
/**
* Special one-way converter that converts from a source array to a target array. Supports type conversion of the
...
...
@@ -29,7 +29,7 @@ import org.springframework.core.convert.TypeDescriptor;
*/
class
ArrayToArray
extends
AbstractCollectionConverter
{
public
ArrayToArray
(
TypeDescriptor
sourceArrayType
,
TypeDescriptor
targetArrayType
,
GenericConversionService
conversionService
)
{
public
ArrayToArray
(
BindingPoint
sourceArrayType
,
BindingPoint
targetArrayType
,
GenericTypeConverter
conversionService
)
{
super
(
sourceArrayType
,
targetArrayType
,
conversionService
);
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java
浏览文件 @
6f74369c
...
...
@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import
java.lang.reflect.Array
;
import
java.util.Collection
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
/**
* Special converter that converts from a source array to a target collection. Supports the selection of an
...
...
@@ -29,8 +29,8 @@ import org.springframework.core.convert.TypeDescriptor;
*/
class
ArrayToCollection
extends
AbstractCollectionConverter
{
public
ArrayToCollection
(
TypeDescriptor
sourceArrayType
,
TypeDescriptor
targetCollectionType
,
Generic
ConversionService
conversionService
)
{
public
ArrayToCollection
(
BindingPoint
sourceArrayType
,
BindingPoint
targetCollectionType
,
Generic
TypeConverter
conversionService
)
{
super
(
sourceArrayType
,
targetCollectionType
,
conversionService
);
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArray.java
浏览文件 @
6f74369c
...
...
@@ -19,7 +19,7 @@ import java.lang.reflect.Array;
import
java.util.Collection
;
import
java.util.Iterator
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
/**
* Special converter that converts from target collection to a source array.
...
...
@@ -28,8 +28,8 @@ import org.springframework.core.convert.TypeDescriptor;
*/
class
CollectionToArray
extends
AbstractCollectionConverter
{
public
CollectionToArray
(
TypeDescriptor
sourceArrayType
,
TypeDescriptor
targetCollectionType
,
Generic
ConversionService
conversionService
)
{
public
CollectionToArray
(
BindingPoint
sourceArrayType
,
BindingPoint
targetCollectionType
,
Generic
TypeConverter
conversionService
)
{
super
(
sourceArrayType
,
targetCollectionType
,
conversionService
);
}
...
...
@@ -52,7 +52,7 @@ class CollectionToArray extends AbstractCollectionConverter {
while
(
it
.
hasNext
())
{
Object
value
=
it
.
next
();
if
(
value
!=
null
)
{
elementConverter
=
getConversionService
().
getConversionExecutor
(
value
.
getClass
(),
TypeDescriptor
.
valueOf
(
getTargetElementType
()));
elementConverter
=
getConversionService
().
getConversionExecutor
(
value
.
getClass
(),
BindingPoint
.
valueOf
(
getTargetElementType
()));
break
;
}
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollection.java
浏览文件 @
6f74369c
...
...
@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import
java.util.Collection
;
import
java.util.Iterator
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
/**
* A converter that can convert from one collection type to another.
...
...
@@ -27,8 +27,8 @@ import org.springframework.core.convert.TypeDescriptor;
*/
class
CollectionToCollection
extends
AbstractCollectionConverter
{
public
CollectionToCollection
(
TypeDescriptor
sourceCollectionType
,
TypeDescriptor
targetCollectionType
,
Generic
ConversionService
conversionService
)
{
public
CollectionToCollection
(
BindingPoint
sourceCollectionType
,
BindingPoint
targetCollectionType
,
Generic
TypeConverter
conversionService
)
{
super
(
sourceCollectionType
,
targetCollectionType
,
conversionService
);
}
...
...
@@ -53,7 +53,7 @@ class CollectionToCollection extends AbstractCollectionConverter {
while
(
it
.
hasNext
())
{
Object
value
=
it
.
next
();
if
(
value
!=
null
)
{
elementConverter
=
getConversionService
().
getConversionExecutor
(
value
.
getClass
(),
TypeDescriptor
.
valueOf
(
getTargetElementType
()));
elementConverter
=
getConversionService
().
getConversionExecutor
(
value
.
getClass
(),
BindingPoint
.
valueOf
(
getTargetElementType
()));
break
;
}
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionExecutor.java
浏览文件 @
6f74369c
...
...
@@ -15,7 +15,7 @@
*/
package
org.springframework.core.convert.support
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.Conversion
Failed
Exception
;
/**
* A command parameterized with the information necessary to perform a conversion of a source input to a
...
...
@@ -29,7 +29,7 @@ public interface ConversionExecutor {
/**
* Convert the source.
* @param source the source to convert
* @throws ConversionException if an exception occurs during type conversion
* @throws Conversion
Failed
Exception if an exception occurs during type conversion
*/
public
Object
execute
(
Object
source
);
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/Default
ConversionService
.java
→
org.springframework.core/src/main/java/org/springframework/core/convert/support/Default
TypeConverter
.java
浏览文件 @
6f74369c
...
...
@@ -15,7 +15,6 @@
*/
package
org.springframework.core.convert.support
;
import
org.springframework.core.convert.support.StringToEnumFactory.StringToEnum
;
/**
* Default implementation of a conversion service. Will automatically register <i>from string</i> converters for
...
...
@@ -23,12 +22,12 @@ import org.springframework.core.convert.support.StringToEnumFactory.StringToEnum
*
* @author Keith Donald
*/
public
class
Default
ConversionService
extends
GenericConversionService
{
public
class
Default
TypeConverter
extends
GenericTypeConverter
{
/**
* Creates a new default conversion service, installing the default converters.
*/
public
Default
ConversionService
()
{
public
Default
TypeConverter
()
{
addDefaultConverters
();
}
...
...
@@ -49,6 +48,9 @@ public class DefaultConversionService extends GenericConversionService {
addConverter
(
new
StringToLocale
());
addConverter
(
new
NumberToCharacter
());
addConverter
(
new
ObjectToString
());
addConverterFactory
(
new
StringToEnumFactory
());
addConverterFactory
(
new
NumberToNumberFactory
());
addConverterFactory
(
new
CharacterToNumberFactory
());
}
}
\ No newline at end of file
org.springframework.core/src/main/java/org/springframework/core/convert/support/Generic
ConversionService
.java
→
org.springframework.core/src/main/java/org/springframework/core/convert/support/Generic
TypeConverter
.java
浏览文件 @
6f74369c
...
...
@@ -28,9 +28,11 @@ import java.util.Map;
import
org.springframework.core.GenericTypeResolver
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.core.convert.TypeConverter
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.core.convert.converter.ConverterFactory
;
import
org.springframework.core.convert.converter.ConverterInfo
;
import
org.springframework.core.convert.converter.ConverterRegistry
;
import
org.springframework.util.Assert
;
/**
...
...
@@ -43,7 +45,7 @@ import org.springframework.util.Assert;
* @author Keith Donald
*/
@SuppressWarnings
(
"unchecked"
)
public
class
Generic
ConversionService
implements
TypeConverter
{
public
class
Generic
TypeConverter
implements
TypeConverter
,
ConverterRegistry
{
/**
* An indexed map of Converters. Each Map.Entry key is a source class (S) that can be converted from. Each Map.Entry
...
...
@@ -83,9 +85,23 @@ public class GenericConversionService implements TypeConverter {
sourceMap
.
put
(
targetType
,
converter
);
}
public
void
addConverterFactory
(
ConverterFactory
<?,
?>
converter
)
{
}
public
void
removeConverter
(
Converter
<?,
?>
converter
)
{
}
public
void
removeConverterFactory
(
Converter
<?,
?>
converter
)
{
}
// implementing ConversionService
public
boolean
canConvert
(
Class
<?>
sourceType
,
TypeDescriptor
<?>
targetType
)
{
public
boolean
canConvert
(
Class
<?>
sourceType
,
Class
<?>
targetType
)
{
return
canConvert
(
sourceType
,
BindingPoint
.
valueOf
(
targetType
));
}
public
boolean
canConvert
(
Class
<?>
sourceType
,
BindingPoint
<?>
targetType
)
{
ConversionExecutor
executor
=
getConversionExecutor
(
sourceType
,
targetType
);
if
(
executor
!=
null
)
{
return
true
;
...
...
@@ -98,16 +114,20 @@ public class GenericConversionService implements TypeConverter {
}
}
public
Object
convert
(
Object
source
,
TypeDescriptor
targetType
)
{
public
<
S
,
T
>
T
convert
(
S
source
,
Class
<
T
>
targetType
)
{
return
convert
(
source
,
BindingPoint
.
valueOf
(
targetType
));
}
public
<
S
,
T
>
T
convert
(
S
source
,
BindingPoint
<
T
>
targetType
)
{
if
(
source
==
null
)
{
return
null
;
}
if
(
source
.
getClass
().
isAssignableFrom
(
targetType
.
getType
()))
{
return
source
;
return
(
T
)
source
;
}
ConversionExecutor
executor
=
getConversionExecutor
(
source
.
getClass
(),
targetType
);
if
(
executor
!=
null
)
{
return
executor
.
execute
(
source
);
return
(
T
)
executor
.
execute
(
source
);
}
else
{
if
(
parent
!=
null
)
{
return
parent
.
convert
(
source
,
targetType
);
...
...
@@ -119,11 +139,11 @@ public class GenericConversionService implements TypeConverter {
}
}
ConversionExecutor
getConversionExecutor
(
Class
sourceClass
,
TypeDescriptor
targetType
)
ConversionExecutor
getConversionExecutor
(
Class
sourceClass
,
BindingPoint
targetType
)
throws
ConverterNotFoundException
{
Assert
.
notNull
(
sourceClass
,
"The sourceType to convert from is required"
);
Assert
.
notNull
(
targetType
,
"The targetType to convert to is required"
);
TypeDescriptor
sourceType
=
TypeDescriptor
.
valueOf
(
sourceClass
);
BindingPoint
sourceType
=
BindingPoint
.
valueOf
(
sourceClass
);
if
(
sourceType
.
isArray
())
{
if
(
targetType
.
isArray
())
{
return
new
ArrayToArray
(
sourceType
,
targetType
,
this
);
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java
浏览文件 @
6f74369c
...
...
@@ -21,20 +21,21 @@ import java.util.Map;
import
java.util.SortedMap
;
import
java.util.TreeMap
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.Conversion
Failed
Exception
;
import
org.springframework.core.convert.
BindingPoint
;
/**
* Converts from one map to another map, with support for converting individual map elements based on generic type information.
* @author Keith Donald
*/
@SuppressWarnings
(
"unchecked"
)
class
MapToMap
implements
ConversionExecutor
{
private
TypeDescriptor
sourceType
;
private
BindingPoint
sourceType
;
private
TypeDescriptor
targetType
;
private
BindingPoint
targetType
;
private
Generic
ConversionService
conversionService
;
private
Generic
TypeConverter
conversionService
;
private
EntryConverter
entryConverter
;
...
...
@@ -44,7 +45,7 @@ class MapToMap implements ConversionExecutor {
* @param targetType the target map type
* @param conversionService the conversion service
*/
public
MapToMap
(
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
,
GenericConversionService
conversionService
)
{
public
MapToMap
(
BindingPoint
sourceType
,
BindingPoint
targetType
,
GenericTypeConverter
conversionService
)
{
this
.
sourceType
=
sourceType
;
this
.
targetType
=
targetType
;
this
.
conversionService
=
conversionService
;
...
...
@@ -54,17 +55,16 @@ class MapToMap implements ConversionExecutor {
private
EntryConverter
createEntryConverter
()
{
if
(
sourceType
.
isMapEntryTypeKnown
()
&&
targetType
.
isMapEntryTypeKnown
())
{
ConversionExecutor
keyConverter
=
conversionService
.
getConversionExecutor
(
sourceType
.
getMapKeyType
(),
TypeDescriptor
.
valueOf
(
targetType
.
getMapKeyType
()));
BindingPoint
.
valueOf
(
targetType
.
getMapKeyType
()));
ConversionExecutor
valueConverter
=
conversionService
.
getConversionExecutor
(
sourceType
.
getMapValueType
(),
TypeDescriptor
.
valueOf
(
targetType
.
getMapValueType
()));
BindingPoint
.
valueOf
(
targetType
.
getMapValueType
()));
return
new
EntryConverter
(
keyConverter
,
valueConverter
);
}
else
{
return
EntryConverter
.
NO_OP_INSTANCE
;
}
}
@SuppressWarnings
(
"unchecked"
)
public
Object
execute
(
Object
source
)
throws
ConversionException
{
public
Object
execute
(
Object
source
)
throws
ConversionFailedException
{
try
{
Map
map
=
(
Map
)
source
;
Map
targetMap
=
(
Map
)
getImpl
(
targetType
.
getType
()).
newInstance
();
...
...
@@ -76,7 +76,7 @@ class MapToMap implements ConversionExecutor {
}
return
targetMap
;
}
catch
(
Exception
e
)
{
throw
new
ConversionException
(
source
,
sourceType
.
getType
(),
targetType
.
getType
(),
e
);
throw
new
Conversion
Failed
Exception
(
source
,
sourceType
.
getType
(),
targetType
.
getType
(),
e
);
}
}
...
...
@@ -94,11 +94,11 @@ class MapToMap implements ConversionExecutor {
Object
key
=
entry
.
getKey
();
Object
value
=
entry
.
getValue
();
if
(
keyConverter
==
null
&&
key
!=
null
)
{
keyConverter
=
conversionService
.
getConversionExecutor
(
key
.
getClass
(),
TypeDescriptor
keyConverter
=
conversionService
.
getConversionExecutor
(
key
.
getClass
(),
BindingPoint
.
valueOf
(
targetKeyType
));
}
if
(
valueConverter
==
null
&&
value
!=
null
)
{
valueConverter
=
conversionService
.
getConversionExecutor
(
value
.
getClass
(),
TypeDescriptor
valueConverter
=
conversionService
.
getConversionExecutor
(
value
.
getClass
(),
BindingPoint
.
valueOf
(
targetValueType
));
}
if
(
keyConverter
!=
null
&&
valueConverter
!=
null
)
{
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java
浏览文件 @
6f74369c
...
...
@@ -15,7 +15,7 @@
*/
package
org.springframework.core.convert.support
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.Conversion
Failed
Exception
;
/**
* Conversion executor that does nothing. Access singleton at {@link #INSTANCE}.s
...
...
@@ -28,7 +28,7 @@ class NoOpConversionExecutor implements ConversionExecutor {
}
public
Object
execute
(
Object
source
)
throws
ConversionException
{
public
Object
execute
(
Object
source
)
throws
Conversion
Failed
Exception
{
return
source
;
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java
浏览文件 @
6f74369c
...
...
@@ -4,7 +4,7 @@ import org.springframework.core.convert.converter.Converter;
/**
* Simply calls {@link Object#toString()} to convert any object to a string.
* Used by the {@link Default
ConversionService
} as a fallback if there are no other explicit to string converters registered.
* Used by the {@link Default
TypeConverter
} as a fallback if there are no other explicit to string converters registered.
* @author Keith Donald
*/
public
class
ObjectToString
implements
Converter
<
Object
,
String
>
{
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java
浏览文件 @
6f74369c
...
...
@@ -15,8 +15,8 @@
*/
package
org.springframework.core.convert.support
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.Conversion
Failed
Exception
;
import
org.springframework.core.convert.
BindingPoint
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.core.style.ToStringCreator
;
...
...
@@ -27,30 +27,30 @@ import org.springframework.core.style.ToStringCreator;
@SuppressWarnings
(
"unchecked"
)
class
StaticConversionExecutor
implements
ConversionExecutor
{
private
final
TypeDescriptor
sourceType
;
private
final
BindingPoint
sourceType
;
private
final
TypeDescriptor
targetType
;
private
final
BindingPoint
targetType
;
private
final
Converter
converter
;
public
StaticConversionExecutor
(
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
,
Converter
converter
)
{
public
StaticConversionExecutor
(
BindingPoint
sourceType
,
BindingPoint
targetType
,
Converter
converter
)
{
this
.
sourceType
=
sourceType
;
this
.
targetType
=
targetType
;
this
.
converter
=
converter
;
}
public
Object
execute
(
Object
source
)
throws
ConversionException
{
public
Object
execute
(
Object
source
)
throws
Conversion
Failed
Exception
{
if
(
source
==
null
)
{
return
null
;
}
if
(
sourceType
!=
null
&&
!
sourceType
.
isInstance
(
source
))
{
throw
new
ConversionException
(
source
,
sourceType
.
getType
(),
targetType
.
getType
(),
"Source object "
throw
new
Conversion
Failed
Exception
(
source
,
sourceType
.
getType
(),
targetType
.
getType
(),
"Source object "
+
source
+
" to convert is expected to be an instance of ["
+
sourceType
.
getName
()
+
"]"
);
}
try
{
return
converter
.
convert
(
source
);
}
catch
(
Exception
e
)
{
throw
new
ConversionException
(
source
,
sourceType
.
getType
(),
targetType
.
getType
(),
e
);
throw
new
Conversion
Failed
Exception
(
source
,
sourceType
.
getType
(),
targetType
.
getType
(),
e
);
}
}
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java
浏览文件 @
6f74369c
...
...
@@ -34,13 +34,13 @@ public class TypeDescriptorTests {
@Test
public
void
testWrapperType
()
{
TypeDescriptor
desc
=
TypeDescriptor
.
valueOf
(
int
.
class
);
BindingPoint
desc
=
BindingPoint
.
valueOf
(
int
.
class
);
assertEquals
(
Integer
.
class
,
desc
.
getType
());
}
@Test
public
void
listDescriptors
()
throws
Exception
{
TypeDescriptor
typeDescriptor
=
new
TypeDescriptor
(
TypeDescriptorTests
.
class
.
getDeclaredField
(
"listOfString"
));
BindingPoint
typeDescriptor
=
new
BindingPoint
(
TypeDescriptorTests
.
class
.
getDeclaredField
(
"listOfString"
));
assertFalse
(
typeDescriptor
.
isArray
());
assertEquals
(
List
.
class
,
typeDescriptor
.
getType
());
assertEquals
(
String
.
class
,
typeDescriptor
.
getElementType
());
...
...
@@ -50,7 +50,7 @@ public class TypeDescriptorTests {
@Test
public
void
arrayTypeDescriptors
()
throws
Exception
{
TypeDescriptor
typeDescriptor
=
new
TypeDescriptor
(
TypeDescriptorTests
.
class
.
getDeclaredField
(
"intArray"
));
BindingPoint
typeDescriptor
=
new
BindingPoint
(
TypeDescriptorTests
.
class
.
getDeclaredField
(
"intArray"
));
assertTrue
(
typeDescriptor
.
isArray
());
assertEquals
(
Integer
.
TYPE
,
typeDescriptor
.
getElementType
());
assertEquals
(
"int[]"
,
typeDescriptor
.
asString
());
...
...
@@ -58,14 +58,14 @@ public class TypeDescriptorTests {
@Test
public
void
buildingArrayTypeDescriptors
()
throws
Exception
{
TypeDescriptor
typeDescriptor
=
new
TypeDescriptor
(
new
int
[
0
].
getClass
());
BindingPoint
typeDescriptor
=
new
BindingPoint
(
new
int
[
0
].
getClass
());
assertTrue
(
typeDescriptor
.
isArray
());
assertEquals
(
Integer
.
TYPE
,
typeDescriptor
.
getElementType
());
}
@Test
public
void
complexTypeDescriptors
()
throws
Exception
{
TypeDescriptor
typeDescriptor
=
new
TypeDescriptor
(
TypeDescriptorTests
.
class
.
getDeclaredField
(
"arrayOfListOfString"
));
BindingPoint
typeDescriptor
=
new
BindingPoint
(
TypeDescriptorTests
.
class
.
getDeclaredField
(
"arrayOfListOfString"
));
assertTrue
(
typeDescriptor
.
isArray
());
assertEquals
(
List
.
class
,
typeDescriptor
.
getElementType
());
// TODO asc notice that the type of the list elements is lost: typeDescriptor.getElementType() should return a TypeDescriptor
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToArrayTests.java
浏览文件 @
6f74369c
...
...
@@ -3,16 +3,16 @@ package org.springframework.core.convert.support;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
org.junit.Test
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
import
org.springframework.core.convert.support.ArrayToArray
;
import
org.springframework.core.convert.support.Default
ConversionService
;
import
org.springframework.core.convert.support.Default
TypeConverter
;
public
class
ArrayToArrayTests
{
@Test
public
void
testArrayToArrayConversion
()
{
Default
ConversionService
service
=
new
DefaultConversionService
();
ArrayToArray
c
=
new
ArrayToArray
(
TypeDescriptor
.
valueOf
(
String
[].
class
),
TypeDescriptor
.
valueOf
(
Integer
[].
class
),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
ArrayToArray
c
=
new
ArrayToArray
(
BindingPoint
.
valueOf
(
String
[].
class
),
BindingPoint
.
valueOf
(
Integer
[].
class
),
service
);
Integer
[]
result
=
(
Integer
[])
c
.
execute
(
new
String
[]
{
"1"
,
"2"
,
"3"
});
assertEquals
(
new
Integer
(
1
),
result
[
0
]);
assertEquals
(
new
Integer
(
2
),
result
[
1
]);
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToCollectionTests.java
浏览文件 @
6f74369c
...
...
@@ -9,16 +9,16 @@ import java.util.Set;
import
java.util.SortedSet
;
import
org.junit.Test
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
import
org.springframework.core.convert.support.ArrayToCollection
;
import
org.springframework.core.convert.support.Default
ConversionService
;
import
org.springframework.core.convert.support.Default
TypeConverter
;
public
class
ArrayToCollectionTests
{
@Test
public
void
testArrayToCollectionConversion
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
ArrayToCollection
c
=
new
ArrayToCollection
(
TypeDescriptor
.
valueOf
(
String
[].
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"bindTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
ArrayToCollection
c
=
new
ArrayToCollection
(
BindingPoint
.
valueOf
(
String
[].
class
),
new
BindingPoint
(
getClass
().
getField
(
"bindTarget"
)),
service
);
List
result
=
(
List
)
c
.
execute
(
new
String
[]
{
"1"
,
"2"
,
"3"
});
assertEquals
(
new
Integer
(
1
),
result
.
get
(
0
));
assertEquals
(
new
Integer
(
2
),
result
.
get
(
1
));
...
...
@@ -27,32 +27,32 @@ public class ArrayToCollectionTests {
@Test
public
void
testArrayToSetConversion
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
ArrayToCollection
c
=
new
ArrayToCollection
(
TypeDescriptor
.
valueOf
(
String
[].
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"setTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
ArrayToCollection
c
=
new
ArrayToCollection
(
BindingPoint
.
valueOf
(
String
[].
class
),
new
BindingPoint
(
getClass
().
getField
(
"setTarget"
)),
service
);
Set
result
=
(
Set
)
c
.
execute
(
new
String
[]
{
"1"
});
assertEquals
(
"1"
,
result
.
iterator
().
next
());
}
@Test
public
void
testArrayToSortedSetConversion
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
ArrayToCollection
c
=
new
ArrayToCollection
(
TypeDescriptor
.
valueOf
(
String
[].
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"sortedSetTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
ArrayToCollection
c
=
new
ArrayToCollection
(
BindingPoint
.
valueOf
(
String
[].
class
),
new
BindingPoint
(
getClass
().
getField
(
"sortedSetTarget"
)),
service
);
SortedSet
result
=
(
SortedSet
)
c
.
execute
(
new
String
[]
{
"1"
});
assertEquals
(
new
Integer
(
1
),
result
.
iterator
().
next
());
}
@Test
public
void
testArrayToCollectionImplConversion
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
ArrayToCollection
c
=
new
ArrayToCollection
(
TypeDescriptor
.
valueOf
(
String
[].
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"implTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
ArrayToCollection
c
=
new
ArrayToCollection
(
BindingPoint
.
valueOf
(
String
[].
class
),
new
BindingPoint
(
getClass
().
getField
(
"implTarget"
)),
service
);
LinkedList
result
=
(
LinkedList
)
c
.
execute
(
new
String
[]
{
"1"
});
assertEquals
(
"1"
,
result
.
iterator
().
next
());
}
@Test
public
void
testArrayToNonGenericCollectionConversionNullElement
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
ArrayToCollection
c
=
new
ArrayToCollection
(
TypeDescriptor
.
valueOf
(
String
[].
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"listTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
ArrayToCollection
c
=
new
ArrayToCollection
(
BindingPoint
.
valueOf
(
String
[].
class
),
new
BindingPoint
(
getClass
().
getField
(
"listTarget"
)),
service
);
List
result
=
(
List
)
c
.
execute
(
new
Integer
[]
{
null
,
new
Integer
(
1
)
});
assertEquals
(
null
,
result
.
get
(
0
));
assertEquals
(
new
Integer
(
1
),
result
.
get
(
1
));
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToArrayTests.java
浏览文件 @
6f74369c
...
...
@@ -6,17 +6,17 @@ import java.util.ArrayList;
import
java.util.Collection
;
import
org.junit.Test
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
import
org.springframework.core.convert.support.CollectionToArray
;
import
org.springframework.core.convert.support.Default
ConversionService
;
import
org.springframework.core.convert.support.Default
TypeConverter
;
public
class
CollectionToArrayTests
{
@Test
public
void
testCollectionToArrayConversion
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToArray
c
=
new
CollectionToArray
(
new
TypeDescriptor
(
getClass
().
getField
(
"bindTarget"
)),
TypeDescriptor
.
valueOf
(
Integer
[].
class
),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToArray
c
=
new
CollectionToArray
(
new
BindingPoint
(
getClass
().
getField
(
"bindTarget"
)),
BindingPoint
.
valueOf
(
Integer
[].
class
),
service
);
bindTarget
.
add
(
"1"
);
bindTarget
.
add
(
"2"
);
bindTarget
.
add
(
"3"
);
...
...
@@ -28,8 +28,8 @@ public class CollectionToArrayTests {
@Test
public
void
testCollectionToArrayConversionNoGenericInfo
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToArray
c
=
new
CollectionToArray
(
TypeDescriptor
.
valueOf
(
Collection
.
class
),
TypeDescriptor
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToArray
c
=
new
CollectionToArray
(
BindingPoint
.
valueOf
(
Collection
.
class
),
BindingPoint
.
valueOf
(
Integer
[].
class
),
service
);
bindTarget
.
add
(
"1"
);
bindTarget
.
add
(
"2"
);
...
...
@@ -42,8 +42,8 @@ public class CollectionToArrayTests {
@Test
public
void
testCollectionToArrayConversionNoGenericInfoNullElement
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToArray
c
=
new
CollectionToArray
(
TypeDescriptor
.
valueOf
(
Collection
.
class
),
TypeDescriptor
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToArray
c
=
new
CollectionToArray
(
BindingPoint
.
valueOf
(
Collection
.
class
),
BindingPoint
.
valueOf
(
Integer
[].
class
),
service
);
bindTarget
.
add
(
null
);
bindTarget
.
add
(
"1"
);
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionTests.java
浏览文件 @
6f74369c
...
...
@@ -8,17 +8,17 @@ import java.util.Collection;
import
java.util.List
;
import
org.junit.Test
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.
BindingPoint
;
import
org.springframework.core.convert.support.CollectionToCollection
;
import
org.springframework.core.convert.support.Default
ConversionService
;
import
org.springframework.core.convert.support.Default
TypeConverter
;
public
class
CollectionToCollectionTests
{
@Test
public
void
testCollectionToCollectionConversion
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToCollection
c
=
new
CollectionToCollection
(
new
TypeDescriptor
(
getClass
().
getField
(
"bindTarget"
)),
new
TypeDescriptor
(
getClass
().
getField
(
"integerTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToCollection
c
=
new
CollectionToCollection
(
new
BindingPoint
(
getClass
().
getField
(
"bindTarget"
)),
new
BindingPoint
(
getClass
().
getField
(
"integerTarget"
)),
service
);
bindTarget
.
add
(
"1"
);
bindTarget
.
add
(
"2"
);
bindTarget
.
add
(
"3"
);
...
...
@@ -30,9 +30,9 @@ public class CollectionToCollectionTests {
@Test
public
void
testCollectionToCollectionConversionNoGenericInfo
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToCollection
c
=
new
CollectionToCollection
(
TypeDescriptor
.
valueOf
(
Collection
.
class
),
TypeDescriptor
.
valueOf
(
List
.
class
),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToCollection
c
=
new
CollectionToCollection
(
BindingPoint
.
valueOf
(
Collection
.
class
),
BindingPoint
.
valueOf
(
List
.
class
),
service
);
bindTarget
.
add
(
"1"
);
bindTarget
.
add
(
"2"
);
bindTarget
.
add
(
"3"
);
...
...
@@ -44,9 +44,9 @@ public class CollectionToCollectionTests {
@Test
public
void
testCollectionToCollectionConversionNoGenericInfoSource
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToCollection
c
=
new
CollectionToCollection
(
TypeDescriptor
.
valueOf
(
Collection
.
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"integerTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToCollection
c
=
new
CollectionToCollection
(
BindingPoint
.
valueOf
(
Collection
.
class
),
new
BindingPoint
(
getClass
().
getField
(
"integerTarget"
)),
service
);
bindTarget
.
add
(
"1"
);
bindTarget
.
add
(
"2"
);
bindTarget
.
add
(
"3"
);
...
...
@@ -58,9 +58,9 @@ public class CollectionToCollectionTests {
@Test
public
void
testCollectionToCollectionConversionNoGenericInfoSourceNullValues
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToCollection
c
=
new
CollectionToCollection
(
TypeDescriptor
.
valueOf
(
Collection
.
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"integerTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToCollection
c
=
new
CollectionToCollection
(
BindingPoint
.
valueOf
(
Collection
.
class
),
new
BindingPoint
(
getClass
().
getField
(
"integerTarget"
)),
service
);
bindTarget
.
add
(
null
);
bindTarget
.
add
(
"1"
);
bindTarget
.
add
(
"2"
);
...
...
@@ -76,9 +76,9 @@ public class CollectionToCollectionTests {
@Test
public
void
testCollectionToCollectionConversionNoGenericInfoSourceEmpty
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
CollectionToCollection
c
=
new
CollectionToCollection
(
TypeDescriptor
.
valueOf
(
Collection
.
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"integerTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
CollectionToCollection
c
=
new
CollectionToCollection
(
BindingPoint
.
valueOf
(
Collection
.
class
),
new
BindingPoint
(
getClass
().
getField
(
"integerTarget"
)),
service
);
List
result
=
(
List
)
c
.
execute
(
bindTarget
);
assertTrue
(
result
.
isEmpty
());
}
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/
converters/Default
ConverterTests.java
→
org.springframework.core/src/test/java/org/springframework/core/convert/
support/DefaultType
ConverterTests.java
浏览文件 @
6f74369c
package
org.springframework.core.convert.
converters
;
package
org.springframework.core.convert.
support
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
fail
;
...
...
@@ -28,7 +28,7 @@ import org.springframework.core.convert.support.StringToShort;
/**
* Tests for the default converters in the converters package.
s */
public
class
DefaultConverterTests
{
public
class
Default
Type
ConverterTests
{
@Test
public
void
testStringToByte
()
throws
Exception
{
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/Generic
ConversionService
Tests.java
→
org.springframework.core/src/test/java/org/springframework/core/convert/support/Generic
TypeConverter
Tests.java
浏览文件 @
6f74369c
...
...
@@ -21,7 +21,6 @@ import static junit.framework.Assert.fail;
import
java.util.AbstractList
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
...
...
@@ -29,43 +28,35 @@ import java.util.Map;
import
org.junit.Ignore
;
import
org.junit.Test
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.BindingPoint
;
import
org.springframework.core.convert.ConversionFailedException
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.core.convert.TypeDescriptor
;
import
org.springframework.core.convert.converter.Converter
;
public
class
Generic
ConversionService
Tests
{
public
class
Generic
TypeConverter
Tests
{
private
Generic
ConversionService
service
=
new
GenericConversionService
();
private
Generic
TypeConverter
converter
=
new
GenericTypeConverter
();
@Test
public
void
executeConversion
()
{
service
.
addConverter
(
new
StringToInteger
());
assertEquals
(
new
Integer
(
3
),
service
.
convert
(
"3"
,
type
(
Integer
.
class
)
));
converter
.
addConverter
(
new
StringToInteger
());
assertEquals
(
new
Integer
(
3
),
converter
.
convert
(
"3"
,
Integer
.
class
));
}
@Test
public
void
executeConversionNullSource
()
{
assertEquals
(
null
,
service
.
convert
(
null
,
type
(
Integer
.
class
)
));
assertEquals
(
null
,
converter
.
convert
(
null
,
Integer
.
class
));
}
@Test
public
void
executeCompatibleSource
()
{
assertEquals
(
false
,
service
.
convert
(
false
,
type
(
boolean
.
class
)
));
assertEquals
(
Boolean
.
FALSE
,
converter
.
convert
(
false
,
boolean
.
class
));
}
@Test
public
void
converterConvert
()
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
.
class
,
type
(
Integer
.
class
));
Integer
three
=
(
Integer
)
executor
.
execute
(
"3"
);
assertEquals
(
3
,
three
.
intValue
());
}
@Test
public
void
convertExecutorNotFound
()
{
public
void
converterNotFound
()
{
try
{
service
.
convert
(
"3"
,
type
(
Integer
.
class
)
);
converter
.
convert
(
"3"
,
Integer
.
class
);
fail
(
"Should have thrown an exception"
);
}
catch
(
ConverterNotFoundException
e
)
{
}
...
...
@@ -74,14 +65,10 @@ public class GenericConversionServiceTests {
@Test
public
void
addConverterNoSourceTargetClassInfoAvailable
()
{
try
{
service
.
addConverter
(
new
Converter
()
{
converter
.
addConverter
(
new
Converter
()
{
public
Object
convert
(
Object
source
)
throws
Exception
{
return
source
;
}
public
Object
convertBack
(
Object
target
)
throws
Exception
{
return
target
;
}
});
fail
(
"Should have failed"
);
}
catch
(
IllegalArgumentException
e
)
{
...
...
@@ -91,51 +78,40 @@ public class GenericConversionServiceTests {
@Test
public
void
convertNull
()
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
.
class
,
type
(
Integer
.
class
));
assertNull
(
executor
.
execute
(
null
));
assertNull
(
converter
.
convert
(
null
,
Integer
.
class
));
}
@Test
public
void
convertWrongTypeArgument
()
{
service
.
addConverter
(
new
StringToInteger
());
converter
.
addConverter
(
new
StringToInteger
());
try
{
service
.
convert
(
"BOGUS"
,
type
(
Integer
.
class
)
);
converter
.
convert
(
"BOGUS"
,
Integer
.
class
);
fail
(
"Should have failed"
);
}
catch
(
ConversionException
e
)
{
}
catch
(
Conversion
Failed
Exception
e
)
{
}
}
@Test
public
void
convertSuperSourceType
()
{
service
.
addConverter
(
new
Converter
<
CharSequence
,
Integer
>()
{
converter
.
addConverter
(
new
Converter
<
CharSequence
,
Integer
>()
{
public
Integer
convert
(
CharSequence
source
)
throws
Exception
{
return
Integer
.
valueOf
(
source
.
toString
());
}
public
CharSequence
convertBack
(
Integer
target
)
throws
Exception
{
return
target
.
toString
();
}
});
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
.
class
,
type
(
Integer
.
class
));
Integer
result
=
(
Integer
)
executor
.
execute
(
"3"
);
Integer
result
=
converter
.
convert
(
"3"
,
Integer
.
class
);
assertEquals
(
new
Integer
(
3
),
result
);
}
@Test
public
void
convertNoSuperTargetType
()
{
service
.
addConverter
(
new
Converter
<
CharSequence
,
Number
>()
{
converter
.
addConverter
(
new
Converter
<
CharSequence
,
Number
>()
{
public
Integer
convert
(
CharSequence
source
)
throws
Exception
{
return
Integer
.
valueOf
(
source
.
toString
());
}
public
CharSequence
convertBack
(
Number
target
)
throws
Exception
{
return
target
.
toString
();
}
});
try
{
service
.
convert
(
"3"
,
type
(
Integer
.
class
)
);
converter
.
convert
(
"3"
,
Integer
.
class
);
fail
(
"Should have failed"
);
}
catch
(
ConverterNotFoundException
e
)
{
...
...
@@ -144,17 +120,15 @@ public class GenericConversionServiceTests {
@Test
public
void
convertObjectToPrimitive
()
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
.
class
,
type
(
int
.
class
));
Integer
three
=
(
Integer
)
executor
.
execute
(
"3"
);
converter
.
addConverter
(
new
StringToInteger
());
Integer
three
=
converter
.
convert
(
"3"
,
int
.
class
);
assertEquals
(
3
,
three
.
intValue
());
}
@Test
public
void
convertArrayToArray
()
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
[].
class
,
type
(
Integer
[].
class
));
Integer
[]
result
=
(
Integer
[])
executor
.
execute
(
new
String
[]
{
"1"
,
"2"
,
"3"
});
converter
.
addConverter
(
new
StringToInteger
());
Integer
[]
result
=
converter
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
Integer
[].
class
);
assertEquals
(
new
Integer
(
1
),
result
[
0
]);
assertEquals
(
new
Integer
(
2
),
result
[
1
]);
assertEquals
(
new
Integer
(
3
),
result
[
2
]);
...
...
@@ -162,9 +136,8 @@ public class GenericConversionServiceTests {
@Test
public
void
convertArrayToPrimitiveArray
()
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
[].
class
,
type
(
int
[].
class
));
int
[]
result
=
(
int
[])
executor
.
execute
(
new
String
[]
{
"1"
,
"2"
,
"3"
});
converter
.
addConverter
(
new
StringToInteger
());
int
[]
result
=
(
int
[])
converter
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
int
[].
class
);
assertEquals
(
1
,
result
[
0
]);
assertEquals
(
2
,
result
[
1
]);
assertEquals
(
3
,
result
[
2
]);
...
...
@@ -172,8 +145,7 @@ public class GenericConversionServiceTests {
@Test
public
void
convertArrayToListInterface
()
{
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
[].
class
,
type
(
List
.
class
));
List
result
=
(
List
)
executor
.
execute
(
new
String
[]
{
"1"
,
"2"
,
"3"
});
List
<?>
result
=
converter
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
List
.
class
);
assertEquals
(
"1"
,
result
.
get
(
0
));
assertEquals
(
"2"
,
result
.
get
(
1
));
assertEquals
(
"3"
,
result
.
get
(
2
));
...
...
@@ -183,10 +155,8 @@ public class GenericConversionServiceTests {
@Test
public
void
convertArrayToListGenericTypeConversion
()
throws
Exception
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
[].
class
,
new
TypeDescriptor
(
getClass
()
.
getDeclaredField
(
"genericList"
)));
List
result
=
(
List
)
executor
.
execute
(
new
String
[]
{
"1"
,
"2"
,
"3"
});
converter
.
addConverter
(
new
StringToInteger
());
List
<
Integer
>
result
=
converter
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
new
BindingPoint
<
List
<
Integer
>>(
getClass
().
getDeclaredField
(
"genericList"
)));
assertEquals
(
new
Integer
(
"1"
),
result
.
get
(
0
));
assertEquals
(
new
Integer
(
"2"
),
result
.
get
(
1
));
assertEquals
(
new
Integer
(
"3"
),
result
.
get
(
2
));
...
...
@@ -194,8 +164,7 @@ public class GenericConversionServiceTests {
@Test
public
void
convertArrayToListImpl
()
{
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
[].
class
,
type
(
LinkedList
.
class
));
LinkedList
result
=
(
LinkedList
)
executor
.
execute
(
new
String
[]
{
"1"
,
"2"
,
"3"
});
LinkedList
<?>
result
=
converter
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
LinkedList
.
class
);
assertEquals
(
"1"
,
result
.
get
(
0
));
assertEquals
(
"2"
,
result
.
get
(
1
));
assertEquals
(
"3"
,
result
.
get
(
2
));
...
...
@@ -204,7 +173,7 @@ public class GenericConversionServiceTests {
@Test
public
void
convertArrayToAbstractList
()
{
try
{
service
.
getConversionExecutor
(
String
[].
class
,
type
(
AbstractList
.
class
)
);
converter
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
AbstractList
.
class
);
}
catch
(
IllegalArgumentException
e
)
{
}
...
...
@@ -212,12 +181,11 @@ public class GenericConversionServiceTests {
@Test
public
void
convertListToArray
()
{
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
Collection
.
class
,
type
(
String
[].
class
));
List
list
=
new
ArrayList
();
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
"1"
);
list
.
add
(
"2"
);
list
.
add
(
"3"
);
String
[]
result
=
(
String
[])
executor
.
execute
(
list
);
String
[]
result
=
(
String
[])
converter
.
convert
(
list
,
String
[].
class
);
assertEquals
(
"1"
,
result
[
0
]);
assertEquals
(
"2"
,
result
[
1
]);
assertEquals
(
"3"
,
result
[
2
]);
...
...
@@ -225,13 +193,12 @@ public class GenericConversionServiceTests {
@Test
public
void
convertListToArrayWithComponentConversion
()
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
Collection
.
class
,
type
(
Integer
[].
class
));
List
list
=
new
ArrayList
();
converter
.
addConverter
(
new
StringToInteger
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
"1"
);
list
.
add
(
"2"
);
list
.
add
(
"3"
);
Integer
[]
result
=
(
Integer
[])
executor
.
execute
(
list
);
Integer
[]
result
=
converter
.
convert
(
list
,
Integer
[].
class
);
assertEquals
(
new
Integer
(
1
),
result
[
0
]);
assertEquals
(
new
Integer
(
2
),
result
[
1
]);
assertEquals
(
new
Integer
(
3
),
result
[
2
]);
...
...
@@ -245,16 +212,17 @@ public class GenericConversionServiceTests {
Map
<
String
,
String
>
foo
=
new
HashMap
<
String
,
String
>();
foo
.
put
(
"1"
,
"BAR"
);
foo
.
put
(
"2"
,
"BAZ"
);
service
.
addConverter
(
new
StringToInteger
());
service
.
addConverter
(
new
StringToEnumFactory
().
getConverter
(
FooEnum
.
class
));
service
.
convert
(
foo
,
new
TypeDescriptor
(
getClass
().
getField
(
"genericMap"
)));
converter
.
addConverter
(
new
StringToInteger
());
converter
.
addConverter
(
new
StringToEnumFactory
().
getConverter
(
FooEnum
.
class
));
Map
<
String
,
FooEnum
>
map
=
converter
.
convert
(
foo
,
new
BindingPoint
<
Map
<
String
,
FooEnum
>>(
getClass
().
getField
(
"genericMap"
)));
assertEquals
(
map
.
get
(
1
),
FooEnum
.
BAR
);
assertEquals
(
map
.
get
(
2
),
FooEnum
.
BAZ
);
}
@Ignore
@Test
public
void
convertObjectToArray
()
{
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
.
class
,
type
(
String
[].
class
));
String
[]
result
=
(
String
[])
executor
.
execute
(
"1,2,3"
);
String
[]
result
=
(
String
[])
converter
.
convert
(
"1,2,3"
,
String
[].
class
);
assertEquals
(
1
,
result
.
length
);
assertEquals
(
"1,2,3"
,
result
[
0
]);
}
...
...
@@ -262,9 +230,8 @@ public class GenericConversionServiceTests {
@Ignore
@Test
public
void
convertObjectToArrayWithElementConversion
()
{
service
.
addConverter
(
new
StringToInteger
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
String
.
class
,
type
(
Integer
[].
class
));
Integer
[]
result
=
(
Integer
[])
executor
.
execute
(
"123"
);
converter
.
addConverter
(
new
StringToInteger
());
Integer
[]
result
=
converter
.
convert
(
"123"
,
Integer
[].
class
);
assertEquals
(
1
,
result
.
length
);
assertEquals
(
new
Integer
(
123
),
result
[
0
]);
}
...
...
@@ -273,8 +240,4 @@ public class GenericConversionServiceTests {
BAR
,
BAZ
}
private
TypeDescriptor
type
(
Class
<?>
clazz
)
{
return
TypeDescriptor
.
valueOf
(
clazz
);
}
}
\ No newline at end of file
org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapTests.java
浏览文件 @
6f74369c
...
...
@@ -6,29 +6,29 @@ import java.util.HashMap;
import
java.util.Map
;
import
org.junit.Test
;
import
org.springframework.core.convert.
TypeDescriptor
;
import
org.springframework.core.convert.support.Default
ConversionService
;
import
org.springframework.core.convert.
BindingPoint
;
import
org.springframework.core.convert.support.Default
TypeConverter
;
import
org.springframework.core.convert.support.MapToMap
;
public
class
MapToMapTests
{
@Test
public
void
testMapToMapConversion
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
MapToMap
c
=
new
MapToMap
(
new
TypeDescriptor
(
getClass
().
getField
(
"source"
)),
new
TypeDescriptor
(
getClass
().
getField
(
"bindTarget"
)),
service
);
Default
TypeConverter
converter
=
new
DefaultTypeConverter
();
MapToMap
c
=
new
MapToMap
(
new
BindingPoint
<
Map
<
String
,
String
>>
(
getClass
().
getField
(
"source"
)),
new
BindingPoint
<
Map
<
String
,
FooEnum
>>(
getClass
().
getField
(
"bindTarget"
)),
converter
);
source
.
put
(
"1"
,
"BAR"
);
source
.
put
(
"2"
,
"BAZ"
);
Map
result
=
(
Map
)
c
.
execute
(
source
);
Map
<
String
,
FooEnum
>
result
=
(
Map
<
String
,
FooEnum
>
)
c
.
execute
(
source
);
assertEquals
(
FooEnum
.
BAR
,
result
.
get
(
1
));
assertEquals
(
FooEnum
.
BAZ
,
result
.
get
(
2
));
}
@Test
public
void
testMapToMapConversionNoGenericInfoOnSource
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
MapToMap
c
=
new
MapToMap
(
TypeDescriptor
.
valueOf
(
Map
.
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"bindTarget"
)),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
MapToMap
c
=
new
MapToMap
(
BindingPoint
.
valueOf
(
Map
.
class
),
new
BindingPoint
(
getClass
().
getField
(
"bindTarget"
)),
service
);
source
.
put
(
"1"
,
"BAR"
);
source
.
put
(
"2"
,
"BAZ"
);
Map
result
=
(
Map
)
c
.
execute
(
source
);
...
...
@@ -38,9 +38,9 @@ public class MapToMapTests {
@Test
public
void
testMapToMapConversionNoGenericInfo
()
throws
Exception
{
Default
ConversionService
service
=
new
DefaultConversionService
();
MapToMap
c
=
new
MapToMap
(
TypeDescriptor
.
valueOf
(
Map
.
class
),
TypeDescriptor
.
valueOf
(
Map
.
class
),
service
);
Default
TypeConverter
service
=
new
DefaultTypeConverter
();
MapToMap
c
=
new
MapToMap
(
BindingPoint
.
valueOf
(
Map
.
class
),
BindingPoint
.
valueOf
(
Map
.
class
),
service
);
source
.
put
(
"1"
,
"BAR"
);
source
.
put
(
"2"
,
"BAZ"
);
Map
result
=
(
Map
)
c
.
execute
(
source
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录