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

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

Reviewed-by: darcy, weijun
上级 e3b58020
...@@ -70,11 +70,11 @@ public class PrintStream extends FilterOutputStream ...@@ -70,11 +70,11 @@ public class PrintStream extends FilterOutputStream
private OutputStreamWriter charOut; private OutputStreamWriter charOut;
/** /**
* nonNull is explicitly declared here so as not to create an extra * requireNonNull is explicitly declared here so as not to create an extra
* dependency on java.util.Objects.nonNull. PrintStream is loaded * dependency on java.util.Objects.requireNonNull. PrintStream is loaded
* early during system initialization. * 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) if (obj == null)
throw new NullPointerException(message); throw new NullPointerException(message);
return obj; return obj;
...@@ -88,7 +88,7 @@ public class PrintStream extends FilterOutputStream ...@@ -88,7 +88,7 @@ public class PrintStream extends FilterOutputStream
private static Charset toCharset(String csn) private static Charset toCharset(String csn)
throws UnsupportedEncodingException throws UnsupportedEncodingException
{ {
nonNull(csn, "charsetName"); requireNonNull(csn, "charsetName");
try { try {
return Charset.forName(csn); return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
...@@ -148,7 +148,7 @@ public class PrintStream extends FilterOutputStream ...@@ -148,7 +148,7 @@ public class PrintStream extends FilterOutputStream
* @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean) * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
*/ */
public PrintStream(OutputStream out, boolean autoFlush) { 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 ...@@ -173,7 +173,7 @@ public class PrintStream extends FilterOutputStream
throws UnsupportedEncodingException throws UnsupportedEncodingException
{ {
this(autoFlush, this(autoFlush,
nonNull(out, "Null output stream"), requireNonNull(out, "Null output stream"),
toCharset(encoding)); toCharset(encoding));
} }
......
...@@ -82,7 +82,7 @@ public class PrintWriter extends Writer { ...@@ -82,7 +82,7 @@ public class PrintWriter extends Writer {
private static Charset toCharset(String csn) private static Charset toCharset(String csn)
throws UnsupportedEncodingException throws UnsupportedEncodingException
{ {
Objects.nonNull(csn, "charsetName"); Objects.requireNonNull(csn, "charsetName");
try { try {
return Charset.forName(csn); return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
......
...@@ -68,8 +68,8 @@ public final class StackTraceElement implements java.io.Serializable { ...@@ -68,8 +68,8 @@ public final class StackTraceElement implements java.io.Serializable {
*/ */
public StackTraceElement(String declaringClass, String methodName, public StackTraceElement(String declaringClass, String methodName,
String fileName, int lineNumber) { String fileName, int lineNumber) {
this.declaringClass = Objects.nonNull(declaringClass, "Declaring class is null"); this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null");
this.methodName = Objects.nonNull(methodName, "Method name is null"); this.methodName = Objects.requireNonNull(methodName, "Method name is null");
this.fileName = fileName; this.fileName = fileName;
this.lineNumber = lineNumber; this.lineNumber = lineNumber;
} }
......
...@@ -56,7 +56,7 @@ public final class DirectoryIteratorException ...@@ -56,7 +56,7 @@ public final class DirectoryIteratorException
* if the cause is {@code null} * if the cause is {@code null}
*/ */
public DirectoryIteratorException(IOException cause) { public DirectoryIteratorException(IOException cause) {
super(Objects.nonNull(cause)); super(Objects.requireNonNull(cause));
} }
/** /**
......
...@@ -69,8 +69,7 @@ class FileTreeWalker { ...@@ -69,8 +69,7 @@ class FileTreeWalker {
FileVisitResult result = walk(start, FileVisitResult result = walk(start,
0, 0,
new ArrayList<AncestorDirectory>()); new ArrayList<AncestorDirectory>());
if (result == null) Objects.requireNonNull(result, "FileVisitor returned null");
throw new NullPointerException("FileVisitor returned null");
} }
/** /**
......
...@@ -2779,7 +2779,7 @@ public final class Files { ...@@ -2779,7 +2779,7 @@ public final class Files {
throws IOException throws IOException
{ {
// ensure not null before opening file // ensure not null before opening file
Objects.nonNull(in); Objects.requireNonNull(in);
// check for REPLACE_EXISTING // check for REPLACE_EXISTING
boolean replaceExisting = false; boolean replaceExisting = false;
...@@ -2861,7 +2861,7 @@ public final class Files { ...@@ -2861,7 +2861,7 @@ public final class Files {
*/ */
public static long copy(Path source, OutputStream out) throws IOException { public static long copy(Path source, OutputStream out) throws IOException {
// ensure not null before opening file // ensure not null before opening file
Objects.nonNull(out); Objects.requireNonNull(out);
try (InputStream in = newInputStream(source)) { try (InputStream in = newInputStream(source)) {
return copy(in, out); return copy(in, out);
...@@ -3035,7 +3035,7 @@ public final class Files { ...@@ -3035,7 +3035,7 @@ public final class Files {
throws IOException throws IOException
{ {
// ensure bytes is not null before opening file // ensure bytes is not null before opening file
Objects.nonNull(bytes); Objects.requireNonNull(bytes);
try (OutputStream out = Files.newOutputStream(path, options)) { try (OutputStream out = Files.newOutputStream(path, options)) {
int len = bytes.length; int len = bytes.length;
...@@ -3094,7 +3094,7 @@ public final class Files { ...@@ -3094,7 +3094,7 @@ public final class Files {
throws IOException throws IOException
{ {
// ensure lines is not null before opening file // ensure lines is not null before opening file
Objects.nonNull(lines); Objects.requireNonNull(lines);
CharsetEncoder encoder = cs.newEncoder(); CharsetEncoder encoder = cs.newEncoder();
OutputStream out = newOutputStream(path, options); OutputStream out = newOutputStream(path, options);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
......
...@@ -57,8 +57,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -57,8 +57,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)
throws IOException throws IOException
{ {
Objects.nonNull(dir); Objects.requireNonNull(dir);
Objects.nonNull(attrs); Objects.requireNonNull(attrs);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
...@@ -72,8 +72,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -72,8 +72,8 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
public FileVisitResult visitFile(T file, BasicFileAttributes attrs) public FileVisitResult visitFile(T file, BasicFileAttributes attrs)
throws IOException throws IOException
{ {
Objects.nonNull(file); Objects.requireNonNull(file);
Objects.nonNull(attrs); Objects.requireNonNull(attrs);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
...@@ -87,7 +87,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -87,7 +87,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
public FileVisitResult visitFileFailed(T file, IOException exc) public FileVisitResult visitFileFailed(T file, IOException exc)
throws IOException throws IOException
{ {
Objects.nonNull(file); Objects.requireNonNull(file);
throw exc; throw exc;
} }
...@@ -104,7 +104,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> { ...@@ -104,7 +104,7 @@ public class SimpleFileVisitor<T> implements FileVisitor<T> {
public FileVisitResult postVisitDirectory(T dir, IOException exc) public FileVisitResult postVisitDirectory(T dir, IOException exc)
throws IOException throws IOException
{ {
Objects.nonNull(dir); Objects.requireNonNull(dir);
if (exc != null) if (exc != null)
throw exc; throw exc;
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
......
...@@ -47,9 +47,6 @@ import java.text.DateFormatSymbols; ...@@ -47,9 +47,6 @@ import java.text.DateFormatSymbols;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.NumberFormat; 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.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -1859,7 +1856,7 @@ public final class Formatter implements Closeable, Flushable { ...@@ -1859,7 +1856,7 @@ public final class Formatter implements Closeable, Flushable {
private static Charset toCharset(String csn) private static Charset toCharset(String csn)
throws UnsupportedEncodingException throws UnsupportedEncodingException
{ {
Objects.nonNull(csn, "charsetName"); Objects.requireNonNull(csn, "charsetName");
try { try {
return Charset.forName(csn); return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
...@@ -2179,7 +2176,7 @@ public final class Formatter implements Closeable, Flushable { ...@@ -2179,7 +2176,7 @@ public final class Formatter implements Closeable, Flushable {
*/ */
public Formatter(PrintStream ps) { public Formatter(PrintStream ps) {
this(Locale.getDefault(Locale.Category.FORMAT), this(Locale.getDefault(Locale.Category.FORMAT),
(Appendable)Objects.nonNull(ps)); (Appendable)Objects.requireNonNull(ps));
} }
/** /**
......
...@@ -187,7 +187,7 @@ public final class Objects { ...@@ -187,7 +187,7 @@ public final class Objects {
* and constructors, as demonstrated below: * and constructors, as demonstrated below:
* <blockquote><pre> * <blockquote><pre>
* public Foo(Bar bar) { * public Foo(Bar bar) {
* this.bar = Objects.nonNull(bar); * this.bar = Objects.requireNonNull(bar);
* } * }
* </pre></blockquote> * </pre></blockquote>
* *
...@@ -196,7 +196,7 @@ public final class Objects { ...@@ -196,7 +196,7 @@ public final class Objects {
* @return {@code obj} if not {@code null} * @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@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) if (obj == null)
throw new NullPointerException(); throw new NullPointerException();
return obj; return obj;
...@@ -209,8 +209,8 @@ public final class Objects { ...@@ -209,8 +209,8 @@ public final class Objects {
* constructors with multiple parameters, as demonstrated below: * constructors with multiple parameters, as demonstrated below:
* <blockquote><pre> * <blockquote><pre>
* public Foo(Bar bar, Baz baz) { * public Foo(Bar bar, Baz baz) {
* this.bar = Objects.nonNull(bar, "bar must not be null"); * this.bar = Objects.requireNonNull(bar, "bar must not be null");
* this.baz = Objects.nonNull(baz, "baz must not be null"); * this.baz = Objects.requireNonNull(baz, "baz must not be null");
* } * }
* </pre></blockquote> * </pre></blockquote>
* *
...@@ -221,7 +221,7 @@ public final class Objects { ...@@ -221,7 +221,7 @@ public final class Objects {
* @return {@code obj} if not {@code null} * @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@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) if (obj == null)
throw new NullPointerException(message); throw new NullPointerException(message);
return obj; return obj;
......
...@@ -35,6 +35,7 @@ import java.nio.channels.*; ...@@ -35,6 +35,7 @@ import java.nio.channels.*;
import java.nio.charset.*; import java.nio.charset.*;
import java.text.*; import java.text.*;
import java.util.Locale; import java.util.Locale;
import sun.misc.LRUCache; import sun.misc.LRUCache;
/** /**
...@@ -592,7 +593,7 @@ public final class Scanner implements Iterator<String>, Closeable { ...@@ -592,7 +593,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* interface * interface
*/ */
public Scanner(Readable source) { 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 { ...@@ -619,7 +620,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* does not exist * does not exist
*/ */
public Scanner(InputStream source, String charsetName) { public Scanner(InputStream source, String charsetName) {
this(makeReadable(Objects.nonNull(source, "source"), toCharset(charsetName)), this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
WHITESPACE_PATTERN); WHITESPACE_PATTERN);
} }
...@@ -629,7 +630,7 @@ public final class Scanner implements Iterator<String>, Closeable { ...@@ -629,7 +630,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* @throws IllegalArgumentException if the charset is not supported * @throws IllegalArgumentException if the charset is not supported
*/ */
private static Charset toCharset(String csn) { private static Charset toCharset(String csn) {
Objects.nonNull(csn, "charsetName"); Objects.requireNonNull(csn, "charsetName");
try { try {
return Charset.forName(csn); return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException e) { } catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
...@@ -670,7 +671,7 @@ public final class Scanner implements Iterator<String>, Closeable { ...@@ -670,7 +671,7 @@ public final class Scanner implements Iterator<String>, Closeable {
public Scanner(File source, String charsetName) public Scanner(File source, String charsetName)
throws FileNotFoundException throws FileNotFoundException
{ {
this(Objects.nonNull(source), toDecoder(charsetName)); this(Objects.requireNonNull(source), toDecoder(charsetName));
} }
private Scanner(File source, CharsetDecoder dec) private Scanner(File source, CharsetDecoder dec)
...@@ -680,7 +681,7 @@ public final class Scanner implements Iterator<String>, Closeable { ...@@ -680,7 +681,7 @@ public final class Scanner implements Iterator<String>, Closeable {
} }
private static CharsetDecoder toDecoder(String charsetName) { private static CharsetDecoder toDecoder(String charsetName) {
Objects.nonNull(charsetName, "charsetName"); Objects.requireNonNull(charsetName, "charsetName");
try { try {
return Charset.forName(charsetName).newDecoder(); return Charset.forName(charsetName).newDecoder();
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
...@@ -729,7 +730,7 @@ public final class Scanner implements Iterator<String>, Closeable { ...@@ -729,7 +730,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* @since 1.7 * @since 1.7
*/ */
public Scanner(Path source, String charsetName) throws IOException { 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 { private Scanner(Path source, Charset charset) throws IOException {
...@@ -755,7 +756,7 @@ public final class Scanner implements Iterator<String>, Closeable { ...@@ -755,7 +756,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* @param source A channel to scan * @param source A channel to scan
*/ */
public Scanner(ReadableByteChannel source) { public Scanner(ReadableByteChannel source) {
this(makeReadable(Objects.nonNull(source, "source")), this(makeReadable(Objects.requireNonNull(source, "source")),
WHITESPACE_PATTERN); WHITESPACE_PATTERN);
} }
...@@ -775,7 +776,7 @@ public final class Scanner implements Iterator<String>, Closeable { ...@@ -775,7 +776,7 @@ public final class Scanner implements Iterator<String>, Closeable {
* does not exist * does not exist
*/ */
public Scanner(ReadableByteChannel source, String charsetName) { public Scanner(ReadableByteChannel source, String charsetName) {
this(makeReadable(Objects.nonNull(source, "source"), toDecoder(charsetName)), this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),
WHITESPACE_PATTERN); WHITESPACE_PATTERN);
} }
......
...@@ -173,7 +173,7 @@ class KrbAsRep extends KrbKdcRep { ...@@ -173,7 +173,7 @@ class KrbAsRep extends KrbKdcRep {
} }
Credentials getCreds() { 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() { sun.security.krb5.internal.ccache.Credentials getCCreds() {
......
...@@ -164,7 +164,7 @@ public class BasicObjectsTest { ...@@ -164,7 +164,7 @@ public class BasicObjectsTest {
// Test 1-arg variant // Test 1-arg variant
try { try {
s = Objects.nonNull("pants"); s = Objects.requireNonNull("pants");
if (s != "pants") { if (s != "pants") {
System.err.printf("1-arg non-null failed to return its arg"); System.err.printf("1-arg non-null failed to return its arg");
errors++; errors++;
...@@ -175,7 +175,7 @@ public class BasicObjectsTest { ...@@ -175,7 +175,7 @@ public class BasicObjectsTest {
} }
try { try {
s = Objects.nonNull(null); s = Objects.requireNonNull(null);
System.err.printf("1-arg nonNull failed to throw NPE"); System.err.printf("1-arg nonNull failed to throw NPE");
errors++; errors++;
} catch (NullPointerException e) { } catch (NullPointerException e) {
...@@ -184,7 +184,7 @@ public class BasicObjectsTest { ...@@ -184,7 +184,7 @@ public class BasicObjectsTest {
// Test 2-arg variant // Test 2-arg variant
try { try {
s = Objects.nonNull("pants", "trousers"); s = Objects.requireNonNull("pants", "trousers");
if (s != "pants") { if (s != "pants") {
System.err.printf("2-arg nonNull failed to return its arg"); System.err.printf("2-arg nonNull failed to return its arg");
errors++; errors++;
...@@ -195,7 +195,7 @@ public class BasicObjectsTest { ...@@ -195,7 +195,7 @@ public class BasicObjectsTest {
} }
try { try {
s = Objects.nonNull(null, "pantaloons"); s = Objects.requireNonNull(null, "pantaloons");
System.err.printf("2-arg nonNull failed to throw NPE"); System.err.printf("2-arg nonNull failed to throw NPE");
errors++; errors++;
} catch (NullPointerException e) { } catch (NullPointerException e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册