Configure inspection to treat test for null + throw the same as assert

Is there any way to configure the 'method ... may produce NPE' such that it treats
a specified test for null in the same manner as assert?

I've got cases along the following lines:


In the above code, if the test for null succeeds, a run time exception is thrown by
the code in the ExceptionUtil class. If I change the code with assert, the inspection
then knows that var cannot be null at the point where performSomeTask is called.

What I'd like to do is tell the inspection engine that the test and the ExceptionUtil
method does something similar and so it would not be flagged as a warning/error

Cheers,

Bonny

4 comments
Comment actions Permalink

There is no way to tell inspection to count this the same as assert. Thiis is why annotations for nullity are introduced: they explicitly tell the contract rather than hiding it in some code pattern that needs to be matched.

0
Comment actions Permalink

The contract ist hidden in the brain of the programer and
of course in the called method.

if ( var == null )
ExceptionUtil.throwBusinessRuleException( reason code)

I see two solutions here. Either we have another annotation
like @WillNeverReturn. Or IDEA is smart enough to analyse the
called method to see if it always throws an exception.

Eugene Vigdorchik schrieb:

There is no way to tell inspection to count this the same as assert. Thiis is why annotations for nullity are introduced: they explicitly tell the contract rather than hiding it in some code pattern that needs to be matched.

0
Comment actions Permalink

Well, in that case, how can I create an annotation that states what I want to achieve.
I've looked and there does not seem to be a way to mark a method as always
throwing an exception.

Also in the particular case I listed above, it is a business rule of the calling method,
not the called method, that is being violated. It would be nice if I could tell IDEA that...

Cheers,

Bonny

0
Comment actions Permalink

Sven Steiniger wrote:

The contract ist hidden in the brain of the programer and
of course in the called method.

if ( var == null )
ExceptionUtil.throwBusinessRuleException( reason code)

I see two solutions here. Either we have another annotation
like @WillNeverReturn. Or IDEA is smart enough to analyse the
called method to see if it always throws an exception.


A third solution is to avoid using methods that always throw exceptions
and simply let them return the exception instead:

if ( var == null )
throw ExceptionUtil.createBusinessRuleException(reason code)

0

Please sign in to leave a comment.