Bye, bye, NPE
As I announced earlier most of my time for the JDT currently goes into static analysis, more specifically into analyzing various issues relating to null references.
Since the compiler can’t read our minds on whether we intend to allow null for a method parameter, a method return etc. we should give it a little help: annotate types in method signatures with either @Nullable or @NonNull, thus creating a contract between a method and its callers.
With such contracts it is possible to write code that is provably free of NPEs — wouldn’t that be great?
Let null contracts become mainstream
The theory and some tools for checking null contracts have been around for a while. I think it is time that the Eclipse Java compiler will make this feature directly available within your favorite IDE.
Towards this goal work in bug 186342 has made good progress, but some issues still need discussing and experimenting before this feature can be released into the JDT.
First prototype available
The current prototype is available for download — see these steps for installing into a fresh Eclipse SDK 3.7 M5.
What the prototype supports:
- @NonNull and @Nullable annotations for method parameters, method return and local variables, seemlessly integrated with the existing flow analysis of the JDT compiler.
- Inheritance of null contracts with the option of compatible refinement in subtypes.
- Configuration of a global default (either @NonNull or @Nullable) to reduce the number or required annotations.
- Configuration of specific qualified names for these annotation types to support compatibility with (some) other tools
- A few quickfixes for adding obviously missing annotations.
If you like to play with these existing features you’re highly welcome to install the plugin and give feedback in the bug.
If you want to see the full story you may want to wait for some more items from my agenda:
- Null annotations for fields
- Nullity profiles for libraries
- Configurability of the default annotation per package and per type.
- More quickfixes
Behind the scenes
There’s a second piece of good news in this development: before this feature gets approved for committing into JDT head, I was faced with the usual choice to either evolve the feature outside CVS or create a branch. Since I didn’t like the efforts incurred by either approach – regarding maintenance as well as deployment – I chose the third option: Object Teams.
I transformed my recent patch from the bug into a regular OT/Equinox plug-in. The Object Teams based solution not only avoids the double maintance associated with branching but also makes the specific code for this feature much easier to read because it is localized in exactly one module. Also, the plug-in can be easier comsumed because it can be packaged and deployed independently of the concurrent evolution of the JDT.
By the way: those interested in these and other cool things that can be done with Object Teams: don’t miss our EclipseCon tutorial:
|See you at|