RFR: 8258422: Cleanup unnecessary null comparison before instanceof check in java.base [v3]

Paul Sandoz paul.sandoz at oracle.com
Thu Dec 17 17:31:49 UTC 2020



> On Dec 16, 2020, at 1:47 AM, Chris Hegarty <chegar at openjdk.java.net> wrote:
> 
> 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;
>    }
> 

It’s also possible to use a ternary expression as in:

   return (obj instanceof InetSocketAddress that) ? holder.equals(that.holder) : false;

Not suggesting you have to do that here. More for information purposes for those that might not know.

Paul.


More information about the core-libs-dev mailing list