RFR: 8258422: Cleanup unnecessary null comparison before instanceof check in java.base [v3]
Chris Hegarty
chegar at openjdk.java.net
Wed Dec 16 09:47:59 UTC 2020
On Wed, 16 Dec 2020 09:20:09 GMT, Andrey Turbanov <github.com+741251+turbanoff at openjdk.org> wrote:
>> 8258422: Cleanup unnecessary null comparison before instanceof check in java.base
>
> Andrey Turbanov has updated the pull request incrementally with one additional commit since the last revision:
>
> 8258422: Cleanup unnecessary null comparison before instanceof check in java.base
> use instanceof pattern matching in UnixPath too
Let's take advantage of "flow scoping" to eliminate some of these casts. A few examples follow.
src/java.base/share/classes/java/net/InetSocketAddress.java line 414:
> 412: if (!(obj instanceof InetSocketAddress))
> 413: return false;
> 414: return holder.equals(((InetSocketAddress) obj).holder);
If we restructure this a little we can get:
public final boolean equals(Object obj) {
if (obj instanceof InetSocketAddress that)
return holder.equals(that.holder);
return false;
}
src/java.base/share/classes/java/net/InetSocketAddress.java line 124:
> 122: if (!(obj instanceof InetSocketAddressHolder))
> 123: return false;
> 124: InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
If we restructure this a little we can take advantage of flow scoping, e.g.
public final boolean equals(Object obj) {
if (!(obj instanceof InetSocketAddressHolder that))
return false;
boolean sameIP;
if (addr != null)
sameIP = addr.equals(that.addr);
else if (hostname != null)
sameIP = (that.addr == null) &&
hostname.equalsIgnoreCase(that.hostname);
else
sameIP = (that.addr == null) && (that.hostname == null);
return sameIP && (port == that.port);
}
-------------
Changes requested by chegar (Reviewer).
PR: https://git.openjdk.java.net/jdk/pull/20
More information about the nio-dev
mailing list