Skip to content

Commit 603cd36

Browse files
committed
8258422: Cleanup unnecessary null comparison before instanceof check in java.base
use instanceof pattern matching
1 parent c0d8b29 commit 603cd36

File tree

8 files changed

+13
-16
lines changed

8 files changed

+13
-16
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 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 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 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 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/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 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/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 instanceof Exception)
70-
finest((Exception)t);
69+
if (t instanceof Exception exception)
70+
finest(exception);
7171
return null;
7272
}
7373
}

src/java.base/share/classes/sun/nio/fs/PollingWatchService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public PollingWatchKey run() throws IOException {
149149
});
150150
} catch (PrivilegedActionException pae) {
151151
Throwable cause = pae.getCause();
152-
if (cause instanceof IOException)
153-
throw (IOException)cause;
152+
if (cause instanceof IOException ioe)
153+
throw ioe;
154154
throw new AssertionError(pae);
155155
}
156156
}

src/java.base/windows/classes/sun/nio/fs/WindowsPath.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,8 @@ public int compareTo(Path obj) {
798798

799799
@Override
800800
public boolean equals(Object obj) {
801-
if (obj instanceof WindowsPath) {
802-
return compareTo((Path)obj) == 0;
801+
if (obj instanceof WindowsPath path) {
802+
return compareTo(path) == 0;
803803
}
804804
return false;
805805
}

0 commit comments

Comments
 (0)