Author: spouliot
Date: 2008-01-12 10:15:03 -0500 (Sat, 12 Jan 2008)
New Revision: 92745

Added:
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Design/AttributeArgumentsShouldHaveAccessorsRule.cs
Modified:
   trunk/cecil/gendarme/rules/Gendarme.Rules.Design/ChangeLog
   trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in
   trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Makefile.am
Log:
2008-01-12  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * AttributeArgumentsShouldHaveAccessorsRule.cs: New. Rule that check 
        that all attribute's constructors have read-only properties (get) that
        match their parameters [Daniel Abramov]
        * Gendarme.Rules.Design.xml.in: Added new rule descriptions.
        * Makefile.am: Add new rules/tests to the build.



Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Design/AttributeArgumentsShouldHaveAccessorsRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Design/AttributeArgumentsShouldHaveAccessorsRule.cs
       2008-01-12 15:13:01 UTC (rev 92744)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Design/AttributeArgumentsShouldHaveAccessorsRule.cs
       2008-01-12 15:15:03 UTC (rev 92745)
@@ -0,0 +1,76 @@
+// 
+// Gendarme.Rules.Design.AttributeArgumentsShouldHaveAccessorsRule
+//
+// Authors:
+//     Daniel Abramov <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2007 Daniel Abramov
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to 
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Collections.Generic;
+
+using Mono.Cecil;
+
+using Gendarme.Framework;
+using Gendarme.Framework.Rocks;
+
+namespace Gendarme.Rules.Design {
+
+       public class AttributeArgumentsShouldHaveAccessorsRule : ITypeRule {
+
+               private List<string> allProperties = new List<string> ();
+
+               public MessageCollection CheckType (TypeDefinition 
typeDefinition, Runner runner)
+               {
+                       // rule applies only to attributes
+                       if (!typeDefinition.IsAttribute ())
+                               return runner.RuleSuccess;
+
+                       // look through getters
+                       allProperties.Clear ();
+                       foreach (PropertyDefinition property in 
typeDefinition.Properties) {
+                               if (property.GetMethod != null) {
+                                       allProperties.Add (property.Name);
+                               }
+                       }
+
+                       Location loc = new Location (typeDefinition);
+                       MessageCollection messages = runner.RuleSuccess;
+
+                       // look through parameters
+                       foreach (MethodDefinition constructor in 
typeDefinition.Constructors) {
+                               foreach (ParameterDefinition param in 
constructor.Parameters) {
+                                       string correspondingPropertyName = 
char.ToUpper (param.Name [0]) + param.Name.Substring (1); // pascal case it
+                                       if (!allProperties.Contains 
(correspondingPropertyName)) {
+                                               Message msg = new Message (
+                                                       string.Format ("All 
parameter values passed to an attribute constructor must be visible through 
read-only properties. Add '{0}' property to the attribute class.", 
correspondingPropertyName),
+                                                                          loc, 
MessageType.Error);
+                                               if (messages == 
runner.RuleSuccess)
+                                                       messages = new 
MessageCollection ();
+                                               messages.Add (msg);
+                                               allProperties.Add 
(correspondingPropertyName); // to avoid double catching same property (e.g. 
from different constructors)
+                                       }
+                               }
+                       }
+                       return messages;
+               }
+       }
+}

Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Design/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Design/ChangeLog  2008-01-12 
15:13:01 UTC (rev 92744)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Design/ChangeLog  2008-01-12 
15:15:03 UTC (rev 92745)
@@ -1,3 +1,11 @@
+2008-01-12  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * AttributeArgumentsShouldHaveAccessorsRule.cs: New. Rule that check 
+       that all attribute's constructors have read-only properties (get) that
+       match their parameters [Daniel Abramov]
+       * Gendarme.Rules.Design.xml.in: Added new rule descriptions.
+       * Makefile.am: Add new rules/tests to the build.
+
 2008-01-05  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * MissingAttributeUsageOnCustomAttributeRule.cs: New. Rule that checks

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in   
    2008-01-12 15:13:01 UTC (rev 92744)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in   
    2008-01-12 15:15:03 UTC (rev 92745)
@@ -5,6 +5,12 @@
                <problem>The class '{0}' provides public 
constructor(s).</problem>
                <solution>Change constructor visibility to protected.</solution>
        </rule>
+       <rule   Name="AttributeArgumentsShouldHaveAccessorsRule"
+               
Type="Gendarme.Rules.Design.AttributeArgumentsShouldHaveAccessorsRule, 
Gendarme.Rules.Design, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
+               Uri="" >
+               <problem>All parameter values passed to '{0}' constructors 
should be visible through read-only properties.</problem>
+               <solution>Add the missing property getters to the 
class.</solution>
+       </rule>
        <rule   Name="AvoidEmptyInterfaceRule"
                Type="Gendarme.Rules.Design.AvoidEmptyInterfaceRule, 
Gendarme.Rules.Design, [EMAIL PROTECTED]@, Culture=neutral, PublicKeyToken=null"
                Uri="" >

Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Makefile.am        
2008-01-12 15:13:01 UTC (rev 92744)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Design/Makefile.am        
2008-01-12 15:15:03 UTC (rev 92745)
@@ -13,7 +13,8 @@
        DefineAZeroValueRule.cs EnumsShouldDefineAZeroValueRule.cs 
FlagsShouldNotDefineAZeroValueRule.cs \
        AbstractTypesShouldNotHavePublicConstructorsRule.cs 
AvoidEmptyInterfaceRule.cs TypesShouldBeInsideNamespacesRule.cs \
        AvoidPropertiesWithoutGetAccessorRule.cs MainShouldNotBePublicRule.cs 
ConsiderConvertingMethodToPropertyRule.cs \
-       AvoidPublicInstanceFieldsRule.cs 
MissingAttributeUsageOnCustomAttributeRule.cs
+       AvoidPublicInstanceFieldsRule.cs 
MissingAttributeUsageOnCustomAttributeRule.cs \
+       AttributeArgumentsShouldHaveAccessorsRule.cs
 
 design_rules_build_sources = $(addprefix $(srcdir)/, $(design_rules_sources))
 design_rules_build_sources += $(design_rules_generated_sources)
@@ -40,7 +41,8 @@
        EnumsShouldDefineAZeroValueTest.cs 
FlagsShouldNotDefineAZeroValueTest.cs \
        AbstractTypesShouldNotHavePublicConstructorsTest.cs 
AvoidEmptyInterfaceTest.cs TypesShouldBeInsideNamespacesTest.cs \
        AvoidPropertiesWithoutGetAccessorTest.cs MainShouldNotBePublicTest.cs 
ConsiderConvertingMethodToPropertyTest.cs \
-       AvoidPublicInstanceFieldsTest.cs 
MissingAttributeUsageOnCustomAttributeTest.cs
+       AvoidPublicInstanceFieldsTest.cs 
MissingAttributeUsageOnCustomAttributeTest.cs \
+       AttributeArgumentsShouldHaveAccessorsTest.cs
 
 design_test_build_sources = $(addprefix $(srcdir)/Test/, 
$(design_test_sources))
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to