Re: IDE plugin to help null handling
neuneudr@yahoo.fr wrote:
My code is littered with @NotNull and should the possibility
"Littered", eh?
of null being assigned to an @NotNull annotated reference
(including reference returned by methods), then IntelliJ IDEA
tells it to you immediately.
All IDEs that I've used, and javac itself, recognize an assertion for this
purpose, though of course the behavior is different from an annotation.
In the very rare case where null is acceptable, then you shoul
use the @Nullable annotation to make your intent clear.
People who have not used it do not realize how helpful it is.
The IntelliJ IDEA itself, which is arguably one of the very
best client-side Java application ever developped, is using
these @NotNull/@Nullable annotations extensively.
I turn a jaundiced eye to an IDE-specific approach when a portable one works
well enough.
Using, for example, Eclipse or derived IDEs, a snippet like
if ( foo == null )
{
doSomethingThatCouldThrowException();
}
foo.execute();
will cause a complaint at the foo method call that foo might be null. Suppose
the 'doSomething...()' method is known to throw an exception - you could
precede 'foo.execute()' with an 'assert foo != null;' to satisfy the IDE.
More generally I suggest that the null branch explicitly throw whatever it
needs to rather than depend on a method to do so, and optionally assert the
invariant after the block:
if ( foo == null )
{
throw new IllegalArgumentException( "null foo" );
}
assert foo != null;
foo.execute();
--
Lew