Skip to content

Commit f5727ca

Browse files
committed
8258422: Cleanup unnecessary null comparison before instanceof check in java.base
take advantage of "flow scoping" to eliminate casts
1 parent 0d2ac40 commit f5727ca

File tree

12 files changed

+32
-41
lines changed

12 files changed

+32
-41
lines changed

src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ protected void leave(InetAddress inetaddr) throws IOException {
235235

236236
protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
237237
throws IOException {
238-
if (!(mcastaddr instanceof InetSocketAddress))
238+
if (!(mcastaddr instanceof InetSocketAddress addr))
239239
throw new IllegalArgumentException("Unsupported address type");
240-
join(((InetSocketAddress)mcastaddr).getAddress(), netIf);
240+
join(addr.getAddress(), netIf);
241241
}
242242

243243
protected abstract void join(InetAddress inetaddr, NetworkInterface netIf)
@@ -253,9 +253,9 @@ protected abstract void join(InetAddress inetaddr, NetworkInterface netIf)
253253
*/
254254
protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
255255
throws IOException {
256-
if (!(mcastaddr instanceof InetSocketAddress))
256+
if (!(mcastaddr instanceof InetSocketAddress addr))
257257
throw new IllegalArgumentException("Unsupported address type");
258-
leave(((InetSocketAddress)mcastaddr).getAddress(), netIf);
258+
leave(addr.getAddress(), netIf);
259259
}
260260

261261
protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf)

