Bug 435207 - [compiler][null] support more assert utilities: Spring Assert
Summary: [compiler][null] support more assert utilities: Spring Assert
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.4   Edit
Hardware: PC Windows 7
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2014-05-19 09:20 EDT by Marton Dinnyes CLA
Modified: 2023-01-02 17:27 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marton Dinnyes CLA 2014-05-19 09:20:27 EDT
When using Spring's Assert to handle invalid arguments, I'm getting "Potential null pointer access" afterwards. "Null check" is given as an argument, it's not in "if" condition anywhere.

Example:

    public void isEmpty(Object arg) {
        Assert.notNull(arg != null, "Invalid parameter.");
        System.out.println(arg.hashCode() > 0); // <-- getting warn here for "arg"
    }
Comment 1 Stephan Herrmann CLA 2014-05-19 13:05:35 EDT
We have support to recognize a few utility methods as asserts.
Apparently, nobody brought the Spring assert to our attention when we implemented that.

See bug 382069 for a list of supported assert facilities.

To get support for Spring's Assert please specify the qualified class name, and all methods that you think should be considered by null analysis, along with a description of the underlying contract. Thanks.

BTW: to explain why you are getting the warning: when the flow analysis sees the expression "arg != null", it recognizes that the developer wasn't 100% sure about the nullness of 'arg', hence it assumes that past this point dereference without check is unsafe.
Comment 2 Marton Dinnyes CLA 2014-05-20 08:05:10 EDT
Seems this is completely invalid.

That Assert is called in wrong way, unfortunately I picked up an existing example written be someone else. It should be Assert.notNull(arg, "...");

In fact, spring's assert is org.springframework.util.Assert.notNull to be considered as runtime null validator.
Comment 3 Stephan Herrmann CLA 2014-05-20 08:18:18 EDT
(In reply to Marton Dinnyes from comment #2)
> Seems this is completely invalid.
> 
> That Assert is called in wrong way, unfortunately I picked up an existing
> example written be someone else. It should be Assert.notNull(arg, "...");

n.p. thanks for the update.
 
> In fact, spring's assert is org.springframework.util.Assert.notNull to be
> considered as runtime null validator.

Since I don't have spring installed: please provide the exact signature that should be recognized by the compiler. Is it just this one method or are there more methods like this? Also a link to their Javadoc would help.
Comment 4 Marton Dinnyes CLA 2014-05-20 08:27:10 EDT
Object MUST be null:

org.springframework.util.Assert.isNull(Object)
org.springframework.util.Assert.isNull(Object, String)

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/util/Assert.html#isNull(java.lang.Object)
http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/util/Assert.html#isNull(java.lang.Object, java.lang.String)

Object MUST NOT be null:

org.springframework.util.Assert.notNull(Object)
org.springframework.util.Assert.notNull(Object, String)

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/util/Assert.html#notNull(java.lang.Object)
http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/util/Assert.html#notNull(java.lang.Object, java.lang.String)

These are links to 3.2.x javadoc, however it hasn't changed ever, and also the same in 4.x.
Comment 5 Marton Dinnyes CLA 2014-05-20 08:30:56 EDT
You might also want to consider covering guava's com.google.common.base.Preconditions

Example, in checkArgument(boolean expression) people tend to state

Preconditions.checkArgument(arg != null); // <-- throws exception if false

(instead of using .checkNotNull)

So after a line like this, arg will not be null.
Comment 6 Stephan Herrmann CLA 2014-05-20 08:36:55 EDT
Thanks for the references.

com.google.common.base.Preconditions is already covered as per the bug I mentioned earlier.

Note that any changes we are discussing now will be scheduled for 4.5.
Comment 7 Marton Dinnyes CLA 2015-01-30 08:18:39 EST
Would it be possible to make a configuration page to cover these kind of checks on user side? There might be other libraries offering these kind of checks which are not even public (I am facing this right now).

I should be able to give a list of static methods which would be considered in null check analysis.
Comment 8 Stephan Herrmann CLA 2015-02-01 14:29:32 EST
(In reply to Marton Dinnyes from comment #7)
> Would it be possible to make a configuration page to cover these kind of
> checks on user side? There might be other libraries offering these kind of
> checks which are not even public (I am facing this right now).
> 
> I should be able to give a list of static methods which would be considered
> in null check analysis.

The compiler can only consider s.t. of which it "knows" the exact semantics. If you say you have some similar methods in a library, who will ensure that those have the exact same meaning as the utilities we have examined and added to the analysis? If there's a subtle difference, it's the compiler that will get blamed for "wrong" or "missing" warnings. Hm ...

OTOH, after the advent of java.util.Objects.requireNonNull, i.e., a solution that is standard and available to everyone, do we really need to support gazillion of "identical" methods?
Comment 9 Stephan Herrmann CLA 2015-04-23 14:23:16 EDT
Ran out of time for 4.5, sorry.
Comment 10 Stephan Herrmann CLA 2016-03-25 10:28:39 EDT
Too much on my plate for 4.6. Bulk deferral to 4.7
Comment 11 Stephan Herrmann CLA 2017-05-16 12:05:03 EDT
Ran out of time for 4.7. Bulk move to 4.8.
Comment 12 Manoj N Palat CLA 2018-05-16 12:56:25 EDT
bulk move out of 4.8
Comment 13 Eclipse Genie CLA 2020-11-10 18:28:40 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 14 Eclipse Genie CLA 2023-01-02 17:27:02 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.