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

Key: IDEABKL-4251
Type: New Feature New Feature
Status: Open Open
Priority: Normal Normal
Assignee: Maxim Shafirov
Reporter: Vladislav Kaznacheev
Votes: 3
Watchers: 5
Available Workflow Actions

Mark as Stalled
Operations

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

Need option to treat all non-annotated defintions as nullable

Created: 19 May 05 13:39   Updated: 22 Oct 08 07:28
Component/s: None
Affects Version/s: None
Fix Version/s: None

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown

Build: 3,339
Severity: Medium


 Description  « Hide
I know you guys and Keith Lea talked about non-annotated elements a little bit. I just thought I'd give a user's point of view.

Let's say I have the following code
public void doSomething (@NotNull Object p1) {
// Hey I love annotations! I can guarantee that if I have no warnings, p1 is never null.
// Great!
int hashCode = p1.hashCode();
}

Now I have an external library with a method
/**

  • @return maybe null
    */
    public Object getMaybeNull () {
    return null;
    }

And my code is

doSomething (getMaybeNull());

Idea won't warn me, because the method isn't annotated. But I'll get a NPE in my code that I thought was safe. I then have two options, write
@Nullable Object ret = getMaybeNull ();
doSomething (ret);
which is kind of ugly and wasteful. Or I do

public void doSomething (@NotNull Object p1) {
// Hmm, not so sure about these annotations now
if (p1 == null) throw new IllegalArgumentException ("p1 may not be null");

int hashCode = p1.hashCode();
}

which of course is ugly and will be flagged by Idea.

It seems to me that there are certain circumstances where we should suppose that unannotated elements are @Nullable. Presumably you have played with this and found that it gives far too much yellow. But it seems to me that the assignment of an unknown element type to an element which has been declared to be @NotNull should be flagged, or we should have the option to flag.



 All   Comments   Work Log   Change History      Sort Order:
NNTP User - 19 May 05 13:58
It looks like/has a lot in common with :
"@Nullable inspection improvement"
http://www.jetbrains.net/jira/browse/IDEA-2051

Alain


Jonas Kvarnstrom - 07 Jun 05 17:56
Newsgroup discussions lead to two slightly different suggestions:

1) Treat non-annotated variables exactly as @Nullable variables are treated now. If you do this without further heuristics, then even System.out.println("") will trigger an NPE warning, because System.out is non-annotated and would be assumed to be @Nullable. You'd have to place this within "if (System.out != null)".

2) Only treat non-annotated variables differently in an assignment to a @NotNull variable. This will only trigger warnings in those parts of your code that you've actually annotated with @NotNull, that is, those parts where you've explicitly stated that you want a warning if the variable might ever be assigned the value null. This is the behaviour I would like to see (I think...).


Derek Foster - 25 Jul 05 23:18
I definitely prefer the #2 option, above. I mostly view the "@Nullable" annotation as a comment to the person writing code. In general, the compiler should assume that anything not declared @NotNull is nullable, but it should only warn about cases where:
1) A @Nullable variable is used in a context where it must not be null, such as a method call.
2) A @Nullable or unannotated variable is assigned to a @NotNull variable.

See also the related bug http://www.jetbrains.net/jira/browse/IDEA-2436 for a more detailed discussion, including a suggestion for some "casting" methods in the annotation library that would make it more explicit when nullability is deliberately added or removed from an expression.


NNTP User - 31 Jul 05 00:22
It looks like/has a lot in common with :
"@Nullable inspection improvement"
http://www.jetbrains.net/jira/browse/IDEA-2051

Alain


Jonas Kvarnstrom - 26 Aug 05 15:12
Any chance of seeing this in an update to IDEA 5.0? It would make @Nullable and @NotNull much more useful and it seems like it wouldn't be too difficult to implement in IDEA, while a plugin would have to re-implement all of your dataflow analysis code in order to avoid too many false positives. If it hadn't been for the dataflow problem, I'd do it myself...

Keith Lea - 26 Aug 05 18:39
Bill Pugh is talking about a @DefaultAnnotation(@NotNull) approach, to annotate a class or package. I think it's too restrictive; I'd rather @DefaultMethodNullness(NOT_NULL) or something, to specify nullness for only methods, and using an enum instead of the annotations themselves.

Gibson - 12 Dec 05 20:03
Will there be any changes on this in 5.1 or Demetra?

Dmitry Jemerov - 12 Dec 05 20:05
In 5.1 - very unlikely. In Demetra - quite possible, but I'm not sure.

NNTP User - 14 Dec 05 19:47
It looks like/has a lot in common with :
"@Nullable inspection improvement"
http://www.jetbrains.net/jira/browse/IDEA-2051

Alain


NNTP User - 27 Dec 06 21:34
It looks like/has a lot in common with :
"@Nullable inspection improvement"
http://www.jetbrains.net/jira/browse/IDEA-2051

Alain


Derek Foster - 28 Dec 06 00:37
Also see IDEABKL-4194.

The current implementation of @NotNull is not reliable, because there is no warning when an unannotated value is assigned to it, so it can't really be trusted.

I would very much love to see some action on this as soon as possible! I'm trying to use @Nullable and @NotNull extensively in a program, and the lack of this feature is causing problems.


Derek Foster - 28 Dec 06 00:40
Also see IDEABKL-3911.

NNTP User - 22 Oct 08 07:09
It looks like/has a lot in common with :
"@Nullable inspection improvement"
http://www.jetbrains.net/jira/browse/IDEA-2051

Alain


NNTP User - 22 Oct 08 07:28
It looks like/has a lot in common with :
"@Nullable inspection improvement"
http://www.jetbrains.net/jira/browse/IDEA-2051

Alain