Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
fb057bb5
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,发现更多精彩内容 >>
提交
fb057bb5
编写于
2月 12, 2009
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
generified NumberUtils signature
上级
8669b329
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
31 addition
and
26 deletion
+31
-26
org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CustomNumberEditor.java
...ngframework/beans/propertyeditors/CustomNumberEditor.java
+3
-1
org.springframework.core/src/main/java/org/springframework/util/NumberUtils.java
...e/src/main/java/org/springframework/util/NumberUtils.java
+24
-22
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java
.../org/springframework/jdbc/core/SingleColumnRowMapper.java
+2
-1
org.springframework.transaction/src/main/java/org/springframework/dao/support/DataAccessUtils.java
...java/org/springframework/dao/support/DataAccessUtils.java
+2
-2
未找到文件。
org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CustomNumberEditor.java
浏览文件 @
fb057bb5
/*
* Copyright 2002-200
8
the original author or authors.
* Copyright 2002-200
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.
...
...
@@ -101,6 +101,7 @@ public class CustomNumberEditor extends PropertyEditorSupport {
* Parse the Number from the given text, using the specified NumberFormat.
*/
@Override
@SuppressWarnings
(
"unchecked"
)
public
void
setAsText
(
String
text
)
throws
IllegalArgumentException
{
if
(
this
.
allowEmpty
&&
!
StringUtils
.
hasText
(
text
))
{
// Treat empty String as null value.
...
...
@@ -120,6 +121,7 @@ public class CustomNumberEditor extends PropertyEditorSupport {
* Coerce a Number value into the required target class, if necessary.
*/
@Override
@SuppressWarnings
(
"unchecked"
)
public
void
setValue
(
Object
value
)
{
if
(
value
instanceof
Number
)
{
super
.
setValue
(
NumberUtils
.
convertNumberToTargetClass
((
Number
)
value
,
this
.
numberClass
));
...
...
org.springframework.core/src/main/java/org/springframework/util/NumberUtils.java
浏览文件 @
fb057bb5
/*
* Copyright 2002-200
8
the original author or authors.
* Copyright 2002-200
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.
...
...
@@ -49,59 +49,60 @@ public abstract class NumberUtils {
* @see java.lang.Double
* @see java.math.BigDecimal
*/
public
static
Number
convertNumberToTargetClass
(
Number
number
,
Class
targetClass
)
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
extends
Number
>
T
convertNumberToTargetClass
(
Number
number
,
Class
<
T
>
targetClass
)
throws
IllegalArgumentException
{
Assert
.
notNull
(
number
,
"Number must not be null"
);
Assert
.
notNull
(
targetClass
,
"Target class must not be null"
);
if
(
targetClass
.
isInstance
(
number
))
{
return
number
;
return
(
T
)
number
;
}
else
if
(
targetClass
.
equals
(
Byte
.
class
))
{
long
value
=
number
.
longValue
();
if
(
value
<
Byte
.
MIN_VALUE
||
value
>
Byte
.
MAX_VALUE
)
{
raiseOverflowException
(
number
,
targetClass
);
}
return
number
.
byteValue
(
);
return
(
T
)
new
Byte
(
number
.
byteValue
()
);
}
else
if
(
targetClass
.
equals
(
Short
.
class
))
{
long
value
=
number
.
longValue
();
if
(
value
<
Short
.
MIN_VALUE
||
value
>
Short
.
MAX_VALUE
)
{
raiseOverflowException
(
number
,
targetClass
);
}
return
number
.
shortValue
(
);
return
(
T
)
new
Short
(
number
.
shortValue
()
);
}
else
if
(
targetClass
.
equals
(
Integer
.
class
))
{
long
value
=
number
.
longValue
();
if
(
value
<
Integer
.
MIN_VALUE
||
value
>
Integer
.
MAX_VALUE
)
{
raiseOverflowException
(
number
,
targetClass
);
}
return
number
.
intValue
(
);
return
(
T
)
new
Integer
(
number
.
intValue
()
);
}
else
if
(
targetClass
.
equals
(
Long
.
class
))
{
return
number
.
longValue
(
);
return
(
T
)
new
Long
(
number
.
longValue
()
);
}
else
if
(
targetClass
.
equals
(
BigInteger
.
class
))
{
if
(
number
instanceof
BigDecimal
)
{
// do not lose precision - use BigDecimal's own conversion
return
((
BigDecimal
)
number
).
toBigInteger
();
return
(
T
)
(
(
BigDecimal
)
number
).
toBigInteger
();
}
else
{
// original value is not a Big* number - use standard long conversion
return
BigInteger
.
valueOf
(
number
.
longValue
());
return
(
T
)
BigInteger
.
valueOf
(
number
.
longValue
());
}
}
else
if
(
targetClass
.
equals
(
Float
.
class
))
{
return
number
.
floatValue
(
);
return
(
T
)
new
Float
(
number
.
floatValue
()
);
}
else
if
(
targetClass
.
equals
(
Double
.
class
))
{
return
number
.
doubleValue
(
);
return
(
T
)
new
Double
(
number
.
doubleValue
()
);
}
else
if
(
targetClass
.
equals
(
BigDecimal
.
class
))
{
// always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
// (see BigDecimal javadoc for details)
return
new
BigDecimal
(
number
.
toString
());
return
(
T
)
new
BigDecimal
(
number
.
toString
());
}
else
{
throw
new
IllegalArgumentException
(
"Could not convert number ["
+
number
+
"] of type ["
+
...
...
@@ -138,34 +139,35 @@ public abstract class NumberUtils {
* @see java.lang.Double#valueOf
* @see java.math.BigDecimal#BigDecimal(String)
*/
public
static
Number
parseNumber
(
String
text
,
Class
targetClass
)
{
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
extends
Number
>
T
parseNumber
(
String
text
,
Class
<
T
>
targetClass
)
{
Assert
.
notNull
(
text
,
"Text must not be null"
);
Assert
.
notNull
(
targetClass
,
"Target class must not be null"
);
String
trimmed
=
StringUtils
.
trimAllWhitespace
(
text
);
if
(
targetClass
.
equals
(
Byte
.
class
))
{
return
(
isHexNumber
(
trimmed
)
?
Byte
.
decode
(
trimmed
)
:
Byte
.
valueOf
(
trimmed
));
return
(
T
)
(
isHexNumber
(
trimmed
)
?
Byte
.
decode
(
trimmed
)
:
Byte
.
valueOf
(
trimmed
));
}
else
if
(
targetClass
.
equals
(
Short
.
class
))
{
return
(
isHexNumber
(
trimmed
)
?
Short
.
decode
(
trimmed
)
:
Short
.
valueOf
(
trimmed
));
return
(
T
)
(
isHexNumber
(
trimmed
)
?
Short
.
decode
(
trimmed
)
:
Short
.
valueOf
(
trimmed
));
}
else
if
(
targetClass
.
equals
(
Integer
.
class
))
{
return
(
isHexNumber
(
trimmed
)
?
Integer
.
decode
(
trimmed
)
:
Integer
.
valueOf
(
trimmed
));
return
(
T
)
(
isHexNumber
(
trimmed
)
?
Integer
.
decode
(
trimmed
)
:
Integer
.
valueOf
(
trimmed
));
}
else
if
(
targetClass
.
equals
(
Long
.
class
))
{
return
(
isHexNumber
(
trimmed
)
?
Long
.
decode
(
trimmed
)
:
Long
.
valueOf
(
trimmed
));
return
(
T
)
(
isHexNumber
(
trimmed
)
?
Long
.
decode
(
trimmed
)
:
Long
.
valueOf
(
trimmed
));
}
else
if
(
targetClass
.
equals
(
BigInteger
.
class
))
{
return
(
isHexNumber
(
trimmed
)
?
decodeBigInteger
(
trimmed
)
:
new
BigInteger
(
trimmed
));
return
(
T
)
(
isHexNumber
(
trimmed
)
?
decodeBigInteger
(
trimmed
)
:
new
BigInteger
(
trimmed
));
}
else
if
(
targetClass
.
equals
(
Float
.
class
))
{
return
Float
.
valueOf
(
trimmed
);
return
(
T
)
Float
.
valueOf
(
trimmed
);
}
else
if
(
targetClass
.
equals
(
Double
.
class
))
{
return
Double
.
valueOf
(
trimmed
);
return
(
T
)
Double
.
valueOf
(
trimmed
);
}
else
if
(
targetClass
.
equals
(
BigDecimal
.
class
)
||
targetClass
.
equals
(
Number
.
class
))
{
return
new
BigDecimal
(
trimmed
);
return
(
T
)
new
BigDecimal
(
trimmed
);
}
else
{
throw
new
IllegalArgumentException
(
...
...
@@ -188,7 +190,7 @@ public abstract class NumberUtils {
* @see #convertNumberToTargetClass
* @see #parseNumber(String, Class)
*/
public
static
Number
parseNumber
(
String
text
,
Class
targetClass
,
NumberFormat
numberFormat
)
{
public
static
<
T
extends
Number
>
T
parseNumber
(
String
text
,
Class
<
T
>
targetClass
,
NumberFormat
numberFormat
)
{
if
(
numberFormat
!=
null
)
{
Assert
.
notNull
(
text
,
"Text must not be null"
);
Assert
.
notNull
(
targetClass
,
"Target class must not be null"
);
...
...
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java
浏览文件 @
fb057bb5
/*
* Copyright 2002-200
8
the original author or authors.
* Copyright 2002-200
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.
...
...
@@ -162,6 +162,7 @@ public class SingleColumnRowMapper<T> implements RowMapper<T> {
* @return the converted value
* @see #getColumnValue(java.sql.ResultSet, int, Class)
*/
@SuppressWarnings
(
"unchecked"
)
protected
Object
convertValueToRequiredType
(
Object
value
,
Class
requiredType
)
{
if
(
String
.
class
.
equals
(
requiredType
))
{
return
value
.
toString
();
...
...
org.springframework.transaction/src/main/java/org/springframework/dao/support/DataAccessUtils.java
浏览文件 @
fb057bb5
/*
* Copyright 2002-200
8
the original author or authors.
* Copyright 2002-200
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.
...
...
@@ -144,7 +144,7 @@ public abstract class DataAccessUtils {
}
else
if
(
Number
.
class
.
isAssignableFrom
(
requiredType
)
&&
Number
.
class
.
isInstance
(
result
))
{
try
{
result
=
NumberUtils
.
convertNumberToTargetClass
(((
Number
)
result
),
requiredType
);
result
=
NumberUtils
.
convertNumberToTargetClass
(((
Number
)
result
),
(
Class
<?
extends
Number
>)
requiredType
);
}
catch
(
IllegalArgumentException
ex
)
{
throw
new
TypeMismatchDataAccessException
(
ex
.
getMessage
());
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录