src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@ protected void connect(SocketAddress address, int timeout)
213213
throws IOException {
214214
boolean connected = false;
215215
try {
216-
if (!(address instanceof InetSocketAddress))
216+
if (!(address instanceof InetSocketAddress addr))
217217
throw new IllegalArgumentException("unsupported address type");
218-
InetSocketAddress addr = (InetSocketAddress) address;
219218
if (addr.isUnresolved())
220219
throw new UnknownHostException(addr.getHostName());
221220
// recording this.address as supplied by caller before calling connect

src/java.base/share/classes/java/net/DatagramPacket.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,8 @@ public synchronized void setPort(int iport) {
352352
* @since 1.4
353353
*/
354354
public synchronized void setSocketAddress(SocketAddress address) {
355-
if (!(address instanceof InetSocketAddress))
355+
if (!(address instanceof InetSocketAddress addr))
356356
throw new IllegalArgumentException("unsupported address type");
357-
InetSocketAddress addr = (InetSocketAddress) address;
358357
if (addr.isUnresolved())
359358
throw new IllegalArgumentException("unresolved address");
360359
setAddress(addr.getAddress());

src/java.base/share/classes/java/net/HttpConnectSocketImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ protected void connect(InetAddress address, int port) throws IOException {
102102
protected void connect(SocketAddress endpoint, int timeout)
103103
throws IOException
104104
{
105-
if (!(endpoint instanceof InetSocketAddress))
105+
if (!(endpoint instanceof InetSocketAddress epoint))
106106
throw new IllegalArgumentException("Unsupported address type");
107-
final InetSocketAddress epoint = (InetSocketAddress)endpoint;
108107
String destHost = epoint.isUnresolved() ? epoint.getHostName()
109108
: epoint.getAddress().getHostAddress();
110109
final int destPort = epoint.getPort();

src/java.base/share/classes/java/net/Inet6Address.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -878,12 +878,10 @@ public int hashCode() {
878878
*/
879879
@Override
880880
public boolean equals(Object obj) {
881-
if (!(obj instanceof Inet6Address))
882-
return false;
883-
884-
Inet6Address inetAddr = (Inet6Address)obj;
885-
886-
return holder6.equals(inetAddr.holder6);
881+
if (obj instanceof Inet6Address inetAddr) {
882+
return holder6.equals(inetAddr.holder6);
883+
}
884+
return false;
887885
}
888886

889887
/**

src/java.base/share/classes/java/net/InetSocketAddress.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ public String toString() {
119119

120120
@Override
121121
public final boolean equals(Object obj) {
122-
if (!(obj instanceof InetSocketAddressHolder))
122+
if (!(obj instanceof InetSocketAddressHolder that))
123123
return false;
124-
InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
125124
boolean sameIP;
126125
if (addr != null)
127126
sameIP = addr.equals(that.addr);
@@ -409,9 +408,10 @@ public String toString() {
409408
*/
410409
@Override
411410
public final boolean equals(Object obj) {
412-
if (!(obj instanceof InetSocketAddress))
413-
return false;
414-
return holder.equals(((InetSocketAddress) obj).holder);
411+
if (obj instanceof InetSocketAddress addr) {
412+
return holder.equals(addr.holder);
413+
}
414+
return false;
415415
}
416416

417417
/**

src/java.base/share/classes/java/net/NetMulticastSocket.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -801,19 +801,19 @@ public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
801801
if (isClosed())
802802
throw new SocketException("Socket is closed");
803803

804-
if (!(mcastaddr instanceof InetSocketAddress))
804+
if (!(mcastaddr instanceof InetSocketAddress addr))
805805
throw new IllegalArgumentException("Unsupported address type");
806806

807807
if (oldImpl)
808808
throw new UnsupportedOperationException();
809809

810-
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "joinGroup");
810+
checkAddress(addr.getAddress(), "joinGroup");
811811
SecurityManager security = System.getSecurityManager();
812812
if (security != null) {
813-
security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
813+
security.checkMulticast(addr.getAddress());
814814
}
815815

816-
if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
816+
if (!addr.getAddress().isMulticastAddress()) {
817817
throw new SocketException("Not a multicast address");
818818
}
819819

@@ -826,19 +826,19 @@ public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
826826
if (isClosed())
827827
throw new SocketException("Socket is closed");
828828

829-
if (!(mcastaddr instanceof InetSocketAddress))
829+
if (!(mcastaddr instanceof InetSocketAddress addr))
830830
throw new IllegalArgumentException("Unsupported address type");
831831

832832
if (oldImpl)
833833
throw new UnsupportedOperationException();
834834

835-
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "leaveGroup");
835+
checkAddress(addr.getAddress(), "leaveGroup");
836836
SecurityManager security = System.getSecurityManager();
837837
if (security != null) {
838-
security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
838+
security.checkMulticast(addr.getAddress());
839839
}
840840

841-
if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
841+
if (!addr.getAddress().isMulticastAddress()) {
842842
throw new SocketException("Not a multicast address");
843843
}
844844

src/java.base/share/classes/java/net/Proxy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ public String toString() {
146146
* @see java.net.InetSocketAddress#equals(java.lang.Object)
147147
*/
148148
public final boolean equals(Object obj) {
149-
if (!(obj instanceof Proxy))
149+
if (!(obj instanceof Proxy p))
150150
return false;
151-
Proxy p = (Proxy) obj;
152151
if (p.type() == type()) {
153152
if (address() == null) {
154153
return (p.address() == null);

src/java.base/share/classes/java/net/SocksSocketImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ protected void connect(SocketAddress endpoint, int timeout) throws IOException {
272272
}
273273

274274
SecurityManager security = System.getSecurityManager();
275-
if (!(endpoint instanceof InetSocketAddress))
275+
if (!(endpoint instanceof InetSocketAddress epoint))
276276
throw new IllegalArgumentException("Unsupported address type");
277-
InetSocketAddress epoint = (InetSocketAddress) endpoint;
278277
if (security != null) {
279278
if (epoint.isUnresolved())
280279
security.checkConnect(epoint.getHostName(),

src/java.base/share/classes/java/nio/file/attribute/AclEntry.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,8 @@ public Set<AclEntryFlag> flags() {
342342
public boolean equals(Object ob) {
343343
if (ob == this)
344344
return true;
345-
if (!(ob instanceof AclEntry))
345+
if (!(ob instanceof AclEntry other))
346346
return false;
347-
AclEntry other = (AclEntry)ob;
348347
if (this.type != other.type)
349348
return false;
350349
if (!this.who.equals(other.who))

0 commit comments

Comments
 (0)