Skip to content
Permalink
Browse files
8258422: Cleanup unnecessary null comparison before instanceof check …
…in java.base

Reviewed-by: chegar, aefimov
  • Loading branch information
turbanoff authored and AlekseiEfimov committed Jan 11, 2021
1 parent ff54b77 commit 022bc9f0cb3c8cda3d14aeadd5f8dc08cad2dc54
Showing with 69 additions and 81 deletions.
  1. +2 −2 src/java.base/share/classes/java/io/File.java
  2. +1 −2 src/java.base/share/classes/java/lang/reflect/Constructor.java
  3. +1 −2 src/java.base/share/classes/java/lang/reflect/Field.java
  4. +1 −2 src/java.base/share/classes/java/lang/reflect/Method.java
  5. +13 −13 src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java
  6. +10 −11 src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
  7. +1 −2 src/java.base/share/classes/java/net/DatagramPacket.java
  8. +1 −2 src/java.base/share/classes/java/net/HttpConnectSocketImpl.java
  9. +2 −2 src/java.base/share/classes/java/net/Inet4Address.java
  10. +4 −6 src/java.base/share/classes/java/net/Inet6Address.java
  11. +5 −5 src/java.base/share/classes/java/net/InetSocketAddress.java
  12. +8 −8 src/java.base/share/classes/java/net/NetMulticastSocket.java
  13. +1 −2 src/java.base/share/classes/java/net/Proxy.java
  14. +1 −2 src/java.base/share/classes/java/net/SocksSocketImpl.java
  15. +1 −2 src/java.base/share/classes/java/nio/file/attribute/AclEntry.java
  16. +6 −7 src/java.base/share/classes/jdk/internal/misc/Signal.java
  17. +2 −2 src/java.base/share/classes/sun/net/www/protocol/http/Negotiator.java
  18. +1 −1 src/java.base/share/classes/sun/net/www/protocol/https/HttpsClient.java
  19. +2 −2 src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java
  20. +2 −2 src/java.base/share/classes/sun/nio/fs/PollingWatchService.java
  21. +2 −2 src/java.base/unix/classes/sun/nio/fs/UnixPath.java
  22. +2 −2 src/java.base/windows/classes/sun/nio/fs/WindowsPath.java
@@ -2190,8 +2190,8 @@ public int compareTo(File pathname) {
* {@code false} otherwise
*/
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof File)) {
return compareTo((File)obj) == 0;
if (obj instanceof File file) {
return compareTo(file) == 0;
}
return false;
}
@@ -308,8 +308,7 @@ public int getModifiers() {
* same formal parameter types.
*/
public boolean equals(Object obj) {
if (obj != null && obj instanceof Constructor) {
Constructor<?> other = (Constructor<?>)obj;
if (obj instanceof Constructor<?> other) {
if (getDeclaringClass() == other.getDeclaringClass()) {
return equalParamTypes(parameterTypes, other.parameterTypes);
}
@@ -279,8 +279,7 @@ public Type getGenericType() {
* and type.
*/
public boolean equals(Object obj) {
if (obj != null && obj instanceof Field) {
Field other = (Field)obj;
if (obj instanceof Field other) {
return (getDeclaringClass() == other.getDeclaringClass())
&& (getName() == other.getName())
&& (getType() == other.getType());
@@ -358,8 +358,7 @@ public Type getGenericReturnType() {
* and formal parameter types and return type.
*/
public boolean equals(Object obj) {
if (obj != null && obj instanceof Method) {
Method other = (Method)obj;
if (obj instanceof Method other) {
if ((getDeclaringClass() == other.getDeclaringClass())
&& (getName() == other.getName())) {
if (!returnType.equals(other.getReturnType()))
@@ -235,9 +235,9 @@ protected void leave(InetAddress inetaddr) throws IOException {

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 @@ protected abstract void join(InetAddress inetaddr, NetworkInterface netIf)
*/
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 @@ public void setOption(int optID, Object o) throws SocketException {
* 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,45 +301,45 @@ public void setOption(int optID, Object o) throws SocketException {
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;
case SO_BINDADDR:
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 @@ protected void connect(SocketAddress address, int timeout)
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,59 +258,59 @@ public void setOption(int opt, Object val) throws SocketException {
* 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 */
on = false;
}
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)
throw new IllegalArgumentException("timeout < 0");
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();
break;
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 @@ public synchronized void setPort(int iport) {
* @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 @@ protected void connect(InetAddress address, int port) throws IOException {
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 @@ public int hashCode() {
* @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 @@ public int hashCode() {
*/
@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 String toString() {

@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 String toString() {
*/
@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 @@ public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
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 @@ public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
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 String toString() {
* @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 @@ protected void connect(SocketAddress endpoint, int timeout) throws IOException {
}

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(),
@@ -342,9 +342,8 @@ public UserPrincipal principal() {
public boolean equals(Object ob) {
if (ob == this)
return true;
if (ob == null || !(ob instanceof AclEntry))
if (!(ob instanceof AclEntry other))
return false;
AclEntry other = (AclEntry)ob;
if (this.type != other.type)
return false;
if (!this.who.equals(other.who))
@@ -95,18 +95,17 @@ public String getName() {
/**
* 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.
*/
public boolean equals(Object other) {
if (this == other) {
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (other == null || !(other instanceof Signal)) {
return false;
if (obj instanceof Signal other) {
return name.equals(other.name) && (number == other.number);
}
Signal other1 = (Signal)other;
return name.equals(other1.name) && (number == other1.number);
return false;
}

/**
@@ -66,8 +66,8 @@ static Negotiator getNegotiator(HttpCallerInfo hci) {
} catch (ReflectiveOperationException roe) {
finest(roe);
Throwable t = roe.getCause();
if (t != null && t instanceof Exception)
finest((Exception)t);
if (t instanceof Exception exception)
finest(exception);
return null;
}
}

1 comment on commit 022bc9f

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 022bc9f Jan 11, 2021

Review

Issues

Please sign in to comment.