RFR: 8258422: Cleanup unnecessary null comparison before instanceof check in java.base
Andrey Turbanov
github.com+741251+turbanoff at openjdk.java.net
Tue Dec 15 18:58:07 UTC 2020
On Mon, 2 Nov 2020 09:15:31 GMT, Chris Hegarty <chegar at openjdk.org> wrote:
>> I’ll see if I can get somebody to take a look at this.
>
> This seems like a reasonable change, which improves readability.
>
> My preference is to wait a little longer (hopefully no more than a couple of weeks), until [JEP 394](https://openjdk.java.net/jeps/394) - "Pattern Matching for instanceof" is finalised, then we can remove the explicit casts in many of these cases. For example:
>
> --- a/src/java.base/share/classes/java/io/File.java
> +++ b/src/java.base/share/classes/java/io/File.java
> @@ -2191,8 +2191,8 @@ public class File
> * {@code false} otherwise
> */
> public boolean equals(Object obj) {
> - if ((obj != null) && (obj instanceof File)) {
> - return compareTo((File)obj) == 0;
> + if (obj instanceof File file) {
> + return compareTo(file) == 0;
> }
> return false;
> }
Related issue - https://bugs.openjdk.java.net/browse/JDK-8257448
-------------
PR: https://git.openjdk.java.net/jdk/pull/20
More information about the nio-dev
mailing list