Parse and expose nullability of types for use in recipes (original) (raw)

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.