Skip to content
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ public Type[] getGenericExceptionTypes() {
* 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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/lang/reflect/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/lang/reflect/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ public Type[] getGenericExceptionTypes() {
* 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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ 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))
throw new IllegalArgumentException("Unsupported address type");
join(((InetSocketAddress)mcastaddr).getAddress(), netIf);
}
Expand All @@ -253,7 +253,7 @@ 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))
throw new IllegalArgumentException("Unsupported address type");
leave(((InetSocketAddress)mcastaddr).getAddress(), netIf);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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)) {
Expand Down
20 changes: 10 additions & 10 deletions src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected void connect(SocketAddress address, int timeout)
throws IOException {
boolean connected = false;
try {
if (address == null || !(address instanceof InetSocketAddress))
if (!(address instanceof InetSocketAddress))
throw new IllegalArgumentException("unsupported address type");
InetSocketAddress addr = (InetSocketAddress) address;
if (addr.isUnresolved())
Expand Down Expand Up @@ -259,59 +259,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");
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/DatagramPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ 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))
throw new IllegalArgumentException("unsupported address type");
InetSocketAddress addr = (InetSocketAddress) address;
if (addr.isUnresolved())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ 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))
throw new IllegalArgumentException("Unsupported address type");
final InetSocketAddress epoint = (InetSocketAddress)endpoint;
String destHost = epoint.isUnresolved() ? epoint.getHostName()
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/net/Inet4Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/Inet6Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ public int hashCode() {
*/
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Inet6Address))
if (!(obj instanceof Inet6Address))
return false;

Inet6Address inetAddr = (Inet6Address)obj;
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/net/InetSocketAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public String toString() {

@Override
public final boolean equals(Object obj) {
if (obj == null || !(obj instanceof InetSocketAddressHolder))
if (!(obj instanceof InetSocketAddressHolder))
return false;
InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
boolean sameIP;
Expand Down Expand Up @@ -409,7 +409,7 @@ public String toString() {
*/
@Override
public final boolean equals(Object obj) {
if (obj == null || !(obj instanceof InetSocketAddress))
if (!(obj instanceof InetSocketAddress))
return false;
return holder.equals(((InetSocketAddress) obj).holder);
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/net/NetMulticastSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ 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))
throw new IllegalArgumentException("Unsupported address type");

if (oldImpl)
Expand All @@ -826,7 +826,7 @@ 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))
throw new IllegalArgumentException("Unsupported address type");

if (oldImpl)
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ 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))
return false;
Proxy p = (Proxy) obj;
if (p.type() == type()) {
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/SocksSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ protected void connect(SocketAddress endpoint, int timeout) throws IOException {
}

SecurityManager security = System.getSecurityManager();
if (endpoint == null || !(endpoint instanceof InetSocketAddress))
if (!(endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException("Unsupported address type");
InetSocketAddress epoint = (InetSocketAddress) endpoint;
if (security != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public Set<AclEntryFlag> flags() {
public boolean equals(Object ob) {
if (ob == this)
return true;
if (ob == null || !(ob instanceof AclEntry))
if (!(ob instanceof AclEntry))
return false;
AclEntry other = (AclEntry)ob;
if (this.type != other.type)
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/jdk/internal/misc/Signal.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof Signal)) {
if (!(other instanceof Signal)) {
return false;
}
Signal other1 = (Signal)other;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ protected Socket createSocket() throws IOException {
// unconnected sockets have not been implemented.
//
Throwable t = se.getCause();
if (t != null && t instanceof UnsupportedOperationException) {
if (t instanceof UnsupportedOperationException) {
return super.createSocket();
} else {
throw se;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ public void leaveGroup(InetAddress group) throws IOException {
* @throws SocketException if group is not a multicast address
*/
private static InetAddress checkGroup(SocketAddress mcastaddr) throws SocketException {
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
if (!(mcastaddr instanceof InetSocketAddress))
throw new IllegalArgumentException("Unsupported address type");
InetAddress group = ((InetSocketAddress) mcastaddr).getAddress();
if (group == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public PollingWatchKey run() throws IOException {
});
} catch (PrivilegedActionException pae) {
Throwable cause = pae.getCause();
if (cause != null && cause instanceof IOException)
throw (IOException)cause;
if (cause instanceof IOException ioe)
throw ioe;
throw new AssertionError(pae);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/windows/classes/sun/nio/fs/WindowsPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,8 @@ public int compareTo(Path obj) {

@Override
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof WindowsPath)) {
return compareTo((Path)obj) == 0;
if (obj instanceof WindowsPath path) {
return compareTo(path) == 0;
}
return false;
}
Expand Down