Code Annotations configuration

I love R# 4, but I must say that the configuration of Code Annotations has become vastly more difficult. I just gave up 'telling' R# about MbUnit's Assert methods as I simply couldn't figure out how to do it.

In version 3 it was a breeze. You could do it in one minute.

As I could find nothing in the Settings dialog that would let me do this, and I ended up creating an XML file in the ExternalAnnotations folder. Didn't work...

I think it is such a shame that Annotations have become next to impossible to set up in version 4. I really hope that it will be improved in later versions as it is simply a killer feature once it has been set up.

- Ole

14 comments
Comment actions Permalink

I think it is such a shame that Annotations have become next to
impossible to set up in version 4. I really hope that it will be
improved in later versions as it is simply a killer feature once it
has been set up.


Can you describe the feature a bit, please? It sounds interesting, but I
haven't heard about it before.


0
Comment actions Permalink

Ole,

That's two of us who'd like to know more. An explanation is now due. :)

0
Comment actions Permalink

Hello guys,

Ole is talking about what was Value Analysis in Resharper 3.

--Jakob

Ole,

That's two of us who'd like to know more. An explanation is now due.
:)



0
Comment actions Permalink

I never used that, unless I was riding a horse by another name! :)

0
Comment actions Permalink

Hello Peter Hamilton-Scott,

Wow - you should - it is one of the coolest and most useful features.

Take a look at: http://www.jetbrains.com/resharper/features/code_analysis.html#Value_Analysis

It has saved me many times, and has improved our code quality immensly.

The Agent Johnson plugin was originally created to help us use Value Analysis
and today makes it a breeze.

--Jakob

I never used that, unless I was riding a horse by another name! :)



0
Comment actions Permalink

Jakob,

I looked at the link you provided. I have come to rely on value analysis but I can't quite see the relevance of code annotations. Reading through the link, I can appreciate the additional parsing that RS does on string.format as that has helped me immensely in the past. The bit I can't quite appreciate is what exactly does including the clipboard text in your solution actually give you? What value does it add to a solution? As I say, I appreciate the string.format usage but I can't really see the knock-out punch that would convince me to make more use of the rest of it. Perhaps I don't move around enough in c# to the extent where it would poke me in the eye and cause me to ask myself "how did I ever live without it?" It's a brilliant solution, I'm sure, but I can't quite see what the problem is that it solves. :)

If I disable JetBrains.Annotations in RS -> Options -> Code Inspection -> Code Annotations and then bounce my solution, I lose the string.format syntax checking. What puzzles me, is that the namespaces display area is awfully large given that it only has one namespace listed. Do I read that as implying you could add others? I think I'm closer to understanding more what code annotations can do but there's obviously more to it than the little I've unravelled.

0
Comment actions Permalink

Hello,

The bit I can't quite
appreciate is what exactly does including the clipboard text in your
solution actually give you? What value does it add to a solution.


As you have the attributes in your code, you could mark your methods and
properties with NotNull / CanBeNull. This way, assigning a return value from
a CanBeNull method to a NotNull property will render a warning that you're
getting a potential NullReference / ArgumentNull exception with that code.
Put aside the warning, having a formal nullity annotations on core classes
saves much time of guessing whether they will return Null or throw if something
is missing inside.

That's the most powerful part of it, as of now. Probably, exception annotations
(Java-like) and const annotations (C++-like) will be coming any soon.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0
Comment actions Permalink

Hello,

I love R# 4, but I must say that the configuration of Code Annotations
has become vastly more difficult. I just gave up 'telling' R# about
MbUnit's Assert methods as I simply couldn't figure out how to do it.


You're right. Pity, but there's no way to do it. R#4 is focused on annotating
own code and provides some pre-built annotations of .NET Framework code.
Manually editing annotations for some other compiled code is not a focus
of this release.

The only way is to mock an external annotations XML file.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0
Comment actions Permalink

Hello Peter Hamilton-Scott,

Let me give you an example of what I think is cool:

Consider:


public string GetValue() {
...
}

public void PrintValue( text) {
...
}

public Foo() {
string text = GetValue();
PrintValue(text);
}

Resharper will highlight the text parameter in PrintValue() call as a possible
null reference as text can be null. Text can be null as GetValue is marked
with CanBeNull, but the text parameter in PrintValue cannot be null as it
is marked with NotNull. If for some reason GetValue returns null, you will
probably get a "Object is not set to an instance of an object" exception
in PrintValue.

This is a very common error, and you usually only find it at runtime.

So this helps you build robust code.

--Jakob

Jakob,

I looked at the link you provided. Reading through it, I can
appreciate the additional parsing that RS does on string.format as
that has helped me immensely in the past. The bit I can't quite
appreciate is what exactly does including the clipboard text in your
solution actually give you? What value does it add to a solution. As I
say, I appreciate the string.format usage but I can't really see the
knock-out punch that would convince me to use more of the rest of it.
Perhaps I don't move around c# to the extent where it would poke me in
the eye and cause me to ask myself "how did I ever live without it?"
It's a brilliant solution, I'm sure, but I can't quite see what the
problem is that it solves. :)



0
Comment actions Permalink

Hello Serge,

Actually Ole and I were wondering, if Resharper could create a file containing
the attributes in the solution automatically.

The way to enable Code Annotations in a project would simply be:

1) Open solution
2) Open Resharper Options / Code Annotations
3) Click a button named something like "Create Code Annotations Attributes".
4) Resharper asks for a namespace which the user supplies.
5) Resharper creates the file and adds it the the solution.

And you are ready to go.

Could even be implemented using File Templates.

--Jakob

Hello,

>> I love R# 4, but I must say that the configuration of Code
>> Annotations has become vastly more difficult. I just gave up
>> 'telling' R# about MbUnit's Assert methods as I simply couldn't
>> figure out how to do it.
>>

