Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
5e0cd9fb
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,发现更多精彩内容 >>
提交
5e0cd9fb
编写于
2月 14, 2019
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
差异文件
Merge branch '5.1.x'
上级
9967ece4
ac0e5d8e
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
61 addition
and
52 deletion
+61
-52
spring-core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java
...springframework/core/convert/support/ConversionUtils.java
+3
-2
spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java
...core/convert/converter/DefaultConversionServiceTests.java
+58
-50
未找到文件。
spring-core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java
浏览文件 @
5e0cd9fb
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -22,6 +22,7 @@ import org.springframework.core.convert.TypeDescriptor;
import
org.springframework.core.convert.converter.GenericConverter
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ClassUtils
;
/**
* Internal utilities for the conversion package.
...
...
@@ -62,7 +63,7 @@ abstract class ConversionUtils {
// yes
return
true
;
}
if
(
sourceElementType
.
getType
().
isAssignableFrom
(
targetElementType
.
getType
()))
{
if
(
ClassUtils
.
isAssignable
(
sourceElementType
.
getType
(),
targetElementType
.
getType
()))
{
// maybe
return
true
;
}
...
...
spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java
浏览文件 @
5e0cd9fb
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -134,7 +134,7 @@ public class DefaultConversionServiceTests {
}
@Test
public
void
testStringToByte
()
throws
Exception
{
public
void
testStringToByte
()
{
assertEquals
(
Byte
.
valueOf
(
"1"
),
conversionService
.
convert
(
"1"
,
Byte
.
class
));
}
...
...
@@ -225,12 +225,12 @@ public class DefaultConversionServiceTests {
}
@Test
public
void
testStringToEnum
()
throws
Exception
{
public
void
testStringToEnum
()
{
assertEquals
(
Foo
.
BAR
,
conversionService
.
convert
(
"BAR"
,
Foo
.
class
));
}
@Test
public
void
testStringToEnumWithSubclass
()
throws
Exception
{
public
void
testStringToEnumWithSubclass
()
{
assertEquals
(
SubFoo
.
BAZ
,
conversionService
.
convert
(
"BAZ"
,
SubFoo
.
BAR
.
getClass
()));
}
...
...
@@ -245,12 +245,12 @@ public class DefaultConversionServiceTests {
}
@Test
public
void
testIntegerToEnum
()
throws
Exception
{
public
void
testIntegerToEnum
()
{
assertEquals
(
Foo
.
BAR
,
conversionService
.
convert
(
0
,
Foo
.
class
));
}
@Test
public
void
testIntegerToEnumWithSubclass
()
throws
Exception
{
public
void
testIntegerToEnumWithSubclass
()
{
assertEquals
(
SubFoo
.
BAZ
,
conversionService
.
convert
(
1
,
SubFoo
.
BAR
.
getClass
()));
}
...
...
@@ -395,10 +395,6 @@ public class DefaultConversionServiceTests {
conversionService
.
convert
(
new
String
[]{
"1"
,
"2"
,
"3"
},
AbstractList
.
class
);
}
public
static
enum
FooEnum
{
BAR
,
BAZ
}
@Test
public
void
convertArrayToString
()
{
String
result
=
conversionService
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
String
.
class
);
...
...
@@ -525,9 +521,8 @@ public class DefaultConversionServiceTests {
}
@Test
@SuppressWarnings
(
"rawtypes"
)
public
void
convertStringToCollection
()
{
List
result
=
conversionService
.
convert
(
"1,2,3"
,
List
.
class
);
List
<?>
result
=
conversionService
.
convert
(
"1,2,3"
,
List
.
class
);
assertEquals
(
3
,
result
.
size
());
assertEquals
(
"1"
,
result
.
get
(
0
));
assertEquals
(
"2"
,
result
.
get
(
1
));
...
...
@@ -535,9 +530,8 @@ public class DefaultConversionServiceTests {
}
@Test
@SuppressWarnings
(
"rawtypes"
)
public
void
convertStringToCollectionWithElementConversion
()
throws
Exception
{
List
result
=
(
List
)
conversionService
.
convert
(
"1,2,3"
,
TypeDescriptor
.
valueOf
(
String
.
class
),
List
<?>
result
=
(
List
)
conversionService
.
convert
(
"1,2,3"
,
TypeDescriptor
.
valueOf
(
String
.
class
),
new
TypeDescriptor
(
getClass
().
getField
(
"genericList"
)));
assertEquals
(
3
,
result
.
size
());
assertEquals
(
1
,
result
.
get
(
0
));
...
...
@@ -546,9 +540,8 @@ public class DefaultConversionServiceTests {
}
@Test
@SuppressWarnings
(
"rawtypes"
)
public
void
convertEmptyStringToCollection
()
{
Collection
result
=
conversionService
.
convert
(
""
,
Collection
.
class
);
Collection
<?>
result
=
conversionService
.
convert
(
""
,
Collection
.
class
);
assertEquals
(
0
,
result
.
size
());
}
...
...
@@ -575,25 +568,18 @@ public class DefaultConversionServiceTests {
}
@Test
@SuppressWarnings
(
"rawtypes"
)
public
void
convertCollectionToObjectWithCustomConverter
()
throws
Exception
{
public
void
convertCollectionToObjectWithCustomConverter
()
{
List
<
String
>
source
=
new
ArrayList
<>();
source
.
add
(
"A"
);
source
.
add
(
"B"
);
conversionService
.
addConverter
(
new
Converter
<
List
,
ListWrapper
>()
{
@Override
public
ListWrapper
convert
(
List
source
)
{
return
new
ListWrapper
(
source
);
}
});
conversionService
.
addConverter
(
List
.
class
,
ListWrapper
.
class
,
ListWrapper:
:
new
);
ListWrapper
result
=
conversionService
.
convert
(
source
,
ListWrapper
.
class
);
assertSame
(
source
,
result
.
getList
());
}
@Test
@SuppressWarnings
(
"rawtypes"
)
public
void
convertObjectToCollection
()
{
List
result
=
conversionService
.
convert
(
3L
,
List
.
class
);
List
<?>
result
=
conversionService
.
convert
(
3L
,
List
.
class
);
assertEquals
(
1
,
result
.
size
());
assertEquals
(
3L
,
result
.
get
(
0
));
}
...
...
@@ -608,7 +594,7 @@ public class DefaultConversionServiceTests {
}
@Test
public
void
convert
ArrayTo
Array
()
{
public
void
convert
StringArrayToInteger
Array
()
{
Integer
[]
result
=
conversionService
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
Integer
[].
class
);
assertEquals
(
Integer
.
valueOf
(
1
),
result
[
0
]);
assertEquals
(
Integer
.
valueOf
(
2
),
result
[
1
]);
...
...
@@ -616,7 +602,7 @@ public class DefaultConversionServiceTests {
}
@Test
public
void
convert
ArrayToPrimitive
Array
()
{
public
void
convert
StringArrayToInt
Array
()
{
int
[]
result
=
conversionService
.
convert
(
new
String
[]
{
"1"
,
"2"
,
"3"
},
int
[].
class
);
assertEquals
(
1
,
result
[
0
]);
assertEquals
(
2
,
result
[
1
]);
...
...
@@ -624,7 +610,39 @@ public class DefaultConversionServiceTests {
}
@Test
public
void
convertArrayToWrapperArray
()
{
public
void
convertIntegerArrayToIntegerArray
()
{
Integer
[]
result
=
conversionService
.
convert
(
new
Integer
[]
{
1
,
2
,
3
},
Integer
[].
class
);
assertEquals
(
Integer
.
valueOf
(
1
),
result
[
0
]);
assertEquals
(
Integer
.
valueOf
(
2
),
result
[
1
]);
assertEquals
(
Integer
.
valueOf
(
3
),
result
[
2
]);
}
@Test
public
void
convertIntegerArrayToIntArray
()
{
int
[]
result
=
conversionService
.
convert
(
new
Integer
[]
{
1
,
2
,
3
},
int
[].
class
);
assertEquals
(
1
,
result
[
0
]);
assertEquals
(
2
,
result
[
1
]);
assertEquals
(
3
,
result
[
2
]);
}
@Test
public
void
convertObjectArrayToIntegerArray
()
{
Integer
[]
result
=
conversionService
.
convert
(
new
Object
[]
{
1
,
2
,
3
},
Integer
[].
class
);
assertEquals
(
Integer
.
valueOf
(
1
),
result
[
0
]);
assertEquals
(
Integer
.
valueOf
(
2
),
result
[
1
]);
assertEquals
(
Integer
.
valueOf
(
3
),
result
[
2
]);
}
@Test
public
void
convertObjectArrayToIntArray
()
{
int
[]
result
=
conversionService
.
convert
(
new
Object
[]
{
1
,
2
,
3
},
int
[].
class
);
assertEquals
(
1
,
result
[
0
]);
assertEquals
(
2
,
result
[
1
]);
assertEquals
(
3
,
result
[
2
]);
}
@Test
public
void
convertByteArrayToWrapperArray
()
{
byte
[]
byteArray
=
new
byte
[]
{
1
,
2
,
3
};
Byte
[]
converted
=
conversionService
.
convert
(
byteArray
,
Byte
[].
class
);
assertThat
(
converted
,
equalTo
(
new
Byte
[]{
1
,
2
,
3
}));
...
...
@@ -694,7 +712,7 @@ public class DefaultConversionServiceTests {
@Test
@SuppressWarnings
(
"rawtypes"
)
public
void
convertCollectionToCollectionNotGeneric
()
throws
Exception
{
public
void
convertCollectionToCollectionNotGeneric
()
{
Set
<
String
>
foo
=
new
LinkedHashSet
<>();
foo
.
add
(
"1"
);
foo
.
add
(
"2"
);
...
...
@@ -740,10 +758,10 @@ public class DefaultConversionServiceTests {
foo
.
put
(
"1"
,
"BAR"
);
foo
.
put
(
"2"
,
"BAZ"
);
@SuppressWarnings
(
"unchecked"
)
Map
<
Integer
,
Foo
Enum
>
map
=
(
Map
<
Integer
,
FooEnum
>)
conversionService
.
convert
(
foo
,
Map
<
Integer
,
Foo
>
map
=
(
Map
<
Integer
,
Foo
>)
conversionService
.
convert
(
foo
,
TypeDescriptor
.
forObject
(
foo
),
new
TypeDescriptor
(
getClass
().
getField
(
"genericMap"
)));
assertEquals
(
Foo
Enum
.
BAR
,
map
.
get
(
1
));
assertEquals
(
Foo
Enum
.
BAZ
,
map
.
get
(
2
));
assertEquals
(
Foo
.
BAR
,
map
.
get
(
1
));
assertEquals
(
Foo
.
BAZ
,
map
.
get
(
2
));
}
@Test
...
...
@@ -881,25 +899,20 @@ public class DefaultConversionServiceTests {
}
@Test
public
void
convertCharArrayToString
()
throws
Exception
{
public
void
convertCharArrayToString
()
{
String
converted
=
conversionService
.
convert
(
new
char
[]
{
'a'
,
'b'
,
'c'
},
String
.
class
);
assertThat
(
converted
,
equalTo
(
"a,b,c"
));
}
@Test
public
void
convertStringToCharArray
()
throws
Exception
{
public
void
convertStringToCharArray
()
{
char
[]
converted
=
conversionService
.
convert
(
"a,b,c"
,
char
[].
class
);
assertThat
(
converted
,
equalTo
(
new
char
[]{
'a'
,
'b'
,
'c'
}));
}
@Test
public
void
convertStringToCustomCharArray
()
throws
Exception
{
conversionService
.
addConverter
(
new
Converter
<
String
,
char
[]>()
{
@Override
public
char
[]
convert
(
String
source
)
{
return
source
.
toCharArray
();
}
});
public
void
convertStringToCustomCharArray
()
{
conversionService
.
addConverter
(
String
.
class
,
char
[].
class
,
String:
:
toCharArray
);
char
[]
converted
=
conversionService
.
convert
(
"abc"
,
char
[].
class
);
assertThat
(
converted
,
equalTo
(
new
char
[]
{
'a'
,
'b'
,
'c'
}));
}
...
...
@@ -916,16 +929,11 @@ public class DefaultConversionServiceTests {
@Test
public
void
convertCannotOptimizeArray
()
{
conversionService
.
addConverter
(
new
Converter
<
Byte
,
Byte
>()
{
@Override
public
Byte
convert
(
Byte
source
)
{
return
(
byte
)
(
source
+
1
);
}
});
conversionService
.
addConverter
(
Byte
.
class
,
Byte
.
class
,
source
->
(
byte
)
(
source
+
1
));
byte
[]
byteArray
=
new
byte
[]
{
1
,
2
,
3
};
byte
[]
converted
=
conversionService
.
convert
(
byteArray
,
byte
[].
class
);
assertNotSame
(
byteArray
,
converted
);
assert
True
(
Arrays
.
equals
(
new
byte
[]
{
2
,
3
,
4
},
converted
)
);
assert
ArrayEquals
(
new
byte
[]{
2
,
3
,
4
},
converted
);
}
@Test
...
...
@@ -977,7 +985,7 @@ public class DefaultConversionServiceTests {
public
Stream
<
Integer
>
genericStream
;
public
Map
<
Integer
,
Foo
Enum
>
genericMap
=
new
HashMap
<>();
public
Map
<
Integer
,
Foo
>
genericMap
=
new
HashMap
<>();
public
EnumSet
<
Foo
>
enumSet
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录