8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull

Replace getClass() with approapriate NPE checks

Reviewed-by: jjg
This commit is contained in:
Aleksey Shipilev 2015-02-27 11:51:53 +00:00 committed by Maurizio Cimadamore
parent 44cc65ae49
commit ee906c96d7
30 changed files with 79 additions and 80 deletions

View File

@ -28,6 +28,7 @@ package javax.tools;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* Provides an easy way to collect diagnostics in a list.
@ -43,7 +44,7 @@ public final class DiagnosticCollector<S> implements DiagnosticListener<S> {
Collections.synchronizedList(new ArrayList<Diagnostic<? extends S>>());
public void report(Diagnostic<? extends S> diagnostic) {
diagnostic.getClass(); // null check
Objects.requireNonNull(diagnostic);
diagnostics.add(diagnostic);
}

View File

@ -31,6 +31,7 @@ import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import java.util.Objects;
/**
* Forwards calls to a given file object. Subclasses of this class
@ -53,8 +54,7 @@ public class ForwardingFileObject<F extends FileObject> implements FileObject {
* @param fileObject delegate to this file object
*/
protected ForwardingFileObject(F fileObject) {
fileObject.getClass(); // null check
this.fileObject = fileObject;
this.fileObject = Objects.requireNonNull(fileObject);
}
public URI toUri() {

View File

@ -27,6 +27,7 @@ package javax.tools;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import javax.tools.JavaFileObject.Kind;
@ -51,8 +52,7 @@ public class ForwardingJavaFileManager<M extends JavaFileManager> implements Jav
* @param fileManager delegate to this file manager
*/
protected ForwardingJavaFileManager(M fileManager) {
fileManager.getClass(); // null check
this.fileManager = fileManager;
this.fileManager = Objects.requireNonNull(fileManager);
}
/**

View File

@ -27,6 +27,7 @@ package javax.tools;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.Modifier;
import java.util.Objects;
/**
* File abstraction for tools operating on Java&trade; programming language
@ -78,8 +79,7 @@ public interface JavaFileObject extends FileObject {
*/
public final String extension;
private Kind(String extension) {
extension.getClass(); // null check
this.extension = extension;
this.extension = Objects.requireNonNull(extension);
}
}

View File

@ -28,6 +28,7 @@ package javax.tools;
import java.io.*;
import java.net.URI;
import java.nio.CharBuffer;
import java.util.Objects;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject.Kind;
@ -61,9 +62,8 @@ public class SimpleJavaFileObject implements JavaFileObject {
* @param kind the kind of this file object
*/
protected SimpleJavaFileObject(URI uri, Kind kind) {
// null checks
uri.getClass();
kind.getClass();
Objects.requireNonNull(uri);
Objects.requireNonNull(kind);
if (uri.getPath() == null)
throw new IllegalArgumentException("URI must have a path: " + uri);
this.uri = uri;

View File

@ -27,7 +27,9 @@ package com.sun.source.util;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import java.util.Iterator;
import java.util.Objects;
/**
* A path of tree nodes, typically used to represent the sequence of ancestor
@ -57,8 +59,8 @@ public class DocTreePath implements Iterable<DocTree> {
* @return a path identifying the target node
*/
public static DocTreePath getPath(DocTreePath path, DocTree target) {
path.getClass();
target.getClass();
Objects.requireNonNull(path); //null check
Objects.requireNonNull(target); //null check
class Result extends Error {
static final long serialVersionUID = -5942088234594905625L;
@ -96,11 +98,8 @@ public class DocTreePath implements Iterable<DocTree> {
* @param t the DocCommentTree to create the path for.
*/
public DocTreePath(TreePath treePath, DocCommentTree t) {
treePath.getClass();
t.getClass();
this.treePath = treePath;
this.docComment = t;
this.treePath = Objects.requireNonNull(treePath);
this.docComment = Objects.requireNonNull(t);
this.parent = null;
this.leaf = t;
}

View File

@ -26,6 +26,7 @@
package com.sun.source.util;
import java.util.Iterator;
import java.util.Objects;
import com.sun.source.tree.*;
@ -57,8 +58,8 @@ public class TreePath implements Iterable<Tree> {
* @return the tree path of the target node
*/
public static TreePath getPath(TreePath path, Tree target) {
path.getClass();
target.getClass();
Objects.requireNonNull(path);
Objects.requireNonNull(target);
class Result extends Error {
static final long serialVersionUID = -5942088234594905625L;

View File

@ -30,6 +30,7 @@ import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
/**
* <p><b>This is NOT part of any supported API.
@ -39,13 +40,9 @@ import java.io.InputStream;
*/
public class ClassReader {
ClassReader(ClassFile classFile, InputStream in, Attribute.Factory attributeFactory) throws IOException {
// null checks
classFile.getClass();
attributeFactory.getClass();
this.classFile = classFile;
this.classFile = Objects.requireNonNull(classFile);
this.attributeFactory = Objects.requireNonNull(attributeFactory);
this.in = new DataInputStream(new BufferedInputStream(in));
this.attributeFactory = attributeFactory;
}
ClassFile getClassFile() {

View File

@ -30,6 +30,7 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
@ -43,6 +44,7 @@ import com.sun.tools.classfile.Type.MethodType;
import com.sun.tools.classfile.Type.SimpleType;
import com.sun.tools.classfile.Type.TypeParamType;
import com.sun.tools.classfile.Type.WildcardType;
import static com.sun.tools.classfile.ConstantPool.*;
/**
@ -165,8 +167,7 @@ public class Dependencies {
* @param f the finder
*/
public void setFinder(Finder f) {
f.getClass(); // null check
finder = f;
finder = Objects.requireNonNull(f);
}
/**
@ -220,8 +221,7 @@ public class Dependencies {
* @param f the filter
*/
public void setFilter(Filter f) {
f.getClass(); // null check
filter = f;
filter = Objects.requireNonNull(f);
}
/**

View File

@ -81,6 +81,7 @@ import com.sun.source.util.DocTreePathScanner;
import com.sun.source.util.TreePath;
import com.sun.tools.doclint.HtmlTag.AttrKind;
import com.sun.tools.javac.tree.DocPretty;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.StringUtils;
@ -137,8 +138,7 @@ public class Checker extends DocTreePathScanner<Void, Void> {
// <editor-fold defaultstate="collapsed" desc="Top level">
Checker(Env env) {
env.getClass();
this.env = env;
this.env = Assert.checkNonNull(env);
tagStack = new LinkedList<>();
implicitHeaderLevel = env.implicitHeaderLevel;
}

View File

@ -45,6 +45,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.lang.model.element.Modifier;
@ -211,8 +212,7 @@ public class ClientCodeWrapper {
protected class WrappedJavaFileManager implements JavaFileManager {
protected JavaFileManager clientJavaFileManager;
WrappedJavaFileManager(JavaFileManager clientJavaFileManager) {
clientJavaFileManager.getClass(); // null check
this.clientJavaFileManager = clientJavaFileManager;
this.clientJavaFileManager = Objects.requireNonNull(clientJavaFileManager);
}
@Override @DefinedBy(Api.COMPILER)
@ -440,8 +440,7 @@ public class ClientCodeWrapper {
protected class WrappedFileObject implements FileObject {
protected FileObject clientFileObject;
WrappedFileObject(FileObject clientFileObject) {
clientFileObject.getClass(); // null check
this.clientFileObject = clientFileObject;
this.clientFileObject = Objects.requireNonNull(clientFileObject);
}
@Override @DefinedBy(Api.COMPILER)
@ -607,8 +606,7 @@ public class ClientCodeWrapper {
protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> {
protected DiagnosticListener<T> clientDiagnosticListener;
WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) {
clientDiagnosticListener.getClass(); // null check
this.clientDiagnosticListener = clientDiagnosticListener;
this.clientDiagnosticListener = Objects.requireNonNull(clientDiagnosticListener);
}
@Override @DefinedBy(Api.COMPILER)
@ -689,8 +687,7 @@ public class ClientCodeWrapper {
protected class WrappedTaskListener implements TaskListener {
protected TaskListener clientTaskListener;
WrappedTaskListener(TaskListener clientTaskListener) {
clientTaskListener.getClass(); // null check
this.clientTaskListener = clientTaskListener;
this.clientTaskListener = Objects.requireNonNull(clientTaskListener);
}
@Override @DefinedBy(Api.COMPILER_TREE)

View File

@ -34,8 +34,7 @@ import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.Assert;
/**
* Provides an implementation of Scope.
@ -67,8 +66,7 @@ public class JavacScope implements com.sun.source.tree.Scope {
protected final Env<AttrContext> env;
private JavacScope(Env<AttrContext> env) {
env.getClass(); // null-check
this.env = env;
this.env = Assert.checkNonNull(env);
}
@DefinedBy(Api.COMPILER_TREE)

View File

@ -108,7 +108,7 @@ public class JavacTaskImpl extends BasicJavacTask {
@Override @DefinedBy(Api.COMPILER)
public void setProcessors(Iterable<? extends Processor> processors) {
processors.getClass(); // null check
Objects.requireNonNull(processors);
// not mt-safe
if (used.get())
throw new IllegalStateException();

View File

@ -34,6 +34,7 @@ import java.nio.charset.Charset;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import javax.lang.model.SourceVersion;
@ -124,7 +125,7 @@ public final class JavacTool implements JavaCompiler {
if (options != null) {
for (String option : options)
option.getClass(); // null check
Objects.requireNonNull(option);
}
if (classes != null) {
@ -177,7 +178,7 @@ public final class JavacTool implements JavaCompiler {
if (err == null)
err = System.err;
for (String argument : arguments)
argument.getClass(); // null check
Objects.requireNonNull(argument);
return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
}

View File

@ -49,6 +49,7 @@ import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -682,8 +683,8 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
@Override @DefinedBy(Api.COMPILER)
public String inferBinaryName(Location location, JavaFileObject file) {
file.getClass(); // null check
location.getClass(); // null check
Objects.requireNonNull(file);
Objects.requireNonNull(location);
// Need to match the path semantics of list(location, ...)
Iterable<? extends Path> path = getLocationAsPaths(location);
if (path == null) {

View File

@ -44,6 +44,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -784,7 +785,7 @@ public class Locations {
}
protected LocationHandler getHandler(Location location) {
location.getClass(); // null check
Objects.requireNonNull(location);
return handlersForLocation.get(location);
}

View File

@ -40,6 +40,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.Normalizer;
import java.util.Objects;
import javax.tools.JavaFileObject;
@ -186,7 +187,7 @@ class RegularFileObject extends BaseFileObject {
@Override @DefinedBy(Api.COMPILER)
public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
cn.getClass();
Objects.requireNonNull(cn);
// null check
if (kind == Kind.OTHER && getKind() != kind) {
return false;

View File

@ -40,6 +40,7 @@ import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@ -254,7 +255,7 @@ public class ZipArchive implements Archive {
@Override @DefinedBy(Api.COMPILER)
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
cn.getClass();
Objects.requireNonNull(cn);
// null check
if (k == Kind.OTHER && getKind() != k) {
return false;

View File

@ -35,6 +35,7 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Set;
import javax.tools.JavaFileObject;
@ -206,7 +207,7 @@ public class ZipFileIndexArchive implements Archive {
@Override @DefinedBy(Api.COMPILER)
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
cn.getClass(); // null check
Objects.requireNonNull(cn);
if (k == Kind.OTHER && getKind() != k)
return false;
return name.equals(cn + k.extension);

View File

@ -39,6 +39,7 @@ import java.nio.charset.CharsetDecoder;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Objects;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
@ -151,10 +152,8 @@ public abstract class PathFileObject implements JavaFileObject {
}
protected PathFileObject(BaseFileManager fileManager, Path path) {
fileManager.getClass(); // null check
path.getClass(); // null check
this.fileManager = fileManager;
this.path = path;
this.fileManager = Objects.requireNonNull(fileManager);
this.path = Objects.requireNonNull(path);
}
public abstract String inferBinaryName(Iterable<? extends Path> paths);
@ -174,7 +173,7 @@ public abstract class PathFileObject implements JavaFileObject {
@Override @DefinedBy(Api.COMPILER)
public boolean isNameCompatible(String simpleName, Kind kind) {
simpleName.getClass();
Objects.requireNonNull(simpleName);
// null check
if (kind == Kind.OTHER && getKind() != kind) {
return false;

View File

@ -2443,8 +2443,7 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public TypeBoundKind kind;
public JCTree inner;
protected JCWildcard(TypeBoundKind kind, JCTree inner) {
kind.getClass(); // null-check
this.kind = kind;
this.kind = Assert.checkNonNull(kind);
this.inner = inner;
}
@Override

View File

@ -46,6 +46,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.tools.JavaFileManager;
@ -431,13 +432,12 @@ public abstract class BaseFileManager implements JavaFileManager {
}
protected static <T> T nullCheck(T o) {
o.getClass(); // null check
return o;
return Objects.requireNonNull(o);
}
protected static <T> Collection<T> nullCheck(Collection<T> it) {
for (T t : it)
t.getClass(); // null check
Objects.requireNonNull(t);
return it;
}
}

View File

@ -124,7 +124,7 @@ public class ListBuffer<A> extends AbstractQueue<A> {
/** Append an element to buffer.
*/
public ListBuffer<A> append(A x) {
x.getClass(); // null check
Assert.checkNonNull(x);
if (shared) copy();
List<A> newLast = List.<A>of(x);
if (last != null) {

View File

@ -341,7 +341,7 @@ public class Log extends AbstractLog {
}
public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) {
name.getClass(); // null check
Assert.checkNonNull(name);
getSource(name).setEndPosTable(endPosTable);
}
@ -373,7 +373,7 @@ public class Log extends AbstractLog {
}
public void setWriter(WriterKind kind, PrintWriter pw) {
pw.getClass();
Assert.checkNonNull(pw);
switch (kind) {
case NOTICE: noticeWriter = pw; break;
case WARNING: warnWriter = pw; break;
@ -383,8 +383,7 @@ public class Log extends AbstractLog {
}
public void setWriters(PrintWriter pw) {
pw.getClass();
noticeWriter = warnWriter = errWriter = pw;
noticeWriter = warnWriter = errWriter = Assert.checkNonNull(pw);
}
/**

View File

@ -43,6 +43,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.Set;
@ -268,7 +269,7 @@ public class JavahTask implements NativeHeaderTool.NativeHeaderTask {
this.classes = new ArrayList<>();
if (classes != null) {
for (String classname: classes) {
classname.getClass(); // null-check
Objects.requireNonNull(classname);
this.classes.add(classname);
}
}

View File

@ -62,6 +62,8 @@ import com.sun.tools.classfile.StackMap_attribute;
import com.sun.tools.classfile.Synthetic_attribute;
import static com.sun.tools.classfile.AccessFlags.*;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.StringUtils;
/*
@ -93,9 +95,8 @@ public class AttributeWriter extends BasicWriter
public void write(Object owner, Attribute attr, ConstantPool constant_pool) {
if (attr != null) {
// null checks
owner.getClass();
constant_pool.getClass();
Assert.checkNonNull(constant_pool);
Assert.checkNonNull(owner);
this.constant_pool = constant_pool;
this.owner = owner;
attr.accept(this, null);
@ -104,9 +105,8 @@ public class AttributeWriter extends BasicWriter
public void write(Object owner, Attributes attrs, ConstantPool constant_pool) {
if (attrs != null) {
// null checks
owner.getClass();
constant_pool.getClass();
Assert.checkNonNull(constant_pool);
Assert.checkNonNull(owner);
this.constant_pool = constant_pool;
this.owner = owner;
for (Attribute attr: attrs)

View File

@ -53,6 +53,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.ResourceBundle;
import javax.lang.model.element.Modifier;
@ -329,7 +330,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
this.classes = new ArrayList<>();
for (String classname: classes) {
classname.getClass(); // null-check
Objects.requireNonNull(classname);
this.classes.add(classname);
}

View File

@ -28,6 +28,7 @@ package com.sun.tools.doclets.internal.toolkit;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Objects;
import com.sun.tools.doclets.internal.toolkit.util.*;
@ -112,7 +113,6 @@ public abstract class Content {
* @return the reference type if not null or else throws a null pointer exception
*/
protected static <T> T nullCheck(T t) {
t.getClass();
return t;
return Objects.requireNonNull(t);
}
}

View File

@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
@ -139,8 +140,7 @@ public class Start extends ToolOption.Helper {
}
public Start(Context context) {
context.getClass(); // null check
this.context = context;
this.context = Objects.requireNonNull(context);
apiMode = true;
defaultDocletClassName = standardDocletClassName;
docletParentClassLoader = null;

View File

@ -34,6 +34,7 @@ import java.nio.charset.Charset;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import javax.lang.model.SourceVersion;
@ -89,7 +90,7 @@ public class JavadocTool implements DocumentationTool {
if (options != null) {
for (String option : options)
option.getClass(); // null check
Objects.requireNonNull(option);
}
if (compilationUnits != null) {