Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Null analysis seems to ignore short-circuiting
Null analysis seems to ignore short-circuiting [message #1833427] Mon, 12 October 2020 22:18 Go to next message
David M. Karr is currently offline David M. KarrFriend
Messages: 722
Registered: July 2009
Senior Member
I've been experimenting with null annotations.

I'm looking at the following block of code, which I did not write, and I've simplified it:

	if (null != response.getCart().getStatus()
			&& response.getCart().getStatus() .equalsIgnoreCase("SUCCESS")) {


Without any null annotations, no warnings appear here.

If I go to the "Cart" class and add "@NonNullByDefault" to the class, that results in the following warning on the first line of that block:

Quote:
Redundant null check: comparing '@NonNull String' against null


This is expected, as it now believes that all the properties of that are not null, so I shouldn't compare it against null.

However, that particular property actually can be null, so I added @Nullable to the return type of the getter. This now results in the following warning on the SECOND line:

Quote:
Potential null pointer access: The method getStatus() may return null


This is certainly true, but in this block of code, it's not possible to reach this line, as the first condition in the ANDed expression will be false, so it will never execute the second subexpression.

Is it reasonable for it to check for short-circuiting here and NOT emit this warning?
Re: Null analysis seems to ignore short-circuiting [message #1833430 is a reply to message #1833427] Tue, 13 October 2020 04:57 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7098
Registered: July 2009
Senior Member
Hi

No. Java is not side effect free, so there is no guarantee that the second call to getCart() returns the same value as the first. It might have an internal counter, there might be concurrent threads.

Note that the enthusiasm for bye bye NPE has sadly gone with the departure of Stephan. The analysis has many useful properties, but it is imperfect and in some respects just pushes inadequacies from one place to another. Have a look at the outstanding null-analysis Bugzillas.

Regards

Ed Willink
Re: Null analysis seems to ignore short-circuiting [message #1833431 is a reply to message #1833430] Tue, 13 October 2020 05:13 Go to previous messageGo to next message
David M. Karr is currently offline David M. KarrFriend
Messages: 722
Registered: July 2009
Senior Member
So you're of the opinion that null annotations is a rabbit hole that won't provide an overall positive value at this point? I wouldn't be terribly surprised to hear that. I think we may get more benefit from strategies for preventing null objects.
Re: Null analysis seems to ignore short-circuiting [message #1833436 is a reply to message #1833431] Tue, 13 October 2020 06:55 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7098
Registered: July 2009
Senior Member
Hi

I have been using null annotations for over five years and find them very useful, but also very inadequate since the lack of PDE support means that each project can be a distinct non-null-island. Now that I know that PDE will almost certainly never fix up external annotations, I am endeavouring to use the analysis without external annotations and find that it is very irritating to have to add a lot of assert != null / maybeNull() fixes for analysis defeating methods such as Map.get. On balance, i think that the fixes to accommodate awkward methods, missing EMF support, bad quick fixes/refactorings are a price worth paying, but only just.

Regards

Ed Willink
Previous Topic:Workspace becoming unreadable when switching devices
Next Topic:How to create error marker on file in order to block the debug launch
Goto Forum:
  


Current Time: Fri Apr 16 18:34:31 GMT 2021

Powered by FUDForum. Page generated in 0.02912 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top