Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Please take java.util.Objects methods into account for potential NullPointer checks
Please take java.util.Objects methods into account for potential NullPointer checks [message #1823227] Mon, 23 March 2020 09:23 Go to next message
Michael Moser is currently offline Michael MoserFriend
Messages: 58
Registered: October 2012
Location: Z├╝rich, Switzerland
Member
I find potential null pointer access warnings really useful. However, since Java (1.)7 there is a utility class java.util.Objects which offers a couple of checks such as
boolean isNull(<obj>) or
boolean nonNull<obj>)

If would be great, if the Java compiler's null-checks could take these standard methods into account and not flag snippets like
...
if (java.util.Objects.nonNull(account)) {
long id = account.getId();
... <doing something with account here> ...
}
...

as potential null-pointer access. Otherwise one tends to ignore or even disable these warnings and that would really be a pity.
Re: Please take java.util.Objects methods into account for potential NullPointer checks [message #1823236 is a reply to message #1823227] Mon, 23 March 2020 12:13 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1828
Registered: July 2009
Senior Member
I already have plans to integrate knowledge about a few more well known utility methods into analysis (don't have the bug number off hand), but for the particular case of Objects.nonNull() your code snippet is abuse of the API, obscuring that you are doing nothing else but a traditional "account != null" check. Please read the javadoc of that method. It is not meant to be used this way.
Re: Please take java.util.Objects methods into account for potential NullPointer checks [message #1823561 is a reply to message #1823236] Sat, 28 March 2020 23:11 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1828
Registered: July 2009
Senior Member
The case of Objects.isNull() or Objects.nonNull() didn't convince me, but we already had a similar request for Class.isInstance(), so I did implement the necessary analysis, and while I was at it, I included the two methods from java.util.Objects. Details are in https://bugs.eclipse.org/bugs/show_bug.cgi?id=465085

Next I may want to implement a warning when isNull() or nonNull() is explicitly called ;p
Previous Topic:Eclipse Not Responding Errors
Next Topic:error in running gradle based project with java9+ modules
Goto Forum:
  


Current Time: Sat Oct 31 13:04:26 GMT 2020

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

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

Back to the top