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

Parse and expose nullability of types for use in recipes #3221

Open
timtebeek opened this issue May 8, 2023 · 2 comments
Open

Parse and expose nullability of types for use in recipes #3221

timtebeek opened this issue May 8, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@timtebeek
Copy link
Contributor

Kotlin quite clearly has explicit nullability, but some Java projects can somewhat express the same with nullability annotations. In addition to that we can deduce nullabillity once for instance a method has been called on an object.

It would be great if we could capture and expose this information through our type system, such that recipes can query this when inserting code snippets. Notably this came up recently in apache/maven-pmd-plugin#124 (comment), where we want to forgo adding null checks where they are not needed. Ideally this is something we determine ahead of time, even if limited.

Once exposed we can also write clean up recipes that remove excessive null checks. Even if we start small (for instance only looking at Kotlin types, or some nullability annotations, then we can gradually expand this over time, to bring more value to repeatedly running recipes that use this information.

I've not looked at implementation specifics yet, merely discussed this with Knut as it came up recently for Kotlin on Slack.

@timtebeek timtebeek added the enhancement New feature or request label May 8, 2023
@timtebeek timtebeek moved this to Backlog in OpenRewrite May 8, 2023
@knutwannheden
Copy link
Contributor

In addition to the nullability information which can be determined from the declarations (Java nullability annotations or Kotlin / TypeScript types) and be amended to the JavaType objects, there is also the aspect of the data flow analysis (e.g. do we know that this variable is null or not null at this point). The latter would have to be computed from the LST using a visitor. Both these sources of information are useful on their own (and can also be developed independently to some extent), but for the full benefit, it would be great if recipes could rely on the combined result of both these components when making a decision on how to generate a certain piece of code.

@blipper
Copy link
Contributor

blipper commented Oct 10, 2024

Note that native null restricted and null-types is coming to the language soon. https://openjdk.org/jeps/8303099. After drops https://openjdk.org/jeps/492 so JDK 24/25/26? Probably worth getting started on this sooner rather than later imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

3 participants