8073479: Replace obj.getClass hacks with Objects.requireNonNull

Reviewed-by: dfuchs, plevart, vlivanov
This commit is contained in:
Aleksey Shipilev 2015-02-20 18:32:10 +03:00
parent f3a755b2ab
commit e0e6ce31fd
10 changed files with 32 additions and 36 deletions

View File

@ -35,6 +35,7 @@ import static java.lang.invoke.MethodTypeForm.*;
import static java.lang.invoke.MethodHandleStatics.*;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.Objects;
import sun.invoke.util.ValueConversions;
import sun.invoke.util.VerifyType;
import sun.invoke.util.Wrapper;
@ -439,8 +440,7 @@ class DirectMethodHandle extends MethodHandle {
// Therefore, the only remaining check is for null.
// Since this check is *not* guaranteed by Unsafe.getInt
// and its siblings, we need to make an explicit one here.
obj.getClass(); // maybe throw NPE
return obj;
return Objects.requireNonNull(obj);
}
/** This subclass handles static field references. */
@ -468,8 +468,7 @@ class DirectMethodHandle extends MethodHandle {
@ForceInline
/*non-public*/ static Object nullCheck(Object obj) {
obj.getClass();
return obj;
return Objects.requireNonNull(obj);
}
@ForceInline

View File

@ -521,7 +521,7 @@ import java.util.Objects;
}
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Method m, boolean wantSpecial) {
m.getClass(); // NPE check
Objects.requireNonNull(m);
// fill in vmtarget, vmindex while we have m in hand:
MethodHandleNatives.init(this, m);
if (clazz == null) { // MHN.init failed
@ -600,7 +600,7 @@ import java.util.Objects;
/** Create a name for the given reflected constructor. The resulting name will be in a resolved state. */
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Constructor<?> ctor) {
ctor.getClass(); // NPE check
Objects.requireNonNull(ctor);
// fill in vmtarget, vmindex while we have ctor in hand:
MethodHandleNatives.init(this, ctor);
assert(isResolved() && this.clazz != null);
@ -615,7 +615,7 @@ import java.util.Objects;
}
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
fld.getClass(); // NPE check
Objects.requireNonNull(fld);
// fill in vmtarget, vmindex while we have fld in hand:
MethodHandleNatives.init(this, fld);
assert(isResolved() && this.clazz != null);

View File

@ -453,10 +453,8 @@ public abstract class MethodHandle {
*/
// @param type type (permanently assigned) of the new method handle
/*non-public*/ MethodHandle(MethodType type, LambdaForm form) {
type.getClass(); // explicit NPE
form.getClass(); // explicit NPE
this.type = type;
this.form = form.uncustomize();
this.type = Objects.requireNonNull(type);
this.form = Objects.requireNonNull(form).uncustomize();
this.form.prepare(); // TO DO: Try to delay this step until just before invocation.
}
@ -1171,7 +1169,7 @@ assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0)));
* @see #asFixedArity
*/
public MethodHandle asVarargsCollector(Class<?> arrayType) {
arrayType.getClass(); // explicit NPE
Objects.requireNonNull(arrayType);
boolean lastMatch = asCollectorChecks(arrayType, 0);
if (isVarargsCollector() && lastMatch)
return this;

View File

@ -29,6 +29,7 @@ import java.lang.reflect.*;
import java.util.BitSet;
import java.util.List;
import java.util.Arrays;
import java.util.Objects;
import sun.invoke.util.ValueConversions;
import sun.invoke.util.VerifyAccess;
@ -632,7 +633,7 @@ public class MethodHandles {
* @throws NullPointerException if the argument is null
*/
public Lookup in(Class<?> requestedLookupClass) {
requestedLookupClass.getClass(); // null check
Objects.requireNonNull(requestedLookupClass);
if (allowedModes == TRUSTED) // IMPL_LOOKUP can make any lookup at all
return new Lookup(requestedLookupClass, ALL_MODES);
if (requestedLookupClass == this.lookupClass)
@ -1367,16 +1368,16 @@ return mh1;
MemberName resolveOrFail(byte refKind, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
checkSymbolicClass(refc); // do this before attempting to resolve
name.getClass(); // NPE
type.getClass(); // NPE
Objects.requireNonNull(name);
Objects.requireNonNull(type);
return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(),
NoSuchFieldException.class);
}
MemberName resolveOrFail(byte refKind, Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
checkSymbolicClass(refc); // do this before attempting to resolve
name.getClass(); // NPE
type.getClass(); // NPE
Objects.requireNonNull(name);
Objects.requireNonNull(type);
checkMethodName(refKind, name); // NPE check on name
return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(),
NoSuchMethodException.class);
@ -1384,14 +1385,14 @@ return mh1;
MemberName resolveOrFail(byte refKind, MemberName member) throws ReflectiveOperationException {
checkSymbolicClass(member.getDeclaringClass()); // do this before attempting to resolve
member.getName().getClass(); // NPE
member.getType().getClass(); // NPE
Objects.requireNonNull(member.getName());
Objects.requireNonNull(member.getType());
return IMPL_NAMES.resolveOrFail(refKind, member, lookupClassOrNull(),
ReflectiveOperationException.class);
}
void checkSymbolicClass(Class<?> refc) throws IllegalAccessException {
refc.getClass(); // NPE
Objects.requireNonNull(refc);
Class<?> caller = lookupClassOrNull();
if (caller != null && !VerifyAccess.isClassAccessible(refc, caller, allowedModes))
throw new MemberName(refc).makeAccessException("symbolic reference class is not public", this);

View File

@ -25,6 +25,7 @@
package java.lang.invoke;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
/**
@ -275,7 +276,7 @@ public class MutableCallSite extends CallSite {
if (sites.length == 0) return;
STORE_BARRIER.lazySet(0);
for (MutableCallSite site : sites) {
site.getClass(); // trigger NPE on first null
Objects.requireNonNull(site); // trigger NPE on first null
}
// FIXME: NYI
}

View File

@ -26,6 +26,7 @@
package java.util.logging;
import java.util.Objects;
import java.io.UnsupportedEncodingException;
import java.security.AccessController;
import java.security.PrivilegedAction;
@ -165,9 +166,7 @@ public abstract class Handler {
*/
public synchronized void setFormatter(Formatter newFormatter) throws SecurityException {
checkPermission();
// Check for a null pointer:
newFormatter.getClass();
formatter = newFormatter;
formatter = Objects.requireNonNull(newFormatter);
}
/**

View File

@ -175,9 +175,7 @@ public class LogRecord implements java.io.Serializable {
* @param msg the raw non-localized logging message (may be null)
*/
public LogRecord(Level level, String msg) {
// Make sure level isn't null, by calling random method.
level.getClass();
this.level = level;
this.level = Objects.requireNonNull(level);
message = msg;
// Assign a thread ID and a unique sequence number.
sequenceNumber = globalSequenceNumber.getAndIncrement();

View File

@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
@ -1746,8 +1747,7 @@ public class Logger {
* does not have LoggingPermission("control").
*/
public void addHandler(Handler handler) throws SecurityException {
// Check for null handler
handler.getClass();
Objects.requireNonNull(handler);
checkPermission();
handlers.add(handler);
}

View File

@ -33,6 +33,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.Objects;
interface Intf {
static int i = 0;
@ -40,9 +41,8 @@ interface Intf {
public class Test8009222 {
public static void main(String[] args) throws Exception {
MethodHandles.lookup()
.findStaticGetter(Intf.class, "i", int.class)
.getClass(); // null check
Objects.requireNonNull(MethodHandles.lookup()
.findStaticGetter(Intf.class, "i", int.class));
System.out.println("TEST PASSED");
}

View File

@ -739,7 +739,7 @@ public abstract class XMLKit {
if (i >= size) {
badIndex(i);
}
e.getClass(); // null check
Objects.requireNonNull(e);
checkNotFrozen();
Object old = parts[i];
setRaw(i, e);
@ -861,7 +861,7 @@ public abstract class XMLKit {
public void add(int i, Object e) {
// (The shape of this method is tweaked for common cases.)
e.getClass(); // force a null check on e
Objects.requireNonNull(e);
if (hasNulls(1 + NEED_SLOP)) {
// Common case: Have some slop space.
if (i == size) {
@ -2943,7 +2943,7 @@ public abstract class XMLKit {
}
public static Filter elementFilter(final Collection nameSet) {
nameSet.getClass(); // null check
Objects.requireNonNull(nameSet);
return new ElementFilter() {
@Override
@ -3299,7 +3299,7 @@ public abstract class XMLKit {
}
public static Filter replaceInTree(Filter f) {
f.getClass(); // null check
Objects.requireNonNull(f);
return replaceInTree(f, null);
}