Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
8e23685a
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,发现更多精彩内容 >>
提交
8e23685a
编写于
1月 07, 2011
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support for empty collection->collection. map->map, collection->array conversion
上级
42403a37
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
63 addition
and
6 deletion
+63
-6
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java
...work/core/convert/support/CollectionToArrayConverter.java
+5
-1
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java
...ork/core/convert/support/CollectionToObjectConverter.java
+5
-1
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java
...ringframework/core/convert/support/MapToMapConverter.java
+8
-0
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
...k/core/convert/support/GenericConversionServiceTests.java
+45
-4
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java
浏览文件 @
8e23685a
...
...
@@ -49,7 +49,11 @@ final class CollectionToArrayConverter implements ConditionalGenericConverter {
}
public
boolean
matches
(
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
return
this
.
conversionService
.
canConvert
(
sourceType
.
getElementTypeDescriptor
(),
targetType
.
getElementTypeDescriptor
());
TypeDescriptor
sourceElementType
=
sourceType
.
getElementTypeDescriptor
();
if
(
Object
.
class
.
equals
(
sourceElementType
.
getType
()))
{
return
true
;
}
return
this
.
conversionService
.
canConvert
(
sourceElementType
,
targetType
.
getElementTypeDescriptor
());
}
public
Object
convert
(
Object
source
,
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java
浏览文件 @
8e23685a
...
...
@@ -43,7 +43,11 @@ final class CollectionToObjectConverter implements ConditionalGenericConverter {
}
public
boolean
matches
(
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
return
this
.
conversionService
.
canConvert
(
sourceType
.
getElementTypeDescriptor
(),
targetType
);
TypeDescriptor
sourceElementType
=
sourceType
.
getElementTypeDescriptor
();
if
(
Object
.
class
.
equals
(
sourceElementType
.
getType
()))
{
return
true
;
}
return
this
.
conversionService
.
canConvert
(
sourceElementType
,
targetType
);
}
public
Object
convert
(
Object
source
,
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java
浏览文件 @
8e23685a
...
...
@@ -49,6 +49,14 @@ final class MapToMapConverter implements ConditionalGenericConverter {
}
public
boolean
matches
(
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
TypeDescriptor
sourceKeyType
=
sourceType
.
getMapKeyTypeDescriptor
();
TypeDescriptor
targetKeyType
=
targetType
.
getMapKeyTypeDescriptor
();
TypeDescriptor
sourceValueType
=
sourceType
.
getMapValueTypeDescriptor
();
TypeDescriptor
targetValueType
=
targetType
.
getMapValueTypeDescriptor
();
if
(
Object
.
class
.
equals
(
sourceKeyType
.
getType
())
&&
Object
.
class
.
equals
(
sourceValueType
.
getType
())
||
Object
.
class
.
equals
(
targetKeyType
.
getType
())
&&
Object
.
class
.
equals
(
targetValueType
.
getType
()))
{
return
true
;
}
return
this
.
conversionService
.
canConvert
(
sourceType
.
getMapKeyTypeDescriptor
(),
targetType
.
getMapKeyTypeDescriptor
())
&&
this
.
conversionService
.
canConvert
(
sourceType
.
getMapValueTypeDescriptor
(),
targetType
.
getMapValueTypeDescriptor
());
}
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
浏览文件 @
8e23685a
...
...
@@ -16,6 +16,14 @@
package
org.springframework.core.convert.support
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
...
...
@@ -24,15 +32,13 @@ import java.util.LinkedList;
import
java.util.List
;
import
java.util.Map
;
import
static
org
.
junit
.
Assert
.*;
import
org.junit.Test
;
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
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.DescriptiveResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.util.StopWatch
;
import
org.springframework.util.StringUtils
;
...
...
@@ -383,7 +389,7 @@ public class GenericConversionServiceTests {
@Test
public
void
emptyList
()
throws
Exception
{
public
void
emptyList
ToList
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToCollectionConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
...
...
@@ -394,6 +400,41 @@ public class GenericConversionServiceTests {
}
public
List
<
Integer
>
emptyListTarget
;
@Test
public
void
emptyListToArray
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToArrayConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
TypeDescriptor
.
valueOf
(
String
[].
class
);
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
assertEquals
(
0
,
((
String
[])
conversionService
.
convert
(
list
,
sourceType
,
targetType
)).
length
);
}
@Test
public
void
emptyListToObject
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToObjectConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
TypeDescriptor
.
valueOf
(
Integer
.
class
);
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
assertNull
(
conversionService
.
convert
(
list
,
sourceType
,
targetType
));
}
@Test
public
void
emptyMapToMap
()
throws
Exception
{
conversionService
.
addConverter
(
new
MapToMapConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
map
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"emptyMapTarget"
));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
assertEquals
(
map
,
conversionService
.
convert
(
map
,
sourceType
,
targetType
));
}
public
Map
<
String
,
String
>
emptyMapTarget
;
private
interface
MyBaseInterface
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录