{"id":26,"date":"2014-03-02T16:39:00","date_gmt":"2014-03-02T16:39:00","guid":{"rendered":"http:\/\/tpodolak.com.hostingasp.pl\/blog\/2014\/03\/02\/resharper-annotation-attributes\/"},"modified":"2016-01-30T23:38:53","modified_gmt":"2016-01-30T23:38:53","slug":"resharper-annotation-attributes","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2014\/03\/02\/resharper-annotation-attributes\/","title":{"rendered":"Resharper &#8211; annotation attributes"},"content":{"rendered":"<h3>1. Introduction<\/b><\/h3>\n<p>I think one of the most important features of Resharper is on-the-fly code quality analysis. Basically, every time Resharper finds possible pitfall in code, it indicates this with appropriate warning. The most common warning is <i>&#8220;Possible System.NullReferenceException&#8221;<\/i>. You can easily reproduce it by writing this code<\/p>\n<pre lang=\"csharp\">\r\nList<object> objlList = new List<object>( );\r\nvar result = objlList.SingleOrDefault( );\r\nConsole.WriteLine( result.GetHashCode( ) );\r\n<\/pre>\n<p><a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/singledefaultwarning.png\" rel=\"attachment wp-att-332\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/singledefaultwarning.png\" alt=\"singledefaultwarning\" width=\"450\" class=\"aligncenter size-full wp-image-332\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/singledefaultwarning.png 443w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/singledefaultwarning-150x56.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/singledefaultwarning-300x112.png 300w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><\/a><br \/>\nAs you can see R# is able to figure out, that LINQ <i>SingleOrDefault<\/i> function can return null. However there is no warning if we try to do something like that<\/p>\n<pre lang=\"csharp\">\r\nprivate static void DisplayHashCode()\r\n{\r\n    var result = Foo();\r\n    Console.WriteLine( result.GetHashCode( ) );\r\n}\r\n\r\nprivate static object Foo()\r\n{\r\n    return null;\r\n}\r\n<\/pre>\n<p>So it got me thinking, how Resharper recognizes whether or whether not to show warnings. It turned out, that it uses annotation attributes, which describe behavior of functions, properties, classes and interfaces.<\/p>\n<h3>2. Source code annotations<\/h3>\n<p>In order to use these attributes we have to place them in our project. First of all, let&#8217;s create Annotation.cs file and then copy default implementation of annotations into this file. The default implementation can be found in <i>Code annotation<\/i> option (Resharper->Options->Code Inspections->Code Annotations) under the button <i>&#8220;Copy default implementation to clipboard&#8221;<\/i>.<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/resharperOptions.png\" rel=\"attachment wp-att-331\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/resharperOptions.png\" alt=\"resharperOptions\" width=\"600\" class=\"aligncenter size-full wp-image-331\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/resharperOptions.png 864w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/resharperOptions-150x56.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/resharperOptions-300x112.png 300w\" sizes=\"(max-width: 864px) 100vw, 864px\" \/><\/a><br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotationoptions.png\" rel=\"attachment wp-att-323\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotationoptions.png\" alt=\"annotationoptions\" width=\"600\" class=\"aligncenter size-full wp-image-323\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotationoptions.png 1022w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotationoptions-150x108.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotationoptions-300x217.png 300w\" sizes=\"(max-width: 1022px) 100vw, 1022px\" \/><\/a><br \/>\nFrom now on we can decorate our functions with e.g. <i>CanBeNull<\/i> attribute<\/p>\n<pre lang=\"csharp\">\r\nprivate static void DisplayHashCode()\r\n{\r\n    var result = Foo();\r\n    Console.WriteLine( result.GetHashCode( ) );\r\n}\r\n       \r\n[CanBeNull]\r\nprivate static object Foo()\r\n{\r\n    return null;\r\n}\r\n<\/pre>\n<p>and we will get warning if necessary<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/after.png\" rel=\"attachment wp-att-322\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/after.png\" alt=\"annotation attributes\" width=\"450\" class=\"aligncenter size-full wp-image-322\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/after.png 422w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/after-150x80.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/after-300x160.png 300w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><\/a><br \/>\nOf course there are a lot more of attributes to use. My favorites are:<\/p>\n<ul>\n<li>StringFormatMethodAttribute<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/format.png\" rel=\"attachment wp-att-327\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/format.png\" alt=\"annotation attributes\" width=\"450\" class=\"aligncenter size-full wp-image-327\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/format.png 521w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/format-150x69.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/format-300x138.png 300w\" sizes=\"(max-width: 521px) 100vw, 521px\" \/><\/a><\/p>\n<li>PathReferenceAttribute<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/path.png\" rel=\"attachment wp-att-330\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/path.png\" alt=\"annotation attributes\" width=\"450\" class=\"aligncenter size-full wp-image-330\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/path.png 672w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/path-150x87.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/path-300x173.png 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/a><\/p>\n<li>NotifyPropertyChangedInvocatorAttribute<\/li>\n<p><a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/notify.png\" rel=\"attachment wp-att-329\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/notify.png\" alt=\"annotation attributes\" width=\"450\" class=\"aligncenter size-full wp-image-329\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/notify.png 571w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/notify-150x76.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/notify-300x152.png 300w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><\/a>\n<\/ul>\n<h3>3. External code annotations <\/h3>\n<p>In case we do not have access to source code, we still are able to use annotation attributes. However we have to define annotations in xml configuration file. Let&#8217;s say we want to decorate NHibernte&#8217;s SaveOrUpdate function with NotNullAttribute. First of all we have to create xml file with appropriate declaration (This is standard XML Document Comment file. You can read specification <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa288481(v=vs.71).aspx\">here<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/fsbx0t7x.aspx\">here<\/a>.)<\/p>\n<pre lang=\"csharp\">\r\n<assembly name=\"NHibernate\">\r\n    <member name=\"M:NHibernate.ISession.SaveOrUpdate(System.Object)\">\r\n        <parameter name=\"obj\">\r\n            <attribute ctor=\"M:JetBrains.Annotations.NotNullAttribute.#ctor\" \/>\r\n        <\/parameter>\r\n    <\/member>\r\n<\/assembly>\r\n<\/pre>\n<p>Having prepared configuration file, we need to place it in appropriate location in order for Resharper to use it. It can be stored in<\/p>\n<ul>\n<li>[ReSharper install directory]\\Bin\\ExternalAnnotations\\[Assembly name].xml<\/li>\n<li>[ReSharper install directory]\\Bin\\ExternalAnnotations\\[Assembly name][Any name].xml.<\/li>\n<\/ul>\n<p>So in my case the location is<\/p>\n<pre lang=\"bash\">\r\nC:\\Program Files (x86)\\JetBrains\\ReSharper\\v8.1\\Bin\\ExternalAnnotations\\NHibernateNHibernate.xml\r\n<\/pre>\n<p>After restarting Visual Studio, Resharper should load the file and show warning messages.<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/nhibernate.png\" rel=\"attachment wp-att-328\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/nhibernate.png\" alt=\"nhibernate\" width=\"450\" class=\"aligncenter size-full wp-image-328\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/nhibernate.png 633w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/nhibernate-150x38.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/nhibernate-300x77.png 300w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><\/a><\/p>\n<h3>4. Generating initial XML annotation files<\/h3>\n<p>Creating xml annotation file for large library is very tedious task, fortunately Resharper have a hidden feature which can help us with generating initial xml file. First of all we have to run Visual Studio with <i>\/ReSharper.Internal<\/i> parameter. In this mode we have an access to undocumented\/hidden features of Resharper. Now, in the <i>&#8220;Resharper&#8221;<\/i> menu there is one additional menu item &#8211; <i>Internal<\/i>, where You can find Annotator submenu in which resides Annotate item.<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotator.png\" rel=\"attachment wp-att-324\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotator.png\" alt=\"annotator\" width=\"450\" class=\"aligncenter size-full wp-image-324\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotator.png 945w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotator-150x76.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotator-300x151.png 300w\" sizes=\"(max-width: 945px) 100vw, 945px\" \/><\/a><br \/>\nNow You can specify for which dll Resharper should generate xml annotation file.<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorwindow.png\" rel=\"attachment wp-att-326\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorwindow.png\" alt=\"annotatorwindow\" width=\"450\" class=\"aligncenter size-full wp-image-326\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorwindow.png 711w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorwindow-150x126.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorwindow-300x252.png 300w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/a><br \/>\nand after a while You should get complete xml declaration.<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorprogress.png\" rel=\"attachment wp-att-325\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorprogress.png\" alt=\"annotatorprogress\" width=\"450\" class=\"aligncenter size-full wp-image-325\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorprogress.png 409w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorprogress-150x73.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/03\/resharper-annotation-attributes\/annotatorprogress-300x147.png 300w\" sizes=\"(max-width: 409px) 100vw, 409px\" \/><\/a><br \/>\n<b>Please bear in mind, that Annotator is not official R# feature, so there is no guarantee that generating annotation files will succeed<\/b>. Source code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/ResharperAnnotations\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction I think one of the most important features of Resharper is on-the-fly code quality analysis. Basically, every time Resharper finds possible pitfall in code, it indicates this with appropriate warning. The most common warning is &#8220;Possible System.NullReferenceException&#8221;. You can easily reproduce it by writing this code List objlList = new List( ); var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54,3,53,50],"tags":[200,159,198,199],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-attributes","category-c","category-r","category-resharper","tag-attributes","tag-c","tag-r","tag-resharper"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":4,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":535,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions\/535"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}