Skip to content

Commit fdd3941

Browse files
committed
8263233: Update java.net and java.nio to use instanceof pattern variable
Reviewed-by: dfuchs, bpb, chegar, michaelm
1 parent 3fe8a46 commit fdd3941

17 files changed

+39
-73
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -80,10 +80,9 @@ public Void run() {
8080
super(delegate);
8181
this.socket = socket;
8282
SocketAddress a = proxy.address();
83-
if ( !(a instanceof InetSocketAddress) )
83+
if ( !(a instanceof InetSocketAddress ad) )
8484
throw new IllegalArgumentException("Unsupported address type");
8585

86-
InetSocketAddress ad = (InetSocketAddress) a;
8786
server = ad.getHostString();
8887
port = ad.getPort();
8988
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -715,9 +715,8 @@ public String toString() {
715715
public boolean equals(Object obj) {
716716
if (obj == this)
717717
return true;
718-
if (!(obj instanceof HttpCookie))
718+
if (!(obj instanceof HttpCookie other))
719719
return false;
720-
HttpCookie other = (HttpCookie)obj;
721720

722721
// One http cookie is equal to another cookie (RFC 2965 sec. 3.3.3) if:
723722
// 1. they come from same domain (case-insensitive),

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -262,10 +262,9 @@ String getHostAddress() {
262262
}
263263

264264
public boolean equals(Object o) {
265-
if (! (o instanceof Inet6AddressHolder)) {
265+
if (!(o instanceof Inet6AddressHolder that)) {
266266
return false;
267267
}
268-
Inet6AddressHolder that = (Inet6AddressHolder)o;
269268

270269
return Arrays.equals(this.ipaddress, that.ipaddress);
271270
}
@@ -525,10 +524,9 @@ private static int deriveNumericScope (byte[] thisAddr, NetworkInterface ifc) th
525524
Enumeration<InetAddress> addresses = ifc.getInetAddresses();
526525
while (addresses.hasMoreElements()) {
527526
InetAddress addr = addresses.nextElement();
528-
if (!(addr instanceof Inet6Address)) {
527+
if (!(addr instanceof Inet6Address ia6_addr)) {
529528
continue;
530529
}
531-
Inet6Address ia6_addr = (Inet6Address)addr;
532530
/* check if site or link local prefixes match */
533531
if (!isDifferentLocalAddressType(thisAddr, ia6_addr.getAddress())){
534532
/* type not the same, so carry on searching */

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -101,17 +101,10 @@ public short getNetworkPrefixLength() {
101101
* @see java.net.InterfaceAddress#hashCode()
102102
*/
103103
public boolean equals(Object obj) {
104-
if (obj instanceof InterfaceAddress) {
105-
InterfaceAddress cmp = (InterfaceAddress) obj;
106-
107-
if (Objects.equals(address, cmp.address) &&
104+
return obj instanceof InterfaceAddress cmp &&
105+
Objects.equals(address, cmp.address) &&
108106
Objects.equals(broadcast, cmp.broadcast) &&
109-
maskLength == cmp.maskLength)
110-
{
111-
return true;
112-
}
113-
}
114-
return false;
107+
maskLength == cmp.maskLength;
115108
}
116109

117110
/**

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -216,9 +216,8 @@ public synchronized void bind(SocketAddress addr) throws SocketException {
216216
throw new SocketException("already bound");
217217
if (addr == null)
218218
addr = new InetSocketAddress(0);
219-
if (!(addr instanceof InetSocketAddress))
219+
if (!(addr instanceof InetSocketAddress epoint))
220220
throw new IllegalArgumentException("Unsupported address type!");
221-
InetSocketAddress epoint = (InetSocketAddress) addr;
222221
if (epoint.isUnresolved())
223222
throw new SocketException("Unresolved address");
224223
InetAddress iaddr = epoint.getAddress();
@@ -259,9 +258,8 @@ public void connect(InetAddress address, int port) {
259258
public void connect(SocketAddress addr) throws SocketException {
260259
if (addr == null)
261260
throw new IllegalArgumentException("Address can't be null");
262-
if (!(addr instanceof InetSocketAddress))
261+
if (!(addr instanceof InetSocketAddress epoint))
263262
throw new IllegalArgumentException("Unsupported address type");
264-
InetSocketAddress epoint = (InetSocketAddress) addr;
265263
if (epoint.isUnresolved())
266264
throw new SocketException("Unresolved address");
267265
connectInternal(epoint.getAddress(), epoint.getPort());

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -590,10 +590,9 @@ public boolean isVirtual() {
590590
* @see java.net.InetAddress#getAddress()
591591
*/
592592
public boolean equals(Object obj) {
593-
if (!(obj instanceof NetworkInterface)) {
593+
if (!(obj instanceof NetworkInterface that)) {
594594
return false;
595595
}
596-
NetworkInterface that = (NetworkInterface)obj;
597596
if (this.name != null ) {
598597
if (!this.name.equals(that.name)) {
599598
return false;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,8 @@ public void bind(SocketAddress endpoint, int backlog) throws IOException {
373373
throw new SocketException("Already bound");
374374
if (endpoint == null)
375375
endpoint = new InetSocketAddress(0);
376-
if (!(endpoint instanceof InetSocketAddress))
376+
if (!(endpoint instanceof InetSocketAddress epoint))
377377
throw new IllegalArgumentException("Unsupported address type");
378-
InetSocketAddress epoint = (InetSocketAddress) endpoint;
379378
if (epoint.isUnresolved())
380379
throw new SocketException("Unresolved address");
381380
if (backlog < 1)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,10 +611,9 @@ public void connect(SocketAddress endpoint, int timeout) throws IOException {
611611
if (isConnected())
612612
throw new SocketException("already connected");
613613

614-
if (!(endpoint instanceof InetSocketAddress))
614+
if (!(endpoint instanceof InetSocketAddress epoint))
615615
throw new IllegalArgumentException("Unsupported address type");
616616

617-
InetSocketAddress epoint = (InetSocketAddress) endpoint;
618617
InetAddress addr = epoint.getAddress ();
619618
int port = epoint.getPort();
620619
checkAddress(addr, "connect");

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -860,14 +860,12 @@ void getIP()
860860
public boolean implies(Permission p) {
861861
int i,j;
862862

863-
if (!(p instanceof SocketPermission))
863+
if (!(p instanceof SocketPermission that))
864864
return false;
865865

866866
if (p == this)
867867
return true;
868868

869-
SocketPermission that = (SocketPermission) p;
870-
871869
return ((this.mask & that.mask) == that.mask) &&
872870
impliesIgnoreMask(that);
873871
}
@@ -1040,11 +1038,9 @@ public boolean equals(Object obj) {
10401038
if (obj == this)
10411039
return true;
10421040

1043-
if (! (obj instanceof SocketPermission))
1041+
if (! (obj instanceof SocketPermission that))
10441042
return false;
10451043

1046-
SocketPermission that = (SocketPermission) obj;
1047-
10481044
//this is (overly?) complex!!!
10491045

10501046
// check the mask first
@@ -1379,15 +1375,13 @@ public SocketPermissionCollection() {
13791375
*/
13801376
@Override
13811377
public void add(Permission permission) {
1382-
if (! (permission instanceof SocketPermission))
1378+
if (! (permission instanceof SocketPermission sp))
13831379
throw new IllegalArgumentException("invalid permission: "+
13841380
permission);
13851381
if (isReadOnly())
13861382
throw new SecurityException(
13871383
"attempt to add a Permission to a readonly PermissionCollection");
13881384

1389-
SocketPermission sp = (SocketPermission)permission;
1390-
13911385
// Add permission to map if it is absent, or replace with new
13921386
// permission if applicable. NOTE: cannot use lambda for
13931387
// remappingFunction parameter until JDK-8076596 is fixed.
@@ -1426,11 +1420,9 @@ public SocketPermission apply(SocketPermission existingVal,
14261420
@Override
14271421
public boolean implies(Permission permission)
14281422
{
1429-
if (! (permission instanceof SocketPermission))
1423+
if (! (permission instanceof SocketPermission np))
14301424
return false;
14311425

1432-
SocketPermission np = (SocketPermission) permission;
1433-
14341426
int desired = np.getMask();
14351427
int effective = 0;
14361428
int needed = desired;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -57,8 +57,7 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
5757
SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
5858
super(delegate);
5959
SocketAddress a = proxy.address();
60-
if (a instanceof InetSocketAddress) {
61-
InetSocketAddress ad = (InetSocketAddress) a;
60+
if (a instanceof InetSocketAddress ad) {
6261
// Use getHostString() to avoid reverse lookups
6362
server = ad.getHostString();
6463
serverPort = ad.getPort();

0 commit comments

Comments
 (0)