This Question is Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
17 Replies Last post: Feb 2, 2009 10:51 PM by Den Orlov  
Noam Kfir Newbie 30 posts since
May 29, 2008
Currently Being Moderated

Aug 14, 2008 2:12 AM

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

http://www.intellij.net/forums/thread.jspa?threadID=275483.

 

TIA,

shovavnik

Rob Cannon Newbie 16 posts since
Mar 26, 2007
Currently Being Moderated
Aug 14, 2008 4:30 AM in response to: Noam Kfir
Re: Annotation to prevent "possible null reference exception" warning?

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.

Evgeny Pasynkov JetBrains 6,085 posts since
Dec 3, 2003
Currently Being Moderated
Aug 14, 2008 11:27 AM in response to: Noam Kfir
Re: Annotation to prevent "possible null reference exception" warning?

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)

 

AssertionMethod

public bool

IsNull(AssertionCondition(AssertionConditionType.IS_NOT_NULL)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.

>

The JetBrains.Annotations namespace has an AssertionMethodAttribute and an

AssertionConditionAttribute.

>

I tried decorating my method as follows:

>

>

AssertionMethod

public bool

IsNull(AssertionCondition(AssertionConditionType.IS_NOT_NULL)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?

 

 

 

Evgeny Pasynkov JetBrains 6,085 posts since
Dec 3, 2003
Currently Being Moderated
Aug 14, 2008 7:25 PM in response to: Noam Kfir
Re: Annotation to prevent "possible null reference exception" warning?

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...

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.

 

 

 

Guest
Currently Being Moderated
Aug 16, 2008 6:35 PM in response to: Noam Kfir
Re: Annotation to prevent "possible null reference exception"warning?

Hello,

 

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.

 

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!”

 

 

 

Michael_K Newbie 13 posts since
Jan 25, 2007
Currently Being Moderated
Aug 22, 2008 2:22 PM in response to: Noam Kfir
Re: Annotation to prevent "possible null reference exception" warning?

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

Evgeny Pasynkov JetBrains 6,085 posts since
Dec 3, 2003
Currently Being Moderated
Aug 24, 2008 12:24 PM in response to: Michael_K
Re: Annotation to prevent "possible null reference exception" warning?

 

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...

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:

<Configuration>

<CodeInspection>

   <AssertionMethod MethodName="System.Diagnostics.Debug.Assert"

Parameter="0" Type="IS_TRUE" />

</CodeInspection>

</Configuration>

>

They seem to be gone in v4. What happened to those?

>

Thanks, Michael

 

 

 

Michael_K Newbie 13 posts since
Jan 25, 2007
Currently Being Moderated
Sep 3, 2008 10:34 AM in response to: Evgeny Pasynkov
Re: Annotation to prevent "possible null reference exception" warning?

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

Ilya Ryzhenkov JetBrains 2,698 posts since
Apr 13, 2004
Currently Being Moderated
Sep 3, 2008 1:36 PM in response to: Michael_K
Re: Annotation to prevent "possible null reference exception"warning?

Hello Michael,

 

You can use any number of annotation namespaces simultaneously, but when

you ask ReSharper to put NotNull 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>

 

 

 

Michael_K Newbie 13 posts since
Jan 25, 2007
Currently Being Moderated
Sep 3, 2008 3:26 PM in response to: Ilya Ryzhenkov
Re: Annotation to prevent "possible null reference exception"warning?

Ah yes, I see. Thanks Ilya.

 

Michael

Michael_K Newbie 13 posts since
Jan 25, 2007
Currently Being Moderated
Sep 10, 2008 3:34 PM in response to: Ilya Ryzhenkov
Re: Annotation to prevent "possible null reference exception"warning?

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

Evgeny Pasynkov JetBrains 6,085 posts since
Dec 3, 2003
Currently Being Moderated
Sep 11, 2008 10:55 AM in response to: Michael_K
Re: Annotation to prevent "possible null reference exception"warning?

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...

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

 

 

 

Den  Orlov Newbie 42 posts since
Apr 10, 2003
Currently Being Moderated
Feb 2, 2009 1:23 PM in response to: Evgeny Pasynkov
Re: Annotation to prevent "possible null reference exception" warning?

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?

Evgeny Pasynkov JetBrains 6,085 posts since
Dec 3, 2003
Currently Being Moderated
Feb 2, 2009 9:18 PM in response to: Den Orlov
Re: Annotation to prevent "possible null reference exception" warning?

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...

Java analog of CanBeNull/NotNull attributes could be used to add runtime

checks that will throw NPE in case null will be passed to NotNull

annotated function parameter

(http://blogs.jetbrains.com/yole/archives/000043.html). Is it possible to

add the same for ReSharper null-check annotations?

>

---

Original message URL:

http://www.jetbrains.net/devnet/message/5231168#5231168

 

 

 

Den  Orlov Newbie 42 posts since
Apr 10, 2003
Currently Being Moderated
Feb 2, 2009 10:51 PM in response to: Evgeny Pasynkov
Re: Annotation to prevent "possible null reference exception" warning?

How can I say compiler to add that checks into my code?

More Like This

  • Retrieving data ...