8258422: Cleanup unnecessary null comparison before instanceof check in java.base

Reviewed-by: chegar, aefimov
This commit is contained in:
Andrey Turbanov 2021-01-11 23:30:44 +00:00 committed by Aleksei Efimov
parent ff54b77b76
commit 022bc9f0cb
22 changed files with 69 additions and 81 deletions

View file

@ -2190,8 +2190,8 @@ public class File
* {@code false} otherwise * {@code false} otherwise
*/ */
public boolean equals(Object obj) { public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof File)) { if (obj instanceof File file) {
return compareTo((File)obj) == 0; return compareTo(file) == 0;
} }
return false; return false;
} }

View file

@ -308,8 +308,7 @@ public final class Constructor<T> extends Executable {
* same formal parameter types. * same formal parameter types.
*/ */
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj != null && obj instanceof Constructor) { if (obj instanceof Constructor<?> other) {
Constructor<?> other = (Constructor<?>)obj;
if (getDeclaringClass() == other.getDeclaringClass()) { if (getDeclaringClass() == other.getDeclaringClass()) {
return equalParamTypes(parameterTypes, other.parameterTypes); return equalParamTypes(parameterTypes, other.parameterTypes);
} }

View file

@ -279,8 +279,7 @@ class Field extends AccessibleObject implements Member {
* and type. * and type.
*/ */
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj != null && obj instanceof Field) { if (obj instanceof Field other) {
Field other = (Field)obj;
return (getDeclaringClass() == other.getDeclaringClass()) return (getDeclaringClass() == other.getDeclaringClass())
&& (getName() == other.getName()) && (getName() == other.getName())
&& (getType() == other.getType()); && (getType() == other.getType());

View file

@ -358,8 +358,7 @@ public final class Method extends Executable {
* and formal parameter types and return type. * and formal parameter types and return type.
*/ */
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj != null && obj instanceof Method) { if (obj instanceof Method other) {
Method other = (Method)obj;
if ((getDeclaringClass() == other.getDeclaringClass()) if ((getDeclaringClass() == other.getDeclaringClass())
&& (getName() == other.getName())) { && (getName() == other.getName())) {
if (!returnType.equals(other.getReturnType())) if (!returnType.equals(other.getReturnType()))

View file

@ -235,9 +235,9 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
throws IOException { throws IOException {
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) if (!(mcastaddr instanceof InetSocketAddress addr))
throw new IllegalArgumentException("Unsupported address type"); throw new IllegalArgumentException("Unsupported address type");
join(((InetSocketAddress)mcastaddr).getAddress(), netIf); join(addr.getAddress(), netIf);
} }
protected abstract void join(InetAddress inetaddr, NetworkInterface netIf) protected abstract void join(InetAddress inetaddr, NetworkInterface netIf)
@ -253,9 +253,9 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
*/ */
protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
throws IOException { throws IOException {
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) if (!(mcastaddr instanceof InetSocketAddress addr))
throw new IllegalArgumentException("Unsupported address type"); throw new IllegalArgumentException("Unsupported address type");
leave(((InetSocketAddress)mcastaddr).getAddress(), netIf); leave(addr.getAddress(), netIf);
} }
protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf) protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf)
@ -292,7 +292,7 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
* PlainSocketImpl.setOption(). * PlainSocketImpl.setOption().
*/ */
case SO_TIMEOUT: case SO_TIMEOUT:
if (o == null || !(o instanceof Integer)) { if (!(o instanceof Integer)) {
throw new SocketException("bad argument for SO_TIMEOUT"); throw new SocketException("bad argument for SO_TIMEOUT");
} }
int tmp = ((Integer) o).intValue(); int tmp = ((Integer) o).intValue();
@ -301,18 +301,18 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
timeout = tmp; timeout = tmp;
return; return;
case IP_TOS: case IP_TOS:
if (o == null || !(o instanceof Integer)) { if (!(o instanceof Integer)) {
throw new SocketException("bad argument for IP_TOS"); throw new SocketException("bad argument for IP_TOS");
} }
trafficClass = ((Integer)o).intValue(); trafficClass = ((Integer)o).intValue();
break; break;
case SO_REUSEADDR: case SO_REUSEADDR:
if (o == null || !(o instanceof Boolean)) { if (!(o instanceof Boolean)) {
throw new SocketException("bad argument for SO_REUSEADDR"); throw new SocketException("bad argument for SO_REUSEADDR");
} }
break; break;
case SO_BROADCAST: case SO_BROADCAST:
if (o == null || !(o instanceof Boolean)) { if (!(o instanceof Boolean)) {
throw new SocketException("bad argument for SO_BROADCAST"); throw new SocketException("bad argument for SO_BROADCAST");
} }
break; break;
@ -320,26 +320,26 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
throw new SocketException("Cannot re-bind Socket"); throw new SocketException("Cannot re-bind Socket");
case SO_RCVBUF: case SO_RCVBUF:
case SO_SNDBUF: case SO_SNDBUF:
if (o == null || !(o instanceof Integer) || if (!(o instanceof Integer) ||
((Integer)o).intValue() < 0) { ((Integer)o).intValue() < 0) {
throw new SocketException("bad argument for SO_SNDBUF or " + throw new SocketException("bad argument for SO_SNDBUF or " +
"SO_RCVBUF"); "SO_RCVBUF");
} }
break; break;
case IP_MULTICAST_IF: case IP_MULTICAST_IF:
if (o == null || !(o instanceof InetAddress)) if (!(o instanceof InetAddress))
throw new SocketException("bad argument for IP_MULTICAST_IF"); throw new SocketException("bad argument for IP_MULTICAST_IF");
break; break;
case IP_MULTICAST_IF2: case IP_MULTICAST_IF2:
if (o == null || !(o instanceof NetworkInterface)) if (!(o instanceof NetworkInterface))
throw new SocketException("bad argument for IP_MULTICAST_IF2"); throw new SocketException("bad argument for IP_MULTICAST_IF2");
break; break;
case IP_MULTICAST_LOOP: case IP_MULTICAST_LOOP:
if (o == null || !(o instanceof Boolean)) if (!(o instanceof Boolean))
throw new SocketException("bad argument for IP_MULTICAST_LOOP"); throw new SocketException("bad argument for IP_MULTICAST_LOOP");
break; break;
case SO_REUSEPORT: case SO_REUSEPORT:
if (o == null || !(o instanceof Boolean)) { if (!(o instanceof Boolean)) {
throw new SocketException("bad argument for SO_REUSEPORT"); throw new SocketException("bad argument for SO_REUSEPORT");
} }
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {

View file

@ -213,9 +213,8 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
throws IOException { throws IOException {
boolean connected = false; boolean connected = false;
try { try {
if (address == null || !(address instanceof InetSocketAddress)) if (!(address instanceof InetSocketAddress addr))
throw new IllegalArgumentException("unsupported address type"); throw new IllegalArgumentException("unsupported address type");
InetSocketAddress addr = (InetSocketAddress) address;
if (addr.isUnresolved()) if (addr.isUnresolved())
throw new UnknownHostException(addr.getHostName()); throw new UnknownHostException(addr.getHostName());
// recording this.address as supplied by caller before calling connect // recording this.address as supplied by caller before calling connect
@ -259,7 +258,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
* PlainSocketImpl.setOption(). * PlainSocketImpl.setOption().
*/ */
case SO_LINGER: case SO_LINGER:
if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean))) if (!(val instanceof Integer) && !(val instanceof Boolean))
throw new SocketException("Bad parameter for option"); throw new SocketException("Bad parameter for option");
if (val instanceof Boolean) { if (val instanceof Boolean) {
/* true only if disabling - enabling should be Integer */ /* true only if disabling - enabling should be Integer */
@ -267,7 +266,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
} }
break; break;
case SO_TIMEOUT: case SO_TIMEOUT:
if (val == null || (!(val instanceof Integer))) if (!(val instanceof Integer))
throw new SocketException("Bad parameter for SO_TIMEOUT"); throw new SocketException("Bad parameter for SO_TIMEOUT");
int tmp = ((Integer) val).intValue(); int tmp = ((Integer) val).intValue();
if (tmp < 0) if (tmp < 0)
@ -275,7 +274,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
timeout = tmp; timeout = tmp;
break; break;
case IP_TOS: case IP_TOS:
if (val == null || !(val instanceof Integer)) { if (!(val instanceof Integer)) {
throw new SocketException("bad argument for IP_TOS"); throw new SocketException("bad argument for IP_TOS");
} }
trafficClass = ((Integer)val).intValue(); trafficClass = ((Integer)val).intValue();
@ -283,35 +282,35 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
case SO_BINDADDR: case SO_BINDADDR:
throw new SocketException("Cannot re-bind socket"); throw new SocketException("Cannot re-bind socket");
case TCP_NODELAY: case TCP_NODELAY:
if (val == null || !(val instanceof Boolean)) if (!(val instanceof Boolean))
throw new SocketException("bad parameter for TCP_NODELAY"); throw new SocketException("bad parameter for TCP_NODELAY");
on = ((Boolean)val).booleanValue(); on = ((Boolean)val).booleanValue();
break; break;
case SO_SNDBUF: case SO_SNDBUF:
case SO_RCVBUF: case SO_RCVBUF:
if (val == null || !(val instanceof Integer) || if (!(val instanceof Integer) ||
!(((Integer)val).intValue() > 0)) { !(((Integer)val).intValue() > 0)) {
throw new SocketException("bad parameter for SO_SNDBUF " + throw new SocketException("bad parameter for SO_SNDBUF " +
"or SO_RCVBUF"); "or SO_RCVBUF");
} }
break; break;
case SO_KEEPALIVE: case SO_KEEPALIVE:
if (val == null || !(val instanceof Boolean)) if (!(val instanceof Boolean))
throw new SocketException("bad parameter for SO_KEEPALIVE"); throw new SocketException("bad parameter for SO_KEEPALIVE");
on = ((Boolean)val).booleanValue(); on = ((Boolean)val).booleanValue();
break; break;
case SO_OOBINLINE: case SO_OOBINLINE:
if (val == null || !(val instanceof Boolean)) if (!(val instanceof Boolean))
throw new SocketException("bad parameter for SO_OOBINLINE"); throw new SocketException("bad parameter for SO_OOBINLINE");
on = ((Boolean)val).booleanValue(); on = ((Boolean)val).booleanValue();
break; break;
case SO_REUSEADDR: case SO_REUSEADDR:
if (val == null || !(val instanceof Boolean)) if (!(val instanceof Boolean))
throw new SocketException("bad parameter for SO_REUSEADDR"); throw new SocketException("bad parameter for SO_REUSEADDR");
on = ((Boolean)val).booleanValue(); on = ((Boolean)val).booleanValue();
break; break;
case SO_REUSEPORT: case SO_REUSEPORT:
if (val == null || !(val instanceof Boolean)) if (!(val instanceof Boolean))
throw new SocketException("bad parameter for SO_REUSEPORT"); throw new SocketException("bad parameter for SO_REUSEPORT");
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT))
throw new UnsupportedOperationException("unsupported option"); throw new UnsupportedOperationException("unsupported option");

View file

@ -352,9 +352,8 @@ class DatagramPacket {
* @since 1.4 * @since 1.4
*/ */
public synchronized void setSocketAddress(SocketAddress address) { public synchronized void setSocketAddress(SocketAddress address) {
if (address == null || !(address instanceof InetSocketAddress)) if (!(address instanceof InetSocketAddress addr))
throw new IllegalArgumentException("unsupported address type"); throw new IllegalArgumentException("unsupported address type");
InetSocketAddress addr = (InetSocketAddress) address;
if (addr.isUnresolved()) if (addr.isUnresolved())
throw new IllegalArgumentException("unresolved address"); throw new IllegalArgumentException("unresolved address");
setAddress(addr.getAddress()); setAddress(addr.getAddress());

View file

@ -102,9 +102,8 @@ import java.util.Set;
protected void connect(SocketAddress endpoint, int timeout) protected void connect(SocketAddress endpoint, int timeout)
throws IOException throws IOException
{ {
if (endpoint == null || !(endpoint instanceof InetSocketAddress)) if (!(endpoint instanceof InetSocketAddress epoint))
throw new IllegalArgumentException("Unsupported address type"); throw new IllegalArgumentException("Unsupported address type");
final InetSocketAddress epoint = (InetSocketAddress)endpoint;
String destHost = epoint.isUnresolved() ? epoint.getHostName() String destHost = epoint.isUnresolved() ? epoint.getHostName()
: epoint.getAddress().getHostAddress(); : epoint.getAddress().getHostAddress();
final int destPort = epoint.getPort(); final int destPort = epoint.getPort();

View file

@ -352,8 +352,8 @@ class Inet4Address extends InetAddress {
* @see java.net.InetAddress#getAddress() * @see java.net.InetAddress#getAddress()
*/ */
public boolean equals(Object obj) { public boolean equals(Object obj) {
return (obj != null) && (obj instanceof Inet4Address) && return (obj instanceof Inet4Address inet4Address) &&
(((InetAddress)obj).holder().getAddress() == holder().getAddress()); inet4Address.holder().getAddress() == holder().getAddress();
} }
// Utilities // Utilities

View file

@ -878,13 +878,11 @@ class Inet6Address extends InetAddress {
*/ */
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Inet6Address)) if (obj instanceof Inet6Address inetAddr) {
return false;
Inet6Address inetAddr = (Inet6Address)obj;
return holder6.equals(inetAddr.holder6); return holder6.equals(inetAddr.holder6);
} }
return false;
}
/** /**
* Utility routine to check if the InetAddress is an * Utility routine to check if the InetAddress is an

View file

@ -119,9 +119,8 @@ public class InetSocketAddress
@Override @Override
public final boolean equals(Object obj) { public final boolean equals(Object obj) {
if (obj == null || !(obj instanceof InetSocketAddressHolder)) if (!(obj instanceof InetSocketAddressHolder that))
return false; return false;
InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
boolean sameIP; boolean sameIP;
if (addr != null) if (addr != null)
sameIP = addr.equals(that.addr); sameIP = addr.equals(that.addr);
@ -409,9 +408,10 @@ public class InetSocketAddress
*/ */
@Override @Override
public final boolean equals(Object obj) { public final boolean equals(Object obj) {
if (obj == null || !(obj instanceof InetSocketAddress)) if (obj instanceof InetSocketAddress addr) {
return holder.equals(addr.holder);
}
return false; return false;
return holder.equals(((InetSocketAddress) obj).holder);
} }
/** /**

View file

@ -801,19 +801,19 @@ final class NetMulticastSocket extends MulticastSocket {
if (isClosed()) if (isClosed())
throw new SocketException("Socket is closed"); throw new SocketException("Socket is closed");
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) if (!(mcastaddr instanceof InetSocketAddress addr))
throw new IllegalArgumentException("Unsupported address type"); throw new IllegalArgumentException("Unsupported address type");
if (oldImpl) if (oldImpl)
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "joinGroup"); checkAddress(addr.getAddress(), "joinGroup");
SecurityManager security = System.getSecurityManager(); SecurityManager security = System.getSecurityManager();
if (security != null) { if (security != null) {
security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress()); security.checkMulticast(addr.getAddress());
} }
if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) { if (!addr.getAddress().isMulticastAddress()) {
throw new SocketException("Not a multicast address"); throw new SocketException("Not a multicast address");
} }
@ -826,19 +826,19 @@ final class NetMulticastSocket extends MulticastSocket {
if (isClosed()) if (isClosed())
throw new SocketException("Socket is closed"); throw new SocketException("Socket is closed");
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) if (!(mcastaddr instanceof InetSocketAddress addr))
throw new IllegalArgumentException("Unsupported address type"); throw new IllegalArgumentException("Unsupported address type");
if (oldImpl) if (oldImpl)
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "leaveGroup"); checkAddress(addr.getAddress(), "leaveGroup");
SecurityManager security = System.getSecurityManager(); SecurityManager security = System.getSecurityManager();
if (security != null) { if (security != null) {
security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress()); security.checkMulticast(addr.getAddress());
} }
if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) { if (!addr.getAddress().isMulticastAddress()) {
throw new SocketException("Not a multicast address"); throw new SocketException("Not a multicast address");
} }

View file

@ -146,9 +146,8 @@ public class Proxy {
* @see java.net.InetSocketAddress#equals(java.lang.Object) * @see java.net.InetSocketAddress#equals(java.lang.Object)
*/ */
public final boolean equals(Object obj) { public final boolean equals(Object obj) {
if (obj == null || !(obj instanceof Proxy)) if (!(obj instanceof Proxy p))
return false; return false;
Proxy p = (Proxy) obj;
if (p.type() == type()) { if (p.type() == type()) {
if (address() == null) { if (address() == null) {
return (p.address() == null); return (p.address() == null);

View file

@ -272,9 +272,8 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
} }
SecurityManager security = System.getSecurityManager(); SecurityManager security = System.getSecurityManager();
if (endpoint == null || !(endpoint instanceof InetSocketAddress)) if (!(endpoint instanceof InetSocketAddress epoint))
throw new IllegalArgumentException("Unsupported address type"); throw new IllegalArgumentException("Unsupported address type");
InetSocketAddress epoint = (InetSocketAddress) endpoint;
if (security != null) { if (security != null) {
if (epoint.isUnresolved()) if (epoint.isUnresolved())
security.checkConnect(epoint.getHostName(), security.checkConnect(epoint.getHostName(),

View file

@ -342,9 +342,8 @@ public final class AclEntry {
public boolean equals(Object ob) { public boolean equals(Object ob) {
if (ob == this) if (ob == this)
return true; return true;
if (ob == null || !(ob instanceof AclEntry)) if (!(ob instanceof AclEntry other))
return false; return false;
AclEntry other = (AclEntry)ob;
if (this.type != other.type) if (this.type != other.type)
return false; return false;
if (!this.who.equals(other.who)) if (!this.who.equals(other.who))

View file

@ -95,18 +95,17 @@ public final class Signal {
/** /**
* Compares the equality of two <code>Signal</code> objects. * Compares the equality of two <code>Signal</code> objects.
* *
* @param other the object to compare with. * @param obj the object to compare with.
* @return whether two <code>Signal</code> objects are equal. * @return whether two <code>Signal</code> objects are equal.
*/ */
public boolean equals(Object other) { public boolean equals(Object obj) {
if (this == other) { if (this == obj) {
return true; return true;
} }
if (other == null || !(other instanceof Signal)) { if (obj instanceof Signal other) {
return false; return name.equals(other.name) && (number == other.number);
} }
Signal other1 = (Signal)other; return false;
return name.equals(other1.name) && (number == other1.number);
} }
/** /**

View file

@ -66,8 +66,8 @@ public abstract class Negotiator {
} catch (ReflectiveOperationException roe) { } catch (ReflectiveOperationException roe) {
finest(roe); finest(roe);
Throwable t = roe.getCause(); Throwable t = roe.getCause();
if (t != null && t instanceof Exception) if (t instanceof Exception exception)
finest((Exception)t); finest(exception);
return null; return null;
} }
} }

View file

@ -426,7 +426,7 @@ final class HttpsClient extends HttpClient
// unconnected sockets have not been implemented. // unconnected sockets have not been implemented.
// //
Throwable t = se.getCause(); Throwable t = se.getCause();
if (t != null && t instanceof UnsupportedOperationException) { if (t instanceof UnsupportedOperationException) {
return super.createSocket(); return super.createSocket();
} else { } else {
throw se; throw se;

View file

@ -504,9 +504,9 @@ public class DatagramSocketAdaptor
* @throws SocketException if group is not a multicast address * @throws SocketException if group is not a multicast address
*/ */
private static InetAddress checkGroup(SocketAddress mcastaddr) throws SocketException { private static InetAddress checkGroup(SocketAddress mcastaddr) throws SocketException {
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) if (!(mcastaddr instanceof InetSocketAddress addr))
throw new IllegalArgumentException("Unsupported address type"); throw new IllegalArgumentException("Unsupported address type");
InetAddress group = ((InetSocketAddress) mcastaddr).getAddress(); InetAddress group = addr.getAddress();
if (group == null) if (group == null)
throw new IllegalArgumentException("Unresolved address"); throw new IllegalArgumentException("Unresolved address");
if (!group.isMulticastAddress()) if (!group.isMulticastAddress())

View file

@ -149,8 +149,8 @@ class PollingWatchService
}); });
} catch (PrivilegedActionException pae) { } catch (PrivilegedActionException pae) {
Throwable cause = pae.getCause(); Throwable cause = pae.getCause();
if (cause != null && cause instanceof IOException) if (cause instanceof IOException ioe)
throw (IOException)cause; throw ioe;
throw new AssertionError(pae); throw new AssertionError(pae);
} }
} }

View file

@ -762,8 +762,8 @@ class UnixPath implements Path {
@Override @Override
public boolean equals(Object ob) { public boolean equals(Object ob) {
if ((ob != null) && (ob instanceof UnixPath)) { if (ob instanceof UnixPath path) {
return compareTo((Path)ob) == 0; return compareTo(path) == 0;
} }
return false; return false;
} }

View file

@ -798,8 +798,8 @@ class WindowsPath implements Path {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof WindowsPath)) { if (obj instanceof WindowsPath path) {
return compareTo((Path)obj) == 0; return compareTo(path) == 0;
} }
return false; return false;
} }