Annotation to prevent "possible null reference exception" warning?
I'm trying to figure out how to annotate a null checking method so code that follows it is not marked as a possible null reference exception.
For example:
public bool IsNull(object value)
{
return value == null;
}
public void DoSomething()
{
object someValue = null;
if(IsNull(someValue))
Console.WriteLine("someValue is null");
else
Console.WriteLine("someValue = " + someValue.ToString());
}
Today, ReSharper marks "someValue" in the else block as a possible null reference exception even though it can logically never be null.
Is it possible to annotate the IsNull method somehow to prevent this?
Is there an alternate solution?
I posted a similar question in May that went unanswered at
.
TIA,
shovavnik
Please sign in to leave a comment.
There used to be a options tab where you could enter functions that validate nulls. It was useful for registering the Assert.IsNull type functions that check for nulls before use of a reference. But it seems to be missing now. Sorry, not too much help.
I found something that might be the answer, but I can't get it to work.
The JetBrains.Annotations namespace has an AssertionMethodAttribute and an AssertionConditionAttribute.
I tried decorating my method as follows:
public bool IsNull(object value)
{
return value == null || value.ToString().Trim().Length == 0;
}
However, ReSharper doesn't seem to be affected by this. I tried using the JetBrains.Annotations namespace directly as well as copying to clipboard and using a local namespace. Neither had any effect.
Is this the right direction? Am I missing something simple here?
What are these attributes? Have you referenced JetBrains.Annotations.dll?
And the "Assertion" family has other semantics - when the chack failed, it
should stop normal control flow (say, throw exception)
public bool
IsNull(object value)
{
if (value == null || value.ToString().Trim().Length == 0) throw new
InvalidOperationException();
}
Unfortunately, custom check functions are not yet supported. (But is
planned)
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Noam Rettig" <no_reply@jetbrains.com> wrote in message
news:28526302.27361218676684594.JavaMail.jive@app4.labs.intellij.net...
>I found something that might be the answer, but I can't get it to work.
>
>
>
>
>
>
>
>
Out of curiosity, how did you decorate the Framework's String.IsNullOrEmpty method.
Clearly ReSharper is doing something here, as evidenced by these examples. Is it possible to apply this behavior to our methods?
That is:
string value = null;
if(string.IsNullOrEmpty(value))
Console.WriteLine( value ); // ReSharper shows a blue squiggly under value.
value = "some string";
if(string.IsNullOrEmpty(value))
Console.WriteLine(value); // ReSharper DOES NOT show a blue squiggly under value.
value = someObject as string;
if(string.IsNullOrEmpty(value))
Console.WriteLine( value ); // ReSharper shows a blue squiggly under value.
value = null;
if( !string.IsNullOrEmpty(value) ) // ReSharper shows a squggly under the expression: "Expression is always false"
Console.WriteLine(value); // ReSharper DOES NOT show a blue squiggly under value.
string.IsNullOrEmpty is hard-coded right now. This is to be refactored in
the future
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Noam Rettig" <no_reply@jetbrains.com> wrote in message
news:24669582.29371218715075730.JavaMail.jive@app4.labs.intellij.net...
>
>
>
>
>
>
When you refactor this code, can I suggest having ReSharper analyze the deep logical tree (or full stack trace) instead of annotating? I think this would remove the need to annotate.
I don't know how this would affect performance, how recursive calls would work, or what would happen in other unclear or complex situations, but maybe this suggestion can apply in certain simple cases (like non-recursive non-virtual methods with a limited depth...).
Does this suggestion have any merit?
For example, if I have:
public bool IsNull(object value) { return value == null; }
and then a call:
if(!IsNull(someObject)) DoSomethingWith(someObject);
This translates logically to:
if(!(someObject == null)) DoSomethingWith(someObject);
(There's probably even a good chance this kind of call would be inlined in many cases.)
Hello,
Our External Annotations (for the system DLLs) are the result of analyzing
the deep logical tree, in fact. Written down, so that applying them takes
some reasonable amount of time. We'll probably enable the same technique
for source code in a future release. This way, methods that are clear about
their nullity will get the annotations automatically.
—
Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”
Hello
In Resharper v3 there had been a list of methods and evaluations that would perform Null-Checks. They had been persisted in the resharper-file like this:
]]>
They seem to be gone in v4. What happened to those?
Thanks, Michael
This functionality is converted to so called "code annotations".
You have to either add attributes to source code, either annotate .dll with
"external annotations"
Please let me know if you need detailed instructions
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Michael K" <no_reply@jetbrains.com> wrote in message
news:3122838.59151219400582791.JavaMail.jive@app4.labs.intellij.net...
>
>
>
Thanks Eugene!
I think I just figured it out by trying out the "copy default implementation to clipboard"-feature. This means I do not have to link to the JetBrains.Annotation assembly, which is preferable since we're building a framework and each external reference adds to the complexity of different projects using different versions of a third party component.
One question is left: What exactly is the purpose of the "default annotation namespace", i.e. can I use two annotation-implementations simultaneously?
Michael
Hello Michael,
You can use any number of annotation namespaces simultaneously, but when
you ask ReSharper to put into code, it have to select one. This
is exactly what "default namespace" is for. In other words, ReSharper listens
to all namespaces, but speaks in default.
Sincerely,
Ilya Ryzhenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
MK> Thanks Eugene!
MK>
MK> I think I just figured it out by trying out the "copy default
MK> implementation to clipboard"-feature. This means I do not have to
MK> link to the JetBrains.Annotation assembly, which is preferable since
MK> we're building a framework and each external reference adds to the
MK> complexity of different projects using different versions of a third
MK> party component.
MK>
MK> One question is left: What exactly is the purpose of the "default
MK> annotation namespace", i.e. can I use two annotation-implementations
MK> simultaneously?
MK>
MK> Michael
MK>
Ah yes, I see. Thanks Ilya.
Michael
Hello Ilya!
Another issue popped up. In nunit (and other testing frameworks, third party components) are a bunch up methods that do checking for a value being not null. Is it possible to annotate them as well like you did with the .net Framework (e.g. Debug.Assert (myValue != null) ) without editing the source code of those third party components?
Thanks, Michael
The NUnit assertion methods are already marked with the external annotations
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Michael K" <no_reply@jetbrains.com> wrote in message
news:17935416.127881221046537173.JavaMail.jive@app4.labs.intellij.net...
>
>
Java analog of CanBeNull/NotNull attributes could be used to add runtime checks that throw NPE in case null will be passed to NotNull annotated function parameter (http://blogs.jetbrains.com/yole/archives/000043.htm). Is it possible to add the same for ReSharper null-check attributes?
They are already here!
--
Eugene Pasynkov
ReSharper Technical Lead
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"
"Den Orlov" <no_reply@jetbrains.com> wrote in message
news:27681671.3101233568699173.JavaMail.clearspace@app4.labs.intellij.net...
>
How can I say compiler to add that checks into my code?