Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
7430fcd9
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,发现更多精彩内容 >>
提交
7430fcd9
编写于
5月 23, 2011
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-8364
上级
5c67dbf4
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
168 addition
and
56 deletion
+168
-56
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java
...work/core/convert/support/ArrayToCollectionConverter.java
+14
-5
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java
...core/convert/support/CollectionToCollectionConverter.java
+11
-3
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java
...ringframework/core/convert/support/MapToMapConverter.java
+4
-2
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java
...ork/core/convert/support/ObjectToCollectionConverter.java
+6
-7
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java
...convert/support/CollectionToCollectionConverterTests.java
+113
-2
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
...k/core/convert/support/GenericConversionServiceTests.java
+16
-34
org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java
...ramework/core/convert/support/MapToMapConverterTests.java
+3
-2
org.springframework.core/src/test/resources/log4j.xml
org.springframework.core/src/test/resources/log4j.xml
+1
-1
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java
浏览文件 @
7430fcd9
...
...
@@ -59,11 +59,20 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter {
}
int
length
=
Array
.
getLength
(
source
);
Collection
<
Object
>
target
=
CollectionFactory
.
createCollection
(
targetType
.
getType
(),
length
);
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
Object
sourceElement
=
Array
.
get
(
source
,
i
);
Object
targetElement
=
this
.
conversionService
.
convert
(
sourceElement
,
sourceType
.
getElementTypeDescriptor
(),
targetType
.
getElementTypeDescriptor
());
target
.
add
(
targetElement
);
}
TypeDescriptor
sourceElementType
=
sourceType
.
getElementTypeDescriptor
();
TypeDescriptor
targetElementType
=
targetType
.
getElementTypeDescriptor
();
if
(
Object
.
class
.
equals
(
targetElementType
.
getType
()))
{
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
Object
sourceElement
=
Array
.
get
(
source
,
i
);
target
.
add
(
sourceElement
);
}
}
else
{
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
Object
sourceElement
=
Array
.
get
(
source
,
i
);
Object
targetElement
=
this
.
conversionService
.
convert
(
sourceElement
,
sourceElementType
,
targetElementType
);
target
.
add
(
targetElement
);
}
}
return
target
;
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java
浏览文件 @
7430fcd9
...
...
@@ -61,9 +61,17 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert
}
Collection
<?>
sourceCollection
=
(
Collection
<?>)
source
;
Collection
<
Object
>
target
=
CollectionFactory
.
createCollection
(
targetType
.
getType
(),
sourceCollection
.
size
());
for
(
Object
sourceElement
:
sourceCollection
)
{
Object
targetElement
=
this
.
conversionService
.
convert
(
sourceElement
,
sourceType
.
getElementTypeDescriptor
(),
targetType
.
getElementTypeDescriptor
());
target
.
add
(
targetElement
);
TypeDescriptor
sourceElementType
=
sourceType
.
getElementTypeDescriptor
();
TypeDescriptor
targetElementType
=
targetType
.
getElementTypeDescriptor
();
if
(
Object
.
class
.
equals
(
targetElementType
.
getType
()))
{
for
(
Object
sourceElement
:
sourceCollection
)
{
target
.
add
(
sourceElement
);
}
}
else
{
for
(
Object
sourceElement
:
sourceCollection
)
{
Object
targetElement
=
this
.
conversionService
.
convert
(
sourceElement
,
sourceElementType
,
targetElementType
);
target
.
add
(
targetElement
);
}
}
return
target
;
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java
浏览文件 @
7430fcd9
...
...
@@ -60,7 +60,9 @@ final class MapToMapConverter implements ConditionalGenericConverter {
}
Map
<
Object
,
Object
>
sourceMap
=
(
Map
<
Object
,
Object
>)
source
;
Map
<
Object
,
Object
>
targetMap
=
CollectionFactory
.
createMap
(
targetType
.
getType
(),
sourceMap
.
size
());
TypeDescriptor
sourceKeyType
=
sourceType
.
getMapKeyTypeDescriptor
();
TypeDescriptor
targetKeyType
=
targetType
.
getMapKeyTypeDescriptor
();
TypeDescriptor
sourceValueType
=
sourceType
.
getMapValueTypeDescriptor
();
TypeDescriptor
targetValueType
=
targetType
.
getMapValueTypeDescriptor
();
if
(
Object
.
class
.
equals
(
targetKeyType
.
getType
())
&&
Object
.
class
.
equals
(
targetValueType
.
getType
()))
{
for
(
Map
.
Entry
<
Object
,
Object
>
entry
:
sourceMap
.
entrySet
())
{
...
...
@@ -70,8 +72,8 @@ final class MapToMapConverter implements ConditionalGenericConverter {
for
(
Map
.
Entry
<
Object
,
Object
>
entry
:
sourceMap
.
entrySet
())
{
Object
sourceKey
=
entry
.
getKey
();
Object
sourceValue
=
entry
.
getValue
();
Object
targetKey
=
this
.
conversionService
.
convert
(
sourceKey
,
source
Type
.
getMapKeyTypeDescriptor
(),
targetType
.
getMapKeyTypeDescriptor
()
);
Object
targetValue
=
this
.
conversionService
.
convert
(
sourceValue
,
source
Type
.
getMapValueTypeDescriptor
(),
targetType
.
getMapValueTypeDescriptor
()
);
Object
targetKey
=
this
.
conversionService
.
convert
(
sourceKey
,
source
KeyType
,
targetKeyType
);
Object
targetValue
=
this
.
conversionService
.
convert
(
sourceValue
,
source
ValueType
,
targetValueType
);
targetMap
.
put
(
targetKey
,
targetValue
);
}
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java
浏览文件 @
7430fcd9
...
...
@@ -55,13 +55,12 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter {
return
null
;
}
Collection
<
Object
>
target
=
CollectionFactory
.
createCollection
(
targetType
.
getType
(),
1
);
TypeDescriptor
elementType
=
targetType
.
getElementTypeDescriptor
();
// Avoid potential recursion...
if
(!
Collection
.
class
.
isAssignableFrom
(
elementType
.
getType
()))
{
target
.
add
(
this
.
conversionService
.
convert
(
source
,
sourceType
,
elementType
));
}
else
{
target
.
add
(
source
);
TypeDescriptor
targetElementType
=
targetType
.
getElementTypeDescriptor
();
// Avoid potential recursion....
if
(
targetElementType
.
isCollection
())
{
target
.
add
(
source
);
}
else
{
target
.
add
(
this
.
conversionService
.
convert
(
source
,
sourceType
,
targetElementType
));
}
return
target
;
}
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java
浏览文件 @
7430fcd9
package
org.springframework.core.convert.support
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.io.File
;
import
java.io.IOException
;
...
...
@@ -9,6 +10,8 @@ import java.io.InputStream;
import
java.net.URI
;
import
java.net.URL
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.LinkedList
;
import
java.util.List
;
import
org.junit.Before
;
...
...
@@ -28,6 +31,114 @@ public class CollectionToCollectionConverterTests {
conversionService
.
addConverter
(
new
CollectionToCollectionConverter
(
conversionService
));
}
@Test
public
void
scalarList
()
throws
Exception
{
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
"9"
);
list
.
add
(
"37"
);
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"scalarListTarget"
));
assertFalse
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
@SuppressWarnings
(
"unchecked"
)
List
<
String
>
result
=
(
List
<
String
>)
conversionService
.
convert
(
list
,
sourceType
,
targetType
);
assertFalse
(
list
.
equals
(
result
));
assertEquals
((
Integer
)
9
,
result
.
get
(
0
));
assertEquals
((
Integer
)
37
,
result
.
get
(
1
));
}
public
List
<
Integer
>
scalarListTarget
;
@Test
public
void
emptyListToList
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToCollectionConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"emptyListTarget"
));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
assertEquals
(
list
,
conversionService
.
convert
(
list
,
sourceType
,
targetType
));
}
public
List
<
Integer
>
emptyListTarget
;
@Test
public
void
emptyListToListDifferentTargetType
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToCollectionConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"emptyListDifferentTarget"
));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
@SuppressWarnings
(
"unchecked"
)
LinkedList
<
Integer
>
result
=
(
LinkedList
<
Integer
>)
conversionService
.
convert
(
list
,
sourceType
,
targetType
);
assertEquals
(
LinkedList
.
class
,
result
.
getClass
());
assertTrue
(
result
.
isEmpty
());
}
public
LinkedList
<
Integer
>
emptyListDifferentTarget
;
@Test
public
void
collectionToObjectInteraction
()
throws
Exception
{
List
<
List
<
String
>>
list
=
new
ArrayList
<
List
<
String
>>();
list
.
add
(
Arrays
.
asList
(
"9"
,
"12"
));
list
.
add
(
Arrays
.
asList
(
"37"
,
"23"
));
conversionService
.
addConverter
(
new
CollectionToObjectConverter
(
conversionService
));
assertTrue
(
conversionService
.
canConvert
(
List
.
class
,
List
.
class
));
assertEquals
(
list
,
conversionService
.
convert
(
list
,
List
.
class
));
}
@Test
public
void
arrayCollectionToObjectInteraction
()
throws
Exception
{
List
<
String
>[]
array
=
new
List
[
2
];
array
[
0
]
=
Arrays
.
asList
(
"9"
,
"12"
);
array
[
1
]
=
Arrays
.
asList
(
"37"
,
"23"
);
conversionService
.
addConverter
(
new
ArrayToCollectionConverter
(
conversionService
));
conversionService
.
addConverter
(
new
CollectionToObjectConverter
(
conversionService
));
assertTrue
(
conversionService
.
canConvert
(
String
[].
class
,
List
.
class
));
assertEquals
(
Arrays
.
asList
(
array
),
conversionService
.
convert
(
array
,
List
.
class
));
}
@Test
public
void
objectToCollection
()
throws
Exception
{
List
<
List
<
String
>>
list
=
new
ArrayList
<
List
<
String
>>();
list
.
add
(
Arrays
.
asList
(
"9"
,
"12"
));
list
.
add
(
Arrays
.
asList
(
"37"
,
"23"
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
conversionService
.
addConverter
(
new
ObjectToCollectionConverter
(
conversionService
));
conversionService
.
addConverter
(
new
CollectionToObjectConverter
(
conversionService
));
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"objectToCollection"
));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
List
<
List
<
List
<
Integer
>>>
result
=
(
List
<
List
<
List
<
Integer
>>>)
conversionService
.
convert
(
list
,
sourceType
,
targetType
);
assertEquals
((
Integer
)
9
,
result
.
get
(
0
).
get
(
0
).
get
(
0
));
assertEquals
((
Integer
)
12
,
result
.
get
(
0
).
get
(
1
).
get
(
0
));
assertEquals
((
Integer
)
37
,
result
.
get
(
1
).
get
(
0
).
get
(
0
));
assertEquals
((
Integer
)
23
,
result
.
get
(
1
).
get
(
1
).
get
(
0
));
}
public
List
<
List
<
List
<
Integer
>>>
objectToCollection
;
@Test
public
void
stringToCollection
()
throws
Exception
{
List
<
List
<
String
>>
list
=
new
ArrayList
<
List
<
String
>>();
list
.
add
(
Arrays
.
asList
(
"9,12"
));
list
.
add
(
Arrays
.
asList
(
"37,23"
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
conversionService
.
addConverter
(
new
StringToCollectionConverter
(
conversionService
));
conversionService
.
addConverter
(
new
ObjectToCollectionConverter
(
conversionService
));
conversionService
.
addConverter
(
new
CollectionToObjectConverter
(
conversionService
));
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"objectToCollection"
));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
List
<
List
<
List
<
Integer
>>>
result
=
(
List
<
List
<
List
<
Integer
>>>)
conversionService
.
convert
(
list
,
sourceType
,
targetType
);
assertEquals
((
Integer
)
9
,
result
.
get
(
0
).
get
(
0
).
get
(
0
));
assertEquals
((
Integer
)
12
,
result
.
get
(
0
).
get
(
0
).
get
(
1
));
assertEquals
((
Integer
)
37
,
result
.
get
(
1
).
get
(
0
).
get
(
0
));
assertEquals
((
Integer
)
23
,
result
.
get
(
1
).
get
(
0
).
get
(
1
));
}
@Test
public
void
differentImpls
()
throws
Exception
{
List
<
Resource
>
resources
=
new
ArrayList
<
Resource
>();
...
...
@@ -69,7 +180,7 @@ public class CollectionToCollectionConverterTests {
public
List
<
Resource
>
resources
;
public
static
abstract
class
BaseResource
implements
Resource
{
public
static
abstract
class
BaseResource
implements
Resource
{
public
InputStream
getInputStream
()
throws
IOException
{
// TODO Auto-generated method stub
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
浏览文件 @
7430fcd9
...
...
@@ -16,7 +16,16 @@
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.Arrays
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
...
...
@@ -24,9 +33,7 @@ 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
;
...
...
@@ -261,8 +268,13 @@ public class GenericConversionServiceTests {
@Test
public
void
testListOfList
()
{
GenericConversionService
service
=
new
DefaultConversionService
();
List
<
List
<
String
>>
list
=
Collections
.
singletonList
(
Collections
.
singletonList
(
"Foo"
));
assertNotNull
(
service
.
convert
(
list
,
String
.
class
));
List
<
String
>
list1
=
Arrays
.
asList
(
"Foo"
,
"Bar"
);
List
<
String
>
list2
=
Arrays
.
asList
(
"Baz"
,
"Boop"
);
@SuppressWarnings
(
"unchecked"
)
List
<
List
<
String
>>
list
=
Arrays
.
asList
(
list1
,
list2
);
String
result
=
service
.
convert
(
list
,
String
.
class
);
assertNotNull
(
result
);
assertEquals
(
"Foo,Bar,Baz,Boop"
,
result
);
}
@Test
...
...
@@ -360,36 +372,6 @@ public class GenericConversionServiceTests {
public
static
Map
<
String
,
Integer
>
map
;
@Test
public
void
emptyListToList
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToCollectionConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"emptyListTarget"
));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
assertEquals
(
list
,
conversionService
.
convert
(
list
,
sourceType
,
targetType
));
}
public
List
<
Integer
>
emptyListTarget
;
@Test
public
void
emptyListToListDifferentTargetType
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToCollectionConverter
(
conversionService
));
conversionService
.
addConverterFactory
(
new
StringToNumberConverterFactory
());
List
<
String
>
list
=
new
ArrayList
<
String
>();
TypeDescriptor
sourceType
=
TypeDescriptor
.
forObject
(
list
);
TypeDescriptor
targetType
=
new
TypeDescriptor
(
getClass
().
getField
(
"emptyListDifferentTarget"
));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
@SuppressWarnings
(
"unchecked"
)
LinkedList
<
Integer
>
result
=
(
LinkedList
<
Integer
>)
conversionService
.
convert
(
list
,
sourceType
,
targetType
);
assertEquals
(
LinkedList
.
class
,
result
.
getClass
());
assertTrue
(
result
.
isEmpty
());
}
public
LinkedList
<
Integer
>
emptyListDifferentTarget
;
@Test
public
void
emptyListToArray
()
throws
Exception
{
conversionService
.
addConverter
(
new
CollectionToArrayConverter
(
conversionService
));
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java
浏览文件 @
7430fcd9
...
...
@@ -52,7 +52,7 @@ public class MapToMapConverterTests {
}
@Test
public
void
scalarMapNotGenericSource
()
throws
Exception
{
public
void
scalarMapNotGenericSource
Field
()
throws
Exception
{
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
map
.
put
(
"1"
,
"9"
);
map
.
put
(
"2"
,
"37"
);
...
...
@@ -117,6 +117,7 @@ public class MapToMapConverterTests {
Map
<
String
,
List
<
String
>>
map
=
new
HashMap
<
String
,
List
<
String
>>();
map
.
put
(
"1"
,
Arrays
.
asList
(
"9"
,
"12"
));
map
.
put
(
"2"
,
Arrays
.
asList
(
"37"
,
"23"
));
conversionService
.
addConverter
(
new
CollectionToCollectionConverter
(
conversionService
));
conversionService
.
addConverter
(
new
CollectionToObjectConverter
(
conversionService
));
assertTrue
(
conversionService
.
canConvert
(
Map
.
class
,
Map
.
class
));
assertEquals
(
map
,
conversionService
.
convert
(
map
,
Map
.
class
));
...
...
@@ -127,7 +128,7 @@ public class MapToMapConverterTests {
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));
assertTrue
(
conversionService
.
canConvert
(
sourceType
,
targetType
));
assertEquals
(
map
,
conversionService
.
convert
(
map
,
sourceType
,
targetType
));
}
...
...
org.springframework.core/src/test/resources/log4j.xml
浏览文件 @
7430fcd9
...
...
@@ -11,7 +11,7 @@
</layout>
</appender>
<logger
name=
"org.springframework.core"
>
<logger
name=
"org.springframework.core
.convert
"
>
<level
value=
"warn"
/>
</logger>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录