Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
chenpeng_fei
spring-framework
提交
f786fc32
S
spring-framework
项目概览
chenpeng_fei
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
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,发现更多精彩内容 >>
提交
f786fc32
编写于
3月 05, 2015
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ObjectToOptionalConverter preserves existing Optional instances
Issue: SPR-12785
上级
dcb11453
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
43 addition
and
32 deletion
+43
-32
spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java
...ework/core/convert/support/ObjectToOptionalConverter.java
+6
-2
spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java
...ramework/core/convert/support/DefaultConversionTests.java
+37
-30
未找到文件。
spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java
浏览文件 @
f786fc32
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -31,6 +31,7 @@ import org.springframework.lang.UsesJava8;
* of Optional when known.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 4.1
*/
@UsesJava8
...
...
@@ -64,7 +65,10 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
if
(
source
==
null
)
{
return
Optional
.
empty
();
}
if
(
targetType
.
getResolvableType
()
==
null
)
{
else
if
(
source
instanceof
Optional
)
{
return
source
;
}
else
if
(
targetType
.
getResolvableType
()
==
null
)
{
return
Optional
.
of
(
source
);
}
else
{
...
...
spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java
浏览文件 @
f786fc32
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -71,7 +71,7 @@ public class DefaultConversionTests {
assertEquals
(
null
,
conversionService
.
convert
(
""
,
Character
.
class
));
}
@Test
(
expected
=
ConversionFailedException
.
class
)
@Test
(
expected
=
ConversionFailedException
.
class
)
public
void
testStringToCharacterInvalidString
()
{
conversionService
.
convert
(
"invalid"
,
Character
.
class
);
}
...
...
@@ -108,7 +108,7 @@ public class DefaultConversionTests {
assertEquals
(
null
,
conversionService
.
convert
(
""
,
Boolean
.
class
));
}
@Test
(
expected
=
ConversionFailedException
.
class
)
@Test
(
expected
=
ConversionFailedException
.
class
)
public
void
testStringToBooleanInvalidString
()
{
conversionService
.
convert
(
"invalid"
,
Boolean
.
class
);
}
...
...
@@ -267,36 +267,11 @@ public class DefaultConversionTests {
assertEquals
(
Long
.
valueOf
(
1
),
conversionService
.
convert
(
1
,
Long
.
class
));
}
@Test
(
expected
=
ConversionFailedException
.
class
)
@Test
(
expected
=
ConversionFailedException
.
class
)
public
void
testNumberToNumberNotSupportedNumber
()
{
conversionService
.
convert
(
1
,
CustomNumber
.
class
);
}
@SuppressWarnings
(
"serial"
)
public
static
class
CustomNumber
extends
Number
{
@Override
public
double
doubleValue
()
{
return
0
;
}
@Override
public
float
floatValue
()
{
return
0
;
}
@Override
public
int
intValue
()
{
return
0
;
}
@Override
public
long
longValue
()
{
return
0
;
}
}
@Test
public
void
testNumberToCharacter
()
{
assertEquals
(
Character
.
valueOf
(
'A'
),
conversionService
.
convert
(
65
,
Character
.
class
));
...
...
@@ -827,7 +802,7 @@ public class DefaultConversionTests {
TypeDescriptor
descriptor
=
new
TypeDescriptor
(
parameter
);
Object
actual
=
conversionService
.
convert
(
"1,2,3"
,
TypeDescriptor
.
valueOf
(
String
.
class
),
descriptor
);
assertEquals
(
Optional
.
class
,
actual
.
getClass
());
assertEquals
(
Arrays
.
asList
(
1
,
2
,
3
),
((
Optional
<
List
<
Integer
>>)
actual
).
get
());
assertEquals
(
Arrays
.
asList
(
1
,
2
,
3
),
((
Optional
<
List
<
Integer
>>)
actual
).
get
());
}
@Test
...
...
@@ -837,6 +812,38 @@ public class DefaultConversionTests {
assertSame
(
Optional
.
empty
(),
conversionService
.
convert
(
null
,
Optional
.
class
));
}
@Test
public
void
convertExistingOptional
()
{
assertSame
(
Optional
.
empty
(),
conversionService
.
convert
(
Optional
.
empty
(),
TypeDescriptor
.
valueOf
(
Object
.
class
),
TypeDescriptor
.
valueOf
(
Optional
.
class
)));
assertSame
(
Optional
.
empty
(),
conversionService
.
convert
(
Optional
.
empty
(),
Optional
.
class
));
}
@SuppressWarnings
(
"serial"
)
public
static
class
CustomNumber
extends
Number
{
@Override
public
double
doubleValue
()
{
return
0
;
}
@Override
public
float
floatValue
()
{
return
0
;
}
@Override
public
int
intValue
()
{
return
0
;
}
@Override
public
long
longValue
()
{
return
0
;
}
}
public
static
class
TestEntity
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录