Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
a0ce2c19
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看板
提交
a0ce2c19
编写于
4月 16, 2013
作者:
C
chegar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8012343: Objects.requireNonNull(Object,Supplier) breaks genstubs build
Reviewed-by: alanb
上级
81262cdc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
32 addition
and
66 deletion
+32
-66
src/share/classes/java/util/Objects.java
src/share/classes/java/util/Objects.java
+1
-29
test/java/util/Objects/BasicObjectsTest.java
test/java/util/Objects/BasicObjectsTest.java
+31
-37
未找到文件。
src/share/classes/java/util/Objects.java
浏览文件 @
a0ce2c19
/*
* Copyright (c) 2009, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 201
1
, Oracle and/or its affiliates. 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
...
...
@@ -25,8 +25,6 @@
package
java.util
;
import
java.util.function.Supplier
;
/**
* This class consists of {@code static} utility methods for operating
* on objects. These utilities include {@code null}-safe or {@code
...
...
@@ -228,30 +226,4 @@ public final class Objects {
throw
new
NullPointerException
(
message
);
return
obj
;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is.
*
* <p>Unlike the method {@link requireNonNull(Object, String},
* this method allows creation of the message to be deferred until
* after the null check is made. While this may confer a
* performance advantage in the non-null case, when deciding to
* call this method care should be taken that the costs of
* creating the message supplier are less than the cost of just
* creating the string message directly.
*
* @param obj the object reference to check for nullity
* @param messageSupplier supplier of the detail message to be
* used in the event that a {@code NullPointerException} is thrown
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
* @since 1.8
*/
public
static
<
T
>
T
requireNonNull
(
T
obj
,
Supplier
<
String
>
messageSupplier
)
{
if
(
obj
==
null
)
throw
new
NullPointerException
(
messageSupplier
.
get
());
return
obj
;
}
}
test/java/util/Objects/BasicObjectsTest.java
浏览文件 @
a0ce2c19
/*
* Copyright (c) 2009, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 201
1
, Oracle and/or its affiliates. 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
...
...
@@ -23,13 +23,12 @@
/*
* @test
* @bug 6797535 6889858 6891113
8011800
* @bug 6797535 6889858 6891113
* @summary Basic tests for methods in java.util.Objects
* @author Joseph D. Darcy
*/
import
java.util.*
;
import
java.util.function.*
;
public
class
BasicObjectsTest
{
public
static
void
main
(
String
...
args
)
{
...
...
@@ -41,7 +40,7 @@ public class BasicObjectsTest {
errors
+=
testToString
();
errors
+=
testToString2
();
errors
+=
testCompare
();
errors
+=
test
Require
NonNull
();
errors
+=
testNonNull
();
if
(
errors
>
0
)
throw
new
RuntimeException
();
}
...
...
@@ -159,54 +158,49 @@ public class BasicObjectsTest {
return
errors
;
}
private
static
int
test
Require
NonNull
()
{
private
static
int
testNonNull
()
{
int
errors
=
0
;
String
s
;
final
String
RNN_1
=
"1-arg requireNonNull"
;
final
String
RNN_2
=
"2-arg requireNonNull"
;
final
String
RNN_3
=
"Supplier requireNonNull"
;
Function
<
String
,
String
>
rnn1
=
s
->
Objects
.
requireNonNull
(
s
);
Function
<
String
,
String
>
rnn2
=
s
->
Objects
.
requireNonNull
(
s
,
"trousers"
)
;
Function
<
String
,
String
>
rnn3
=
s
->
Objects
.
requireNonNull
(
s
,
()
->
"trousers"
);
errors
+=
testRNN_NonNull
(
rnn1
,
RNN_1
);
errors
+=
testRNN_NonNull
(
rnn2
,
RNN_2
)
;
errors
+=
testRNN_NonNull
(
rnn3
,
RNN_3
);
// Test 1-arg variant
try
{
s
=
Objects
.
requireNonNull
(
"pants"
)
;
if
(
s
!=
"pants"
)
{
System
.
err
.
printf
(
"1-arg non-null failed to return its arg"
);
errors
++
;
}
}
catch
(
NullPointerException
e
)
{
System
.
err
.
printf
(
"1-arg nonNull threw unexpected NPE"
);
errors
++
;
}
errors
+=
testRNN_Null
(
rnn1
,
RNN_1
,
null
);
errors
+=
testRNN_Null
(
rnn2
,
RNN_2
,
"trousers"
);
errors
+=
testRNN_Null
(
rnn3
,
RNN_3
,
"trousers"
);
return
errors
;
}
try
{
s
=
Objects
.
requireNonNull
(
null
);
System
.
err
.
printf
(
"1-arg nonNull failed to throw NPE"
);
errors
++;
}
catch
(
NullPointerException
e
)
{
// Expected
}
private
static
int
testRNN_NonNull
(
Function
<
String
,
String
>
testFunc
,
String
testFuncName
)
{
int
errors
=
0
;
// Test 2-arg variant
try
{
String
s
=
testFunc
.
apply
(
"pant
s"
);
s
=
Objects
.
requireNonNull
(
"pants"
,
"trouser
s"
);
if
(
s
!=
"pants"
)
{
System
.
err
.
printf
(
testFuncName
+
"
failed to return its arg"
);
System
.
err
.
printf
(
"2-arg nonNull
failed to return its arg"
);
errors
++;
}
}
catch
(
NullPointerException
e
)
{
System
.
err
.
printf
(
testFuncName
+
"
threw unexpected NPE"
);
System
.
err
.
printf
(
"2-arg nonNull
threw unexpected NPE"
);
errors
++;
}
return
errors
;
}
private
static
int
testRNN_Null
(
Function
<
String
,
String
>
testFunc
,
String
testFuncName
,
String
expectedMessage
)
{
int
errors
=
0
;
try
{
String
s
=
testFunc
.
apply
(
null
);
System
.
err
.
printf
(
testFuncName
+
"
failed to throw NPE"
);
s
=
Objects
.
requireNonNull
(
null
,
"pantaloons"
);
System
.
err
.
printf
(
"2-arg nonNull
failed to throw NPE"
);
errors
++;
}
catch
(
NullPointerException
e
)
{
if
(
e
.
getMessage
()
!=
expectedMessage
)
{
System
.
err
.
printf
(
testFuncName
+
"
threw NPE w/ bad detail msg"
);
if
(
e
.
getMessage
()
!=
"pantaloons"
)
{
System
.
err
.
printf
(
"2-arg nonNull
threw NPE w/ bad detail msg"
);
errors
++;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录