Skip to content

Commit 022bc9f

Browse files
turbanoffAlekseiEfimov
authored andcommitted
8258422: Cleanup unnecessary null comparison before instanceof check in java.base
Reviewed-by: chegar, aefimov
1 parent ff54b77 commit 022bc9f

22 files changed

+69
-81
lines changed

src/java.base/share/classes/java/io/File.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,8 +2190,8 @@ public int compareTo(File pathname) {
21902190
* {@code false} otherwise
21912191
*/
21922192
public boolean equals(Object obj) {
2193-
if ((obj != null) && (obj instanceof File)) {
2194-
return compareTo((File)obj) == 0;
2193+
if (obj instanceof File file) {
2194+
return compareTo(file) == 0;
21952195
}
21962196
return false;
21972197
}

src/java.base/share/classes/java/lang/reflect/Constructor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ public Type[] getGenericExceptionTypes() {
308308
* same formal parameter types.
309309
*/
310310
public boolean equals(Object obj) {
311-
if (obj != null && obj instanceof Constructor) {
312-
Constructor<?> other = (Constructor<?>)obj;
311+
if (obj instanceof Constructor<?> other) {
313312
if (getDeclaringClass() == other.getDeclaringClass()) {
314313
return equalParamTypes(parameterTypes, other.parameterTypes);
315314
}

src/java.base/share/classes/java/lang/reflect/Field.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ public Type getGenericType() {
279279
* and type.
280280
*/
281281
public boolean equals(Object obj) {
282-
if (obj != null && obj instanceof Field) {
283-
Field other = (Field)obj;
282+
if (obj instanceof Field other) {
284283
return (getDeclaringClass() == other.getDeclaringClass())
285284
&& (getName() == other.getName())
286285
&& (getType() == other.getType());

src/java.base/share/classes/java/lang/reflect/Method.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ public Type[] getGenericExceptionTypes() {
358358
* and formal parameter types and return type.
359359
*/
360360
public boolean equals(Object obj) {
361-
if (obj != null && obj instanceof Method) {
362-
Method other = (Method)obj;
361+
if (obj instanceof Method other) {
363362
if ((getDeclaringClass() == other.getDeclaringClass())
364363
&& (getName() == other.getName())) {
365364
if (!returnType.equals(other.getReturnType()))

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

Lines changed: 13 additions & 13 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 == null || !(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 == null || !(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)
@@ -292,7 +292,7 @@ public void setOption(int optID, Object o) throws SocketException {
292292
* PlainSocketImpl.setOption().
293293
*/
294294
case SO_TIMEOUT:
295-
if (o == null || !(o instanceof Integer)) {
295+
if (!(o instanceof Integer)) {
296296
throw new SocketException("bad argument for SO_TIMEOUT");
297297
}
298298
int tmp = ((Integer) o).intValue();
@@ -301,45 +301,45 @@ public void setOption(int optID, Object o) throws SocketException {
301301
timeout = tmp;
302302
return;
303303
case IP_TOS:
304-
if (o == null || !(o instanceof Integer)) {
304+
if (!(o instanceof Integer)) {
305305
throw new SocketException("bad argument for IP_TOS");
306306
}
307307
trafficClass = ((Integer)o).intValue();
308308
break;
309309
case SO_REUSEADDR:
310-
if (o == null || !(o instanceof Boolean)) {
310+
if (!(o instanceof Boolean)) {
311311
throw new SocketException("bad argument for SO_REUSEADDR");
312312
}
313313
break;
314314
case SO_BROADCAST:
315-
if (o == null || !(o instanceof Boolean)) {
315+
if (!(o instanceof Boolean)) {
316316
throw new SocketException("bad argument for SO_BROADCAST");
317317
}
318318
break;
319319
case SO_BINDADDR:
320320
throw new SocketException("Cannot re-bind Socket");
321321
case SO_RCVBUF:
322322
case SO_SNDBUF:
323-
if (o == null || !(o instanceof Integer) ||
323+
if (!(o instanceof Integer) ||
324324
((Integer)o).intValue() < 0) {
325325
throw new SocketException("bad argument for SO_SNDBUF or " +
326326
"SO_RCVBUF");
327327
}
328328
break;
329329
case IP_MULTICAST_IF:
330-
if (o == null || !(o instanceof InetAddress))
330+
if (!(o instanceof InetAddress))
331331
throw new SocketException("bad argument for IP_MULTICAST_IF");
332332
break;
333333
case IP_MULTICAST_IF2:
334-
if (o == null || !(o instanceof NetworkInterface))
334+
if (!(o instanceof NetworkInterface))
335335
throw new SocketException("bad argument for IP_MULTICAST_IF2");
336336
break;
337337
case IP_MULTICAST_LOOP:
338-
if (o == null || !(o instanceof Boolean))
338+
if (!(o instanceof Boolean))
339339
throw new SocketException("bad argument for IP_MULTICAST_LOOP");
340340
break;
341341
case SO_REUSEPORT:
342-
if (o == null || !(o instanceof Boolean)) {
342+
if (!(o instanceof Boolean)) {
343343
throw new SocketException("bad argument for SO_REUSEPORT");
344344
}
345345
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {

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

Lines changed: 10 additions & 11 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 == null || !(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
@@ -259,59 +258,59 @@ public void setOption(int opt, Object val) throws SocketException {
259258
* PlainSocketImpl.setOption().
260259
*/
261260
case SO_LINGER:
262-
if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean)))
261+
if (!(val instanceof Integer) && !(val instanceof Boolean))
263262
throw new SocketException("Bad parameter for option");
264263
if (val instanceof Boolean) {
265264
/* true only if disabling - enabling should be Integer */
266265
on = false;
267266
}
268267
break;
269268
case SO_TIMEOUT:
270-
if (val == null || (!(val instanceof Integer)))
269+
if (!(val instanceof Integer))
271270
throw new SocketException("Bad parameter for SO_TIMEOUT");
272271
int tmp = ((Integer) val).intValue();
273272
if (tmp < 0)
274273
throw new IllegalArgumentException("timeout < 0");
275274
timeout = tmp;
276275
break;
277276
case IP_TOS:
278-
if (val == null || !(val instanceof Integer)) {
277+
if (!(val instanceof Integer)) {
279278
throw new SocketException("bad argument for IP_TOS");
280279
}
281280
trafficClass = ((Integer)val).intValue();
282281
break;
283282
case SO_BINDADDR:
284283
throw new SocketException("Cannot re-bind socket");
285284
case TCP_NODELAY:
286-
if (val == null || !(val instanceof Boolean))
285+
if (!(val instanceof Boolean))
287286
throw new SocketException("bad parameter for TCP_NODELAY");
288287
on = ((Boolean)val).booleanValue();
289288
break;
290289
case SO_SNDBUF:
291290
case SO_RCVBUF:
292-
if (val == null || !(val instanceof Integer) ||
291+
if (!(val instanceof Integer) ||
293292
!(((Integer)val).intValue() > 0)) {
294293
throw new SocketException("bad parameter for SO_SNDBUF " +
295294
"or SO_RCVBUF");
296295
}
297296
break;
298297
case SO_KEEPALIVE:
299-
if (val == null || !(val instanceof Boolean))
298+
if (!(val instanceof Boolean))
300299
throw new SocketException("bad parameter for SO_KEEPALIVE");
301300
on = ((Boolean)val).booleanValue();
302301
break;
303302
case SO_OOBINLINE:
304-
if (val == null || !(val instanceof Boolean))
303+
if (!(val instanceof Boolean))
305304
throw new SocketException("bad parameter for SO_OOBINLINE");
306305
on = ((Boolean)val).booleanValue();
307306
break;
308307
case SO_REUSEADDR:
309-
if (val == null || !(val instanceof Boolean))
308+
if (!(val instanceof Boolean))
310309
throw new SocketException("bad parameter for SO_REUSEADDR");
311310
on = ((Boolean)val).booleanValue();
312311
break;
313312
case SO_REUSEPORT:
314-
if (val == null || !(val instanceof Boolean))
313+
if (!(val instanceof Boolean))
315314
throw new SocketException("bad parameter for SO_REUSEPORT");
316315
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT))
317316
throw new UnsupportedOperationException("unsupported option");

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 == null || !(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 == null || !(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/Inet4Address.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ public int hashCode() {
352352
* @see java.net.InetAddress#getAddress()
353353
*/
354354
public boolean equals(Object obj) {
355-
return (obj != null) && (obj instanceof Inet4Address) &&
356-
(((InetAddress)obj).holder().getAddress() == holder().getAddress());
355+
return (obj instanceof Inet4Address inet4Address) &&
356+
inet4Address.holder().getAddress() == holder().getAddress();
357357
}
358358

359359
// Utilities

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 == null || !(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 == null || !(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 == null || !(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 == null || !(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 == null || !(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 == null || !(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 == null || !(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 == null || !(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))

src/java.base/share/classes/jdk/internal/misc/Signal.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,17 @@ public String getName() {
9595
/**
9696
* Compares the equality of two <code>Signal</code> objects.
9797
*
98-
* @param other the object to compare with.
98+
* @param obj the object to compare with.
9999
* @return whether two <code>Signal</code> objects are equal.
100100
*/
101-
public boolean equals(Object other) {
102-
if (this == other) {
101+
public boolean equals(Object obj) {
102+
if (this == obj) {
103103
return true;
104104
}
105-
if (other == null || !(other instanceof Signal)) {
106-
return false;
105+
if (obj instanceof Signal other) {
106+
return name.equals(other.name) && (number == other.number);
107107
}
108-
Signal other1 = (Signal)other;
109-
return name.equals(other1.name) && (number == other1.number);
108+
return false;
110109
}
111110

112111
/**

src/java.base/share/classes/sun/net/www/protocol/http/Negotiator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ static Negotiator getNegotiator(HttpCallerInfo hci) {
6666
} catch (ReflectiveOperationException roe) {
6767
finest(roe);
6868
Throwable t = roe.getCause();
69-
if (t != null && t instanceof Exception)
70-
finest((Exception)t);
69+
if (t instanceof Exception exception)
70+
finest(exception);
7171
return null;
7272
}
7373
}

0 commit comments

Comments
 (0)