提交 33a455f7 编写于 作者: B briangoetz

7012540: java.util.Objects.nonNull() incorrectly named

Reviewed-by: darcy, weijun
上级 e3b58020
......@@ -70,11 +70,11 @@ public class PrintStream extends FilterOutputStream
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 @@ public class PrintStream extends FilterOutputStream
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 @@ public class PrintStream extends FilterOutputStream
* @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 @@ public class PrintStream extends FilterOutputStream
throws UnsupportedEncodingException
{
this(autoFlush,
nonNull(out, "Null output stream"),
requireNonNull(out, "Null output stream"),
toCharset(encoding));
}
......
......@@ -82,7 +82,7 @@ public class PrintWriter extends Writer {
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) {
......
......@@ -68,8 +68,8 @@ public final class StackTraceElement implements java.io.Serializable {
*/
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;
}
......
......@@ -56,7 +56,7 @@ public final class DirectoryIteratorException
* if the cause is {@code null}
*/
public DirectoryIteratorException(IOException cause) {
super(Objects.nonNull(cause));
super(Objects.requireNonNull(cause));
}
/**
......
......@@ -69,8 +69,7 @@ class FileTreeWalker {
FileVisitResult result = walk(start,
0,
new ArrayList<AncestorDirectory>());
if (result == null)
throw new NullPointerException("FileVisitor returned null");
Objects.requireNonNull(result, "FileVisitor returned null");
}
/**
......
......@@ -2779,7 +2779,7 @@ public final class Files {
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 final class Files {
*/
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 @@ public final class Files {
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 @@ public final class Files {
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))) {
......
......@@ -57,8 +57,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
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 class SimpleFileVisitor<T> implements FileVisitor<T> {
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 class SimpleFileVisitor<T> implements FileVisitor<T> {
public FileVisitResult visitFileFailed(T file, IOException exc)
throws IOException
{
Objects.nonNull(file);
Objects.requireNonNull(file);
throw exc;
}
......@@ -104,7 +104,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
public FileVisitResult postVisitDirectory(T dir, IOException exc)
throws IOException
{
Objects.nonNull(dir);
Objects.requireNonNull(dir);
if (exc != null)
throw exc;
return FileVisitResult.CONTINUE;
......
......@@ -47,9 +47,6 @@ import java.text.DateFormatSymbols;
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 @@ public final class Formatter implements Closeable, Flushable {
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 final class Formatter implements Closeable, Flushable {
*/
public Formatter(PrintStream ps) {
this(Locale.getDefault(Locale.Category.FORMAT),
(Appendable)Objects.nonNull(ps));
(Appendable)Objects.requireNonNull(ps));
}
/**
......
......@@ -187,7 +187,7 @@ public final class Objects {
* 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 @@ public final class Objects {
* @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 @@ public final class Objects {
* 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 @@ public final class Objects {
* @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;
......
......@@ -35,6 +35,7 @@ import java.nio.channels.*;
import java.nio.charset.*;
import java.text.*;
import java.util.Locale;
import sun.misc.LRUCache;
/**
......@@ -592,7 +593,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* interface
*/
public Scanner(Readable source) {
this(Objects.nonNull(source, "source"), WHITESPACE_PATTERN);
this(Objects.requireNonNull(source, "source"), WHITESPACE_PATTERN);
}
/**
......@@ -619,7 +620,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* 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 @@ public final class Scanner implements Iterator<String>, Closeable {
* @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 final class Scanner implements Iterator<String>, Closeable {
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 @@ public final class Scanner implements Iterator<String>, Closeable {
}
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 @@ public final class Scanner implements Iterator<String>, Closeable {
* @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 @@ public final class Scanner implements Iterator<String>, Closeable {
* @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 @@ public final class Scanner implements Iterator<String>, Closeable {
* 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);
}
......
......@@ -173,7 +173,7 @@ class KrbAsRep extends KrbKdcRep {
}
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() {
......
......@@ -164,7 +164,7 @@ public class BasicObjectsTest {
// 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 @@ public class BasicObjectsTest {
}
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 @@ public class BasicObjectsTest {
// 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 @@ public class BasicObjectsTest {
}
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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册