mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8258422: Cleanup unnecessary null comparison before instanceof check in java.base
Reviewed-by: chegar, aefimov
This commit is contained in:
parent
ff54b77b76
commit
022bc9f0cb
22 changed files with 69 additions and 81 deletions
|
@ -235,9 +235,9 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
|
|||
|
||||
protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
|
||||
throws IOException {
|
||||
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
|
||||
if (!(mcastaddr instanceof InetSocketAddress addr))
|
||||
throw new IllegalArgumentException("Unsupported address type");
|
||||
join(((InetSocketAddress)mcastaddr).getAddress(), netIf);
|
||||
join(addr.getAddress(), 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)
|
||||
throws IOException {
|
||||
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
|
||||
if (!(mcastaddr instanceof InetSocketAddress addr))
|
||||
throw new IllegalArgumentException("Unsupported address type");
|
||||
leave(((InetSocketAddress)mcastaddr).getAddress(), netIf);
|
||||
leave(addr.getAddress(), netIf);
|
||||
}
|
||||
|
||||
protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf)
|
||||
|
@ -292,7 +292,7 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
|
|||
* PlainSocketImpl.setOption().
|
||||
*/
|
||||
case SO_TIMEOUT:
|
||||
if (o == null || !(o instanceof Integer)) {
|
||||
if (!(o instanceof Integer)) {
|
||||
throw new SocketException("bad argument for SO_TIMEOUT");
|
||||
}
|
||||
int tmp = ((Integer) o).intValue();
|
||||
|
@ -301,18 +301,18 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
|
|||
timeout = tmp;
|
||||
return;
|
||||
case IP_TOS:
|
||||
if (o == null || !(o instanceof Integer)) {
|
||||
if (!(o instanceof Integer)) {
|
||||
throw new SocketException("bad argument for IP_TOS");
|
||||
}
|
||||
trafficClass = ((Integer)o).intValue();
|
||||
break;
|
||||
case SO_REUSEADDR:
|
||||
if (o == null || !(o instanceof Boolean)) {
|
||||
if (!(o instanceof Boolean)) {
|
||||
throw new SocketException("bad argument for SO_REUSEADDR");
|
||||
}
|
||||
break;
|
||||
case SO_BROADCAST:
|
||||
if (o == null || !(o instanceof Boolean)) {
|
||||
if (!(o instanceof Boolean)) {
|
||||
throw new SocketException("bad argument for SO_BROADCAST");
|
||||
}
|
||||
break;
|
||||
|
@ -320,26 +320,26 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
|
|||
throw new SocketException("Cannot re-bind Socket");
|
||||
case SO_RCVBUF:
|
||||
case SO_SNDBUF:
|
||||
if (o == null || !(o instanceof Integer) ||
|
||||
if (!(o instanceof Integer) ||
|
||||
((Integer)o).intValue() < 0) {
|
||||
throw new SocketException("bad argument for SO_SNDBUF or " +
|
||||
"SO_RCVBUF");
|
||||
}
|
||||
break;
|
||||
case IP_MULTICAST_IF:
|
||||
if (o == null || !(o instanceof InetAddress))
|
||||
if (!(o instanceof InetAddress))
|
||||
throw new SocketException("bad argument for IP_MULTICAST_IF");
|
||||
break;
|
||||
case IP_MULTICAST_IF2:
|
||||
if (o == null || !(o instanceof NetworkInterface))
|
||||
if (!(o instanceof NetworkInterface))
|
||||
throw new SocketException("bad argument for IP_MULTICAST_IF2");
|
||||
break;
|
||||
case IP_MULTICAST_LOOP:
|
||||
if (o == null || !(o instanceof Boolean))
|
||||
if (!(o instanceof Boolean))
|
||||
throw new SocketException("bad argument for IP_MULTICAST_LOOP");
|
||||
break;
|
||||
case SO_REUSEPORT:
|
||||
if (o == null || !(o instanceof Boolean)) {
|
||||
if (!(o instanceof Boolean)) {
|
||||
throw new SocketException("bad argument for SO_REUSEPORT");
|
||||
}
|
||||
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
|
||||
|
|
|
@ -213,9 +213,8 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
|
|||
throws IOException {
|
||||
boolean connected = false;
|
||||
try {
|
||||
if (address == null || !(address instanceof InetSocketAddress))
|
||||
if (!(address instanceof InetSocketAddress addr))
|
||||
throw new IllegalArgumentException("unsupported address type");
|
||||
InetSocketAddress addr = (InetSocketAddress) address;
|
||||
if (addr.isUnresolved())
|
||||
throw new UnknownHostException(addr.getHostName());
|
||||
// recording this.address as supplied by caller before calling connect
|
||||
|
@ -259,7 +258,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
|
|||
* PlainSocketImpl.setOption().
|
||||
*/
|
||||
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");
|
||||
if (val instanceof Boolean) {
|
||||
/* true only if disabling - enabling should be Integer */
|
||||
|
@ -267,7 +266,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
|
|||
}
|
||||
break;
|
||||
case SO_TIMEOUT:
|
||||
if (val == null || (!(val instanceof Integer)))
|
||||
if (!(val instanceof Integer))
|
||||
throw new SocketException("Bad parameter for SO_TIMEOUT");
|
||||
int tmp = ((Integer) val).intValue();
|
||||
if (tmp < 0)
|
||||
|
@ -275,7 +274,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
|
|||
timeout = tmp;
|
||||
break;
|
||||
case IP_TOS:
|
||||
if (val == null || !(val instanceof Integer)) {
|
||||
if (!(val instanceof Integer)) {
|
||||
throw new SocketException("bad argument for IP_TOS");
|
||||
}
|
||||
trafficClass = ((Integer)val).intValue();
|
||||
|
@ -283,35 +282,35 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
|
|||
case SO_BINDADDR:
|
||||
throw new SocketException("Cannot re-bind socket");
|
||||
case TCP_NODELAY:
|
||||
if (val == null || !(val instanceof Boolean))
|
||||
if (!(val instanceof Boolean))
|
||||
throw new SocketException("bad parameter for TCP_NODELAY");
|
||||
on = ((Boolean)val).booleanValue();
|
||||
break;
|
||||
case SO_SNDBUF:
|
||||
case SO_RCVBUF:
|
||||
if (val == null || !(val instanceof Integer) ||
|
||||
if (!(val instanceof Integer) ||
|
||||
!(((Integer)val).intValue() > 0)) {
|
||||
throw new SocketException("bad parameter for SO_SNDBUF " +
|
||||
"or SO_RCVBUF");
|
||||
}
|
||||
break;
|
||||
case SO_KEEPALIVE:
|
||||
if (val == null || !(val instanceof Boolean))
|
||||
if (!(val instanceof Boolean))
|
||||
throw new SocketException("bad parameter for SO_KEEPALIVE");
|
||||
on = ((Boolean)val).booleanValue();
|
||||
break;
|
||||
case SO_OOBINLINE:
|
||||
if (val == null || !(val instanceof Boolean))
|
||||
if (!(val instanceof Boolean))
|
||||
throw new SocketException("bad parameter for SO_OOBINLINE");
|
||||
on = ((Boolean)val).booleanValue();
|
||||
break;
|
||||
case SO_REUSEADDR:
|
||||
if (val == null || !(val instanceof Boolean))
|
||||
if (!(val instanceof Boolean))
|
||||
throw new SocketException("bad parameter for SO_REUSEADDR");
|
||||
on = ((Boolean)val).booleanValue();
|
||||
break;
|
||||
case SO_REUSEPORT:
|
||||
if (val == null || !(val instanceof Boolean))
|
||||
if (!(val instanceof Boolean))
|
||||
throw new SocketException("bad parameter for SO_REUSEPORT");
|
||||
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT))
|
||||
throw new UnsupportedOperationException("unsupported option");
|
||||
|
|
|
@ -352,9 +352,8 @@ class DatagramPacket {
|
|||
* @since 1.4
|
||||
*/
|
||||
public synchronized void setSocketAddress(SocketAddress address) {
|
||||
if (address == null || !(address instanceof InetSocketAddress))
|
||||
if (!(address instanceof InetSocketAddress addr))
|
||||
throw new IllegalArgumentException("unsupported address type");
|
||||
InetSocketAddress addr = (InetSocketAddress) address;
|
||||
if (addr.isUnresolved())
|
||||
throw new IllegalArgumentException("unresolved address");
|
||||
setAddress(addr.getAddress());
|
||||
|
|
|
@ -102,9 +102,8 @@ import java.util.Set;
|
|||
protected void connect(SocketAddress endpoint, int timeout)
|
||||
throws IOException
|
||||
{
|
||||
if (endpoint == null || !(endpoint instanceof InetSocketAddress))
|
||||
if (!(endpoint instanceof InetSocketAddress epoint))
|
||||
throw new IllegalArgumentException("Unsupported address type");
|
||||
final InetSocketAddress epoint = (InetSocketAddress)endpoint;
|
||||
String destHost = epoint.isUnresolved() ? epoint.getHostName()
|
||||
: epoint.getAddress().getHostAddress();
|
||||
final int destPort = epoint.getPort();
|
||||
|
|
|
@ -352,8 +352,8 @@ class Inet4Address extends InetAddress {
|
|||
* @see java.net.InetAddress#getAddress()
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
return (obj != null) && (obj instanceof Inet4Address) &&
|
||||
(((InetAddress)obj).holder().getAddress() == holder().getAddress());
|
||||
return (obj instanceof Inet4Address inet4Address) &&
|
||||
inet4Address.holder().getAddress() == holder().getAddress();
|
||||
}
|
||||
|
||||
// Utilities
|
||||
|
|
|
@ -878,12 +878,10 @@ class Inet6Address extends InetAddress {
|
|||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null || !(obj instanceof Inet6Address))
|
||||
return false;
|
||||
|
||||
Inet6Address inetAddr = (Inet6Address)obj;
|
||||
|
||||
return holder6.equals(inetAddr.holder6);
|
||||
if (obj instanceof Inet6Address inetAddr) {
|
||||
return holder6.equals(inetAddr.holder6);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -119,9 +119,8 @@ public class InetSocketAddress
|
|||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
if (obj == null || !(obj instanceof InetSocketAddressHolder))
|
||||
if (!(obj instanceof InetSocketAddressHolder that))
|
||||
return false;
|
||||
InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
|
||||
boolean sameIP;
|
||||
if (addr != null)
|
||||
sameIP = addr.equals(that.addr);
|
||||
|
@ -409,9 +408,10 @@ public class InetSocketAddress
|
|||
*/
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
if (obj == null || !(obj instanceof InetSocketAddress))
|
||||
return false;
|
||||
return holder.equals(((InetSocketAddress) obj).holder);
|
||||
if (obj instanceof InetSocketAddress addr) {
|
||||
return holder.equals(addr.holder);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -801,19 +801,19 @@ final class NetMulticastSocket extends MulticastSocket {
|
|||
if (isClosed())
|
||||
throw new SocketException("Socket is closed");
|
||||
|
||||
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
|
||||
if (!(mcastaddr instanceof InetSocketAddress addr))
|
||||
throw new IllegalArgumentException("Unsupported address type");
|
||||
|
||||
if (oldImpl)
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "joinGroup");
|
||||
checkAddress(addr.getAddress(), "joinGroup");
|
||||
SecurityManager security = System.getSecurityManager();
|
||||
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");
|
||||
}
|
||||
|
||||
|
@ -826,19 +826,19 @@ final class NetMulticastSocket extends MulticastSocket {
|
|||
if (isClosed())
|
||||
throw new SocketException("Socket is closed");
|
||||
|
||||
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
|
||||
if (!(mcastaddr instanceof InetSocketAddress addr))
|
||||
throw new IllegalArgumentException("Unsupported address type");
|
||||
|
||||
if (oldImpl)
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "leaveGroup");
|
||||
checkAddress(addr.getAddress(), "leaveGroup");
|
||||
SecurityManager security = System.getSecurityManager();
|
||||
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");
|
||||
}
|
||||
|
||||
|
|
|
@ -146,9 +146,8 @@ public class Proxy {
|
|||
* @see java.net.InetSocketAddress#equals(java.lang.Object)
|
||||
*/
|
||||
public final boolean equals(Object obj) {
|
||||
if (obj == null || !(obj instanceof Proxy))
|
||||
if (!(obj instanceof Proxy p))
|
||||
return false;
|
||||
Proxy p = (Proxy) obj;
|
||||
if (p.type() == type()) {
|
||||
if (address() == null) {
|
||||
return (p.address() == null);
|
||||
|
|
|
@ -272,9 +272,8 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
|
|||
}
|
||||
|
||||
SecurityManager security = System.getSecurityManager();
|
||||
if (endpoint == null || !(endpoint instanceof InetSocketAddress))
|
||||
if (!(endpoint instanceof InetSocketAddress epoint))
|
||||
throw new IllegalArgumentException("Unsupported address type");
|
||||
InetSocketAddress epoint = (InetSocketAddress) endpoint;
|
||||
if (security != null) {
|
||||
if (epoint.isUnresolved())
|
||||
security.checkConnect(epoint.getHostName(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue