Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Prevent potential NPEs and improve nullability handling #89999

Conversation

melquiadess
Copy link
Contributor

Context

This PR aims to improve nullability handling in Godot's Android lib module:

  • Kotlin's !! operator is used in a few lines, whereas elsewhere the safer ?. is used - this is now replaced with the safe ?. (or ?.let) operator consistently; (usually !! would only be used very rarely, and with a good reason - there is one place left in this PR where I believe !! makes sense -> GodotActivity.kt, L79)
  • Java's if (x != null) replaced with idiomatic Kotlin's ?.
  • FileAccessHandler.renameFile() accepted nullable args, (and so its use sites were again having !! for the args passed to this function), but it can be inferred that the args the function receives are non-nullable, therefore we can get rid of nullability in those args (and so the !! use)
  • Godot.getRotatedValues() - this could produce an NPE in onSensorChanged() in some situations - fixed

There are 2 outcomes from the above:

  • safer code (potential NPEs eliminated, non-nullable function args)
  • more consistent code (Kotlin conventions followed more closely)

@melquiadess melquiadess requested a review from a team as a code owner March 28, 2024 23:17
Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup looks good!

Added some comments to address and it should be good to go.

Log.v(TAG, "Force quitting Godot instance")
ProcessPhoenix.forceQuit(this)
godotFragment?.let {
if (instance == it.godot) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the original logic has a triple equal === for referential equality; looks like it was inadvertently converted to a double equals ==.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch, I missed that, thanks! Will fix...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Log.v(TAG, "Restarting Godot instance...")
ProcessPhoenix.triggerRebirth(this)
godotFragment?.let {
if (instance == it.godot) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as above; the original logic has a triple equal === for referential equality; looks like it was inadvertently converted to a double equals ==.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks

This PR prevents potential NPEs, and follows Kotlin conventions more closely
by replacing the unsafe !! operator with safe ?. (or ?.let) (usually
!! would only be used very rarely, and with a good reason - there is one
place left in this PR where !! makes sense), and by replacing Java style
'if (x != null)' with Kotlin's '?.'
@melquiadess melquiadess force-pushed the prevent-potential-NPEs-and-improve-nullability-handling branch from 26fbf64 to 70ea3e2 Compare March 28, 2024 23:38
Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@melquiadess
Copy link
Contributor Author

Looks good!

Thanks for a quick review

@akien-mga akien-mga changed the title Prevent potential NPEs and improve nullability handling Android: Prevent potential NPEs and improve nullability handling Apr 4, 2024
@akien-mga akien-mga merged commit 88f7012 into godotengine:master Apr 4, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

@melquiadess melquiadess deleted the prevent-potential-NPEs-and-improve-nullability-handling branch April 8, 2024 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants