History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEADEV-13181
Type: New Feature New Feature
Status: Open Open
Priority: Major Major
Assignee: Maxim Shafirov
Reporter: Stephen Friedrich
Votes: 5
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
IDEA: Development

Configurable "assert" calls for "Contants Conditions and exceptions"

Created: 06 Dec 06 13:42   Updated: 06 Nov 08 23:25
Component/s: Code Analysis. Inspection
Fix Version/s: Undefined

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown
Issue Links:
Duplicate
 
This issue is duplicated by:
IDEA-14020 Mechanism to Designate a method as ha... Resolved

Build: 6,112
Severity: Medium


 Description  « Hide
A lot of projects out there include custom assertion helpers, see for example http://eclipsezone.com/eclipse/forums/t84288.html
Currently we have the strange situation that an inspection warning like "may throw NPE" appears if and only if some custom assert checks that a variable is not null.
class Assert {
    public static void test(boolean b, String message) {
        if (!b) {
            throw new RuntimeException(message);
        }
    }
}

public class Main {

    public static void foo(JFrame frame) {
        Assert.test(frame != null, "Need a frame");
        frame.setVisible(true);
    }
}

This triggers "may throw NullPointerException" on "frame.setVisible()".
As soon as you remove the preceding Assert the warning vanishes.

I understand this from a technical point of view, but it feels very odd.



 All   Comments   Work Log   Change History      Sort Order:
Taras Tielkes - 10 Sep 07 22:07
Stephen, if I understand correctly you'd like to annotate (possibly "external annotate") a method with a @PerformsNullCheck or something similar?

Such an annotation would indeed be nice.

An extra bonus would be: IDEA could index/remember such methods present in module classpath, and automatically provide QuickFixes based on that data.


Taras Tielkes - 10 Sep 07 22:08
IDEA-14669 is related.

Yann Cébron - 10 Sep 07 22:25
same goes for Commons Lang "Validate" class, I'd really welcome that RFE

Stephen Friedrich - 11 Sep 07 11:04
Yup, that's what I have in mind. I am not sure how it's best implemented. You'll definitely need something more flexible than @PerformsNullCheck: You need to specify which of possible multiple parameters is checked for no-nullness.
Also in my example above (from the last project I worked on) there's no dedicated not-null check, but just a simple test method that assert that the first parameter is true.

I would put this much lower on my priority list if this weren't so weird:
frame.setVisible(true);
by itself is not flagged by Idea.
Only if you do assert that frame is non-null by adding
Assert.test(frame != null);
in front of it, then Idea complains that it may be null.

Somehow I technically understand this, but it really makes the constant conditions inspection useless if the project frequently uses custom assert statements.