7012540: java.util.Objects.nonNull() incorrectly named
Reviewed-by: darcy, weijun
--- a/jdk/src/share/classes/java/io/PrintStream.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/io/PrintStream.java Wed Feb 02 13:13:34 2011 -0500
@@ -70,11 +70,11 @@
private OutputStreamWriter charOut;
/**
- * nonNull is explicitly declared here so as not to create an extra
- * dependency on java.util.Objects.nonNull. PrintStream is loaded
+ * requireNonNull is explicitly declared here so as not to create an extra
+ * dependency on java.util.Objects.requireNonNull. PrintStream is loaded
* early during system initialization.
*/
- private static <T> T nonNull(T obj, String message) {
+ private static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
@@ -88,7 +88,7 @@
private static Charset toCharset(String csn)
throws UnsupportedEncodingException
{
- nonNull(csn, "charsetName");
+ requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
@@ -148,7 +148,7 @@
* @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
*/
public PrintStream(OutputStream out, boolean autoFlush) {
- this(autoFlush, nonNull(out, "Null output stream"));
+ this(autoFlush, requireNonNull(out, "Null output stream"));
}
/**
@@ -173,7 +173,7 @@
throws UnsupportedEncodingException
{
this(autoFlush,
- nonNull(out, "Null output stream"),
+ requireNonNull(out, "Null output stream"),
toCharset(encoding));
}
--- a/jdk/src/share/classes/java/io/PrintWriter.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/io/PrintWriter.java Wed Feb 02 13:13:34 2011 -0500
@@ -82,7 +82,7 @@
private static Charset toCharset(String csn)
throws UnsupportedEncodingException
{
- Objects.nonNull(csn, "charsetName");
+ Objects.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
--- a/jdk/src/share/classes/java/lang/StackTraceElement.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/lang/StackTraceElement.java Wed Feb 02 13:13:34 2011 -0500
@@ -68,8 +68,8 @@
*/
public StackTraceElement(String declaringClass, String methodName,
String fileName, int lineNumber) {
- this.declaringClass = Objects.nonNull(declaringClass, "Declaring class is null");
- this.methodName = Objects.nonNull(methodName, "Method name is null");
+ this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null");
+ this.methodName = Objects.requireNonNull(methodName, "Method name is null");
this.fileName = fileName;
this.lineNumber = lineNumber;
}
--- a/jdk/src/share/classes/java/nio/file/DirectoryIteratorException.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/nio/file/DirectoryIteratorException.java Wed Feb 02 13:13:34 2011 -0500
@@ -56,7 +56,7 @@
* if the cause is {@code null}
*/
public DirectoryIteratorException(IOException cause) {
- super(Objects.nonNull(cause));
+ super(Objects.requireNonNull(cause));
}
/**
--- a/jdk/src/share/classes/java/nio/file/FileTreeWalker.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/nio/file/FileTreeWalker.java Wed Feb 02 13:13:34 2011 -0500
@@ -69,8 +69,7 @@
FileVisitResult result = walk(start,
0,
new ArrayList<AncestorDirectory>());
- if (result == null)
- throw new NullPointerException("FileVisitor returned null");
+ Objects.requireNonNull(result, "FileVisitor returned null");
}
/**
--- a/jdk/src/share/classes/java/nio/file/Files.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/nio/file/Files.java Wed Feb 02 13:13:34 2011 -0500
@@ -2779,7 +2779,7 @@
throws IOException
{
// ensure not null before opening file
- Objects.nonNull(in);
+ Objects.requireNonNull(in);
// check for REPLACE_EXISTING
boolean replaceExisting = false;
@@ -2861,7 +2861,7 @@
*/
public static long copy(Path source, OutputStream out) throws IOException {
// ensure not null before opening file
- Objects.nonNull(out);
+ Objects.requireNonNull(out);
try (InputStream in = newInputStream(source)) {
return copy(in, out);
@@ -3035,7 +3035,7 @@
throws IOException
{
// ensure bytes is not null before opening file
- Objects.nonNull(bytes);
+ Objects.requireNonNull(bytes);
try (OutputStream out = Files.newOutputStream(path, options)) {
int len = bytes.length;
@@ -3094,7 +3094,7 @@
throws IOException
{
// ensure lines is not null before opening file
- Objects.nonNull(lines);
+ Objects.requireNonNull(lines);
CharsetEncoder encoder = cs.newEncoder();
OutputStream out = newOutputStream(path, options);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
--- a/jdk/src/share/classes/java/nio/file/SimpleFileVisitor.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/nio/file/SimpleFileVisitor.java Wed Feb 02 13:13:34 2011 -0500
@@ -57,8 +57,8 @@
public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)
throws IOException
{
- Objects.nonNull(dir);
- Objects.nonNull(attrs);
+ Objects.requireNonNull(dir);
+ Objects.requireNonNull(attrs);
return FileVisitResult.CONTINUE;
}
@@ -72,8 +72,8 @@
public FileVisitResult visitFile(T file, BasicFileAttributes attrs)
throws IOException
{
- Objects.nonNull(file);
- Objects.nonNull(attrs);
+ Objects.requireNonNull(file);
+ Objects.requireNonNull(attrs);
return FileVisitResult.CONTINUE;
}
@@ -87,7 +87,7 @@
public FileVisitResult visitFileFailed(T file, IOException exc)
throws IOException
{
- Objects.nonNull(file);
+ Objects.requireNonNull(file);
throw exc;
}
@@ -104,7 +104,7 @@
public FileVisitResult postVisitDirectory(T dir, IOException exc)
throws IOException
{
- Objects.nonNull(dir);
+ Objects.requireNonNull(dir);
if (exc != null)
throw exc;
return FileVisitResult.CONTINUE;
--- a/jdk/src/share/classes/java/util/Formatter.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/util/Formatter.java Wed Feb 02 13:13:34 2011 -0500
@@ -47,9 +47,6 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -1859,7 +1856,7 @@
private static Charset toCharset(String csn)
throws UnsupportedEncodingException
{
- Objects.nonNull(csn, "charsetName");
+ Objects.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
@@ -2179,7 +2176,7 @@
*/
public Formatter(PrintStream ps) {
this(Locale.getDefault(Locale.Category.FORMAT),
- (Appendable)Objects.nonNull(ps));
+ (Appendable)Objects.requireNonNull(ps));
}
/**
--- a/jdk/src/share/classes/java/util/Objects.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/util/Objects.java Wed Feb 02 13:13:34 2011 -0500
@@ -187,7 +187,7 @@
* and constructors, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar) {
- * this.bar = Objects.nonNull(bar);
+ * this.bar = Objects.requireNonNull(bar);
* }
* </pre></blockquote>
*
@@ -196,7 +196,7 @@
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
- public static <T> T nonNull(T obj) {
+ public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
@@ -209,8 +209,8 @@
* constructors with multiple parameters, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar, Baz baz) {
- * this.bar = Objects.nonNull(bar, "bar must not be null");
- * this.baz = Objects.nonNull(baz, "baz must not be null");
+ * this.bar = Objects.requireNonNull(bar, "bar must not be null");
+ * this.baz = Objects.requireNonNull(baz, "baz must not be null");
* }
* </pre></blockquote>
*
@@ -221,7 +221,7 @@
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
- public static <T> T nonNull(T obj, String message) {
+ public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
--- a/jdk/src/share/classes/java/util/Scanner.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/java/util/Scanner.java Wed Feb 02 13:13:34 2011 -0500
@@ -35,6 +35,7 @@
import java.nio.charset.*;
import java.text.*;
import java.util.Locale;
+
import sun.misc.LRUCache;
/**
@@ -592,7 +593,7 @@
* interface
*/
public Scanner(Readable source) {
- this(Objects.nonNull(source, "source"), WHITESPACE_PATTERN);
+ this(Objects.requireNonNull(source, "source"), WHITESPACE_PATTERN);
}
/**
@@ -619,7 +620,7 @@
* does not exist
*/
public Scanner(InputStream source, String charsetName) {
- this(makeReadable(Objects.nonNull(source, "source"), toCharset(charsetName)),
+ this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
WHITESPACE_PATTERN);
}
@@ -629,7 +630,7 @@
* @throws IllegalArgumentException if the charset is not supported
*/
private static Charset toCharset(String csn) {
- Objects.nonNull(csn, "charsetName");
+ Objects.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
@@ -670,7 +671,7 @@
public Scanner(File source, String charsetName)
throws FileNotFoundException
{
- this(Objects.nonNull(source), toDecoder(charsetName));
+ this(Objects.requireNonNull(source), toDecoder(charsetName));
}
private Scanner(File source, CharsetDecoder dec)
@@ -680,7 +681,7 @@
}
private static CharsetDecoder toDecoder(String charsetName) {
- Objects.nonNull(charsetName, "charsetName");
+ Objects.requireNonNull(charsetName, "charsetName");
try {
return Charset.forName(charsetName).newDecoder();
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
@@ -729,7 +730,7 @@
* @since 1.7
*/
public Scanner(Path source, String charsetName) throws IOException {
- this(Objects.nonNull(source), toCharset(charsetName));
+ this(Objects.requireNonNull(source), toCharset(charsetName));
}
private Scanner(Path source, Charset charset) throws IOException {
@@ -755,7 +756,7 @@
* @param source A channel to scan
*/
public Scanner(ReadableByteChannel source) {
- this(makeReadable(Objects.nonNull(source, "source")),
+ this(makeReadable(Objects.requireNonNull(source, "source")),
WHITESPACE_PATTERN);
}
@@ -775,7 +776,7 @@
* does not exist
*/
public Scanner(ReadableByteChannel source, String charsetName) {
- this(makeReadable(Objects.nonNull(source, "source"), toDecoder(charsetName)),
+ this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),
WHITESPACE_PATTERN);
}
--- a/jdk/src/share/classes/sun/security/krb5/KrbAsRep.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/src/share/classes/sun/security/krb5/KrbAsRep.java Wed Feb 02 13:13:34 2011 -0500
@@ -173,7 +173,7 @@
}
Credentials getCreds() {
- return Objects.nonNull(creds, "Creds not available yet.");
+ return Objects.requireNonNull(creds, "Creds not available yet.");
}
sun.security.krb5.internal.ccache.Credentials getCCreds() {
--- a/jdk/test/java/util/Objects/BasicObjectsTest.java Tue Feb 01 14:20:01 2011 -0800
+++ b/jdk/test/java/util/Objects/BasicObjectsTest.java Wed Feb 02 13:13:34 2011 -0500
@@ -164,7 +164,7 @@
// Test 1-arg variant
try {
- s = Objects.nonNull("pants");
+ s = Objects.requireNonNull("pants");
if (s != "pants") {
System.err.printf("1-arg non-null failed to return its arg");
errors++;
@@ -175,7 +175,7 @@
}
try {
- s = Objects.nonNull(null);
+ s = Objects.requireNonNull(null);
System.err.printf("1-arg nonNull failed to throw NPE");
errors++;
} catch (NullPointerException e) {
@@ -184,7 +184,7 @@
// Test 2-arg variant
try {
- s = Objects.nonNull("pants", "trousers");
+ s = Objects.requireNonNull("pants", "trousers");
if (s != "pants") {
System.err.printf("2-arg nonNull failed to return its arg");
errors++;
@@ -195,7 +195,7 @@
}
try {
- s = Objects.nonNull(null, "pantaloons");
+ s = Objects.requireNonNull(null, "pantaloons");
System.err.printf("2-arg nonNull failed to throw NPE");
errors++;
} catch (NullPointerException e) {