Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
aa97329a
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
aa97329a
编写于
3月 24, 2009
作者:
A
alanb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6807702: Integer.valueOf cache should be configurable
Reviewed-by: darcy
上级
07b65aba
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
124 addition
and
19 deletion
+124
-19
src/share/classes/java/lang/Integer.java
src/share/classes/java/lang/Integer.java
+58
-15
src/share/classes/java/lang/Long.java
src/share/classes/java/lang/Long.java
+4
-4
src/share/classes/java/lang/System.java
src/share/classes/java/lang/System.java
+7
-0
test/java/lang/Integer/ValueOf.java
test/java/lang/Integer/ValueOf.java
+55
-0
未找到文件。
src/share/classes/java/lang/Integer.java
浏览文件 @
aa97329a
...
...
@@ -25,6 +25,8 @@
package
java.lang
;
import
java.util.Properties
;
/**
* The {@code Integer} class wraps a value of the primitive type
* {@code int} in an object. An object of type {@code Integer}
...
...
@@ -442,6 +444,12 @@ public final class Integer extends Number implements Comparable<Integer> {
public
static
int
parseInt
(
String
s
,
int
radix
)
throws
NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
if
(
s
==
null
)
{
throw
new
NumberFormatException
(
"null"
);
}
...
...
@@ -545,7 +553,7 @@ public final class Integer extends Number implements Comparable<Integer> {
* does not contain a parsable {@code int}.
*/
public
static
Integer
valueOf
(
String
s
,
int
radix
)
throws
NumberFormatException
{
return
new
Integer
(
parseInt
(
s
,
radix
));
return
Integer
.
valueOf
(
parseInt
(
s
,
radix
));
}
/**
...
...
@@ -570,20 +578,56 @@ public final class Integer extends Number implements Comparable<Integer> {
* @exception NumberFormatException if the string cannot be parsed
* as an integer.
*/
public
static
Integer
valueOf
(
String
s
)
throws
NumberFormatException
{
return
new
Integer
(
parseInt
(
s
,
10
));
public
static
Integer
valueOf
(
String
s
)
throws
NumberFormatException
{
return
Integer
.
valueOf
(
parseInt
(
s
,
10
));
}
private
static
class
IntegerCache
{
private
IntegerCache
(){}
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. During VM initialization the
* getAndRemoveCacheProperties method may be used to get and remove any system
* properites that configure the cache size. At this time, the size of the
* cache may be controlled by the -XX:AutoBoxCacheMax=<size> option.
*/
// value of java.lang.Integer.IntegerCache.high property (obtained during VM init)
private
static
String
integerCacheHighPropValue
;
static
final
Integer
cache
[]
=
new
Integer
[-(-
128
)
+
127
+
1
];
static
void
getAndRemoveCacheProperties
()
{
if
(!
sun
.
misc
.
VM
.
isBooted
())
{
Properties
props
=
System
.
getProperties
();
integerCacheHighPropValue
=
(
String
)
props
.
remove
(
"java.lang.Integer.IntegerCache.high"
);
if
(
integerCacheHighPropValue
!=
null
)
System
.
setProperties
(
props
);
// remove from system props
}
}
private
static
class
IntegerCache
{
static
final
int
low
=
-
128
;
static
final
int
high
;
static
final
Integer
cache
[];
static
{
for
(
int
i
=
0
;
i
<
cache
.
length
;
i
++)
cache
[
i
]
=
new
Integer
(
i
-
128
);
// high value may be configured by property
int
h
=
127
;
if
(
integerCacheHighPropValue
!=
null
)
{
int
i
=
parseInt
(
integerCacheHighPropValue
);
i
=
Math
.
max
(
i
,
127
);
// Maximum array size is Integer.MAX_VALUE
h
=
Math
.
min
(
i
,
Integer
.
MAX_VALUE
-
(-
low
));
}
high
=
h
;
cache
=
new
Integer
[(
high
-
low
)
+
1
];
int
j
=
low
;
for
(
int
k
=
0
;
k
<
cache
.
length
;
k
++)
cache
[
k
]
=
new
Integer
(
j
++);
}
private
IntegerCache
()
{}
}
/**
...
...
@@ -599,10 +643,9 @@ public final class Integer extends Number implements Comparable<Integer> {
* @since 1.5
*/
public
static
Integer
valueOf
(
int
i
)
{
final
int
offset
=
128
;
if
(
i
>=
-
128
&&
i
<=
127
)
{
// must cache
return
IntegerCache
.
cache
[
i
+
offset
];
}
assert
IntegerCache
.
high
>=
127
;
if
(
i
>=
IntegerCache
.
low
&&
i
<=
IntegerCache
.
high
)
return
IntegerCache
.
cache
[
i
+
(-
IntegerCache
.
low
)];
return
new
Integer
(
i
);
}
...
...
@@ -806,7 +849,7 @@ public final class Integer extends Number implements Comparable<Integer> {
*/
public
static
Integer
getInteger
(
String
nm
,
int
val
)
{
Integer
result
=
getInteger
(
nm
,
null
);
return
(
result
==
null
)
?
new
Integer
(
val
)
:
result
;
return
(
result
==
null
)
?
Integer
.
valueOf
(
val
)
:
result
;
}
/**
...
...
@@ -938,7 +981,7 @@ public final class Integer extends Number implements Comparable<Integer> {
try
{
result
=
Integer
.
valueOf
(
nm
.
substring
(
index
),
radix
);
result
=
negative
?
new
Integer
(-
result
.
intValue
())
:
result
;
result
=
negative
?
Integer
.
valueOf
(-
result
.
intValue
())
:
result
;
}
catch
(
NumberFormatException
e
)
{
// If number is Integer.MIN_VALUE, we'll end up here. The next line
// handles this case, and causes any genuine format error to be
...
...
src/share/classes/java/lang/Long.java
浏览文件 @
aa97329a
...
...
@@ -510,7 +510,7 @@ public final class Long extends Number implements Comparable<Long> {
* contain a parsable {@code long}.
*/
public
static
Long
valueOf
(
String
s
,
int
radix
)
throws
NumberFormatException
{
return
new
Long
(
parseLong
(
s
,
radix
));
return
Long
.
valueOf
(
parseLong
(
s
,
radix
));
}
/**
...
...
@@ -537,7 +537,7 @@ public final class Long extends Number implements Comparable<Long> {
*/
public
static
Long
valueOf
(
String
s
)
throws
NumberFormatException
{
return
new
Long
(
parseLong
(
s
,
10
));
return
Long
.
valueOf
(
parseLong
(
s
,
10
));
}
private
static
class
LongCache
{
...
...
@@ -650,7 +650,7 @@ public final class Long extends Number implements Comparable<Long> {
try
{
result
=
Long
.
valueOf
(
nm
.
substring
(
index
),
radix
);
result
=
negative
?
new
Long
(-
result
.
longValue
())
:
result
;
result
=
negative
?
Long
.
valueOf
(-
result
.
longValue
())
:
result
;
}
catch
(
NumberFormatException
e
)
{
// If number is Long.MIN_VALUE, we'll end up here. The next line
// handles this case, and causes any genuine format error to be
...
...
@@ -869,7 +869,7 @@ public final class Long extends Number implements Comparable<Long> {
*/
public
static
Long
getLong
(
String
nm
,
long
val
)
{
Long
result
=
Long
.
getLong
(
nm
,
null
);
return
(
result
==
null
)
?
new
Long
(
val
)
:
result
;
return
(
result
==
null
)
?
Long
.
valueOf
(
val
)
:
result
;
}
/**
...
...
src/share/classes/java/lang/System.java
浏览文件 @
aa97329a
...
...
@@ -1105,6 +1105,13 @@ public final class System {
props
=
new
Properties
();
initProperties
(
props
);
sun
.
misc
.
Version
.
init
();
// Gets and removes system properties that configure the Integer
// cache used to support the object identity semantics of autoboxing.
// At this time, the size of the cache may be controlled by the
// -XX:AutoBoxCacheMax=<size> option.
Integer
.
getAndRemoveCacheProperties
();
FileInputStream
fdIn
=
new
FileInputStream
(
FileDescriptor
.
in
);
FileOutputStream
fdOut
=
new
FileOutputStream
(
FileDescriptor
.
out
);
FileOutputStream
fdErr
=
new
FileOutputStream
(
FileDescriptor
.
err
);
...
...
test/java/lang/Integer/ValueOf.java
0 → 100644
浏览文件 @
aa97329a
/*
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* @test
* @bug 6807702
* @summary Basic test for Integer.valueOf
* @run main ValueOf
* @run main/othervm -esa -XX:+AggressiveOpts ValueOf
*/
public
class
ValueOf
{
// test Integer.valueOf over this range (inclusive)
private
static
final
int
TEST_LOW
=
-
1024
;
private
static
final
int
TEST_HIGH
=
24576
;
public
static
void
main
(
String
[]
args
)
{
int
i
=
TEST_LOW
;
while
(
i
<=
TEST_HIGH
)
{
// check that valueOf stores i
if
(
Integer
.
valueOf
(
i
).
intValue
()
!=
i
)
throw
new
RuntimeException
();
// check that the same object is returned for integral values
// in the range -128 to 127 (inclusive)
if
(
i
>=
-
128
&&
i
<=
127
)
{
if
(
Integer
.
valueOf
(
i
)
!=
Integer
.
valueOf
(
i
))
throw
new
RuntimeException
();
}
i
++;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录