You're right. Pity, but there's no way to do it. R#4 is focused on
annotating own code and provides some pre-built annotations of .NET
Framework code. Manually editing annotations for some other compiled
code is not a focus of this release.

The only way is to mock an external annotations XML file.

-
Serge Baltic
JetBrains, Inc - http://www.jetbrains.com
"Develop with pleasure!"



0
Comment actions Permalink

Guys, I did this:

1. I copied the JetBrains.Annnotations to the clipboard.
2. I added a new class to my solution and pasted the code from 1 into it.
3. I added this method to my main program .cs file:

OPEN SQUARE BRACKET JetBrains.Annotations.NotNull CLOSE SQUARE BRACKET
public string GetValue()
{
return null;
}

4. RS highlights the return null statement with a blue squiggly and a tooltip message "Possibly null assignment to entity marked with 'NotNull' attribute". I'm very happy with that.
5. If I delete the new class (2 above) from my solution, the JetBrains.Annotations.NotNull statement in 3 above is highlighted as an error.

Q: I must copy the code from the code annotations option and create a new source file (2 above) before I can use the items it contains?

Q: Does this mean I could create some annotations of my own and link them into RS to perform other validations of my invention? If so, does anyone have a practical example of one?

0
Comment actions Permalink

Hello Peter Hamilton-Scott,

Q1) Yes - you need the Attribute definitions. Either you define your own
(just like you have done) or you reference the JetBrains.Annotations.dll
assembly. If you define your own, you can change the namespace, but keep
the name of the attribute.

Q2) If I understand you correctly - no. Resharper only reacts to the these
wellknown attributes: StringFormatMethod, InvokerParameterName, AssertionMethod,
AssertionCondition, TerminatesProgram, CanBeNull, NotNull, CannotApplyEquality
and BaseTypeRequired. Here is an example that use some of the attributes:

This is an assertion method that asserts that a parameter is not null.


public static void ArgumentNotNull(
object argument, string argumentName) {
if(argument != null){
return;
}
if(argumentName != null){
throw new ArgumentNullException(argumentName);
}

throw new ArgumentNullException();
}


I do not know if these attributes are documented anywhere.

--Jakob

Guys, I did this:

1. I copied the JetBrains.Annnotations to the clipboard.
2. I added a new class to my solution and pasted the code from 1 into
it.
3. I added this method to my main program .cs file:

public string GetValue()
{
return null;
}
4. RS highlights the return null statement with a blue squiggly and a
tooltip message "Possibly null assignment to entity marked with
'NotNull' attribute". I'm very happy with that.

5. If I delete the new class (2 above) from my solution, the
statement in 3 above is highlighted as
an error.

Q: I must copy the code from the code annotation option and create a
new source file (2 above) before I can use the items it contains.

Q: Does this mean I could create some annotations of my own and link
them into RS to perform other validations of my invention? If so, does
anyone have a practical example of one?



0
Comment actions Permalink

Hello,

Actually Ole and I were wondering, if Resharper could create a file
containing the attributes in the solution automatically.


I were replying to the statement that it's now hard to mark some other DLL
(namely, MBUnit) with R# annotations. I suppose you do not have access to
source code in this case, that's why it's a problem — annotating an already-compiled
member.

As for adding the attribute declarations to a solution of your own, then
yes, a new code file should be created in the location of your choice and
the R#-provided declarations code should be pasted into it.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0
Comment actions Permalink

You can modify the namespace where these attributes are located.
Once you get your own copy of attribute source, just alter the namespace as
you wish and put this source into your project.
Then visit the options once more - and ReSharepr will detect these new
attributes, and check the box in the list

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Jakob Christensen" <jc@sitecore.net> wrote in message
news:b5a5f1bac4e8caa9b092dd8429@news.jetbrains.com...

Hello Peter Hamilton-Scott,

>

Q1) Yes - you need the Attribute definitions. Either you define your own
(just like you have done) or you reference the JetBrains.Annotations.dll
assembly. If you define your own, you can change the namespace, but keep
the name of the attribute.

>

Q2) If I understand you correctly - no. Resharper only reacts to the these
wellknown attributes: StringFormatMethod, InvokerParameterName,
AssertionMethod, AssertionCondition, TerminatesProgram, CanBeNull,
NotNull, CannotApplyEquality and BaseTypeRequired. Here is an example that
use some of the attributes:

>

This is an assertion method that asserts that a parameter is not null.

>


public static void ArgumentNotNull(
object argument,
string argumentName) {
if(argument != null){
return;
}
if(argumentName != null){
throw new ArgumentNullException(argumentName);
}

>

throw new ArgumentNullException();
}

>
>

I do not know if these attributes are documented anywhere.

>

--Jakob

>
>
>> Guys, I did this:
>>
>> 1. I copied the JetBrains.Annnotations to the clipboard.
>> 2. I added a new class to my solution and pasted the code from 1 into
>> it.
>> 3. I added this method to my main program .cs file:
>>
>> public string GetValue()
>> {
>> return null;
>> }
>> 4. RS highlights the return null statement with a blue squiggly and a
>> tooltip message "Possibly null assignment to entity marked with
>> 'NotNull' attribute". I'm very happy with that.
>>
>> 5. If I delete the new class (2 above) from my solution, the
>> statement in 3 above is highlighted as
>> an error.
>>
>> Q: I must copy the code from the code annotation option and create a
>> new source file (2 above) before I can use the items it contains.
>>
>> Q: Does this mean I could create some annotations of my own and link
>> them into RS to perform other validations of my invention? If so, does
>> anyone have a practical example of one?
>>
>



0

Please sign in to leave a comment.