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

File tree

22 files changed

+69
-81
lines changed

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
/**

0 commit comments

Comments
 (0)