8073479: Replace obj.getClass hacks with Objects.requireNonNull
Reviewed-by: dfuchs, plevart, vlivanov
--- a/jdk/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java Fri Feb 20 18:32:10 2015 +0300
@@ -35,6 +35,7 @@
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 @@
// 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 @@
@ForceInline
/*non-public*/ static Object nullCheck(Object obj) {
- obj.getClass();
- return obj;
+ return Objects.requireNonNull(obj);
}
@ForceInline
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MemberName.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MemberName.java Fri Feb 20 18:32:10 2015 +0300
@@ -521,7 +521,7 @@
}
@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 @@
/** 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 @@
}
@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);
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandle.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandle.java Fri Feb 20 18:32:10 2015 +0300
@@ -453,10 +453,8 @@
*/
// @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 @@
* @see #asFixedArity
*/
public MethodHandle asVarargsCollector(Class<?> arrayType) {
- arrayType.getClass(); // explicit NPE
+ Objects.requireNonNull(arrayType);
boolean lastMatch = asCollectorChecks(arrayType, 0);
if (isVarargsCollector() && lastMatch)
return this;
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java Fri Feb 20 18:32:10 2015 +0300
@@ -29,6 +29,7 @@
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 @@
* @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 @@
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 @@
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);
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MutableCallSite.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MutableCallSite.java Fri Feb 20 18:32:10 2015 +0300
@@ -25,6 +25,7 @@
package java.lang.invoke;
+import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
/**
@@ -275,7 +276,7 @@
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
}
--- a/jdk/src/java.logging/share/classes/java/util/logging/Handler.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.logging/share/classes/java/util/logging/Handler.java Fri Feb 20 18:32:10 2015 +0300
@@ -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 synchronized void setFormatter(Formatter newFormatter) throws SecurityException {
checkPermission();
- // Check for a null pointer:
- newFormatter.getClass();
- formatter = newFormatter;
+ formatter = Objects.requireNonNull(newFormatter);
}
/**
--- a/jdk/src/java.logging/share/classes/java/util/logging/LogRecord.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.logging/share/classes/java/util/logging/LogRecord.java Fri Feb 20 18:32:10 2015 +0300
@@ -175,9 +175,7 @@
* @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();
--- a/jdk/src/java.logging/share/classes/java/util/logging/Logger.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/src/java.logging/share/classes/java/util/logging/Logger.java Fri Feb 20 18:32:10 2015 +0300
@@ -33,6 +33,7 @@
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 @@
* 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);
}
--- a/jdk/test/java/lang/invoke/8009222/Test8009222.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/test/java/lang/invoke/8009222/Test8009222.java Fri Feb 20 18:32:10 2015 +0300
@@ -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 @@
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");
}
--- a/jdk/test/tools/pack200/pack200-verifier/src/xmlkit/XMLKit.java Wed Jul 05 20:21:13 2017 +0200
+++ b/jdk/test/tools/pack200/pack200-verifier/src/xmlkit/XMLKit.java Fri Feb 20 18:32:10 2015 +0300
@@ -739,7 +739,7 @@
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 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 static Filter elementFilter(final Collection nameSet) {
- nameSet.getClass(); // null check
+ Objects.requireNonNull(nameSet);
return new ElementFilter() {
@Override
@@ -3299,7 +3299,7 @@
}
public static Filter replaceInTree(Filter f) {
- f.getClass(); // null check
+ Objects.requireNonNull(f);
return replaceInTree(f, null);
}