Author: nestor
Date: 2008-02-07 11:04:56 -0500 (Thu, 07 Feb 2008)
New Revision: 95150

Modified:
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/ChangeLog
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/ChangeLog
Log:
2008-02-07  Nestor Salceda  <[EMAIL PROTECTED]>
        
        * AvoidLongMethodsRule.cs: The rule should be applied to static
        constructors.  Now we add several extra instructions for
        each field for initialize, specially if the field is an array.

        * Test/AvoidLongMethodsTest.cs: Added a test for the new strategy for
        static constructors.



Modified: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs   
    2008-02-07 15:55:30 UTC (rev 95149)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs   
    2008-02-07 16:04:56 UTC (rev 95150)
@@ -39,6 +39,8 @@
 
        public class AvoidLongMethodsRule : IMethodRule {
                private int maxInstructions = 170;
+               const int AsignationRatio = 7;
+               const int DefaultAmountOfElements = 13;
                static Hashtable typeMethodDictionary;
 
                static AvoidLongMethodsRule ()
@@ -77,6 +79,42 @@
                        return method.Name == MethodDefinition.Cctor;
                }
 
+               private static bool IsConstructor (MethodDefinition method)
+               {
+                       return method.Name == MethodDefinition.Ctor;
+               }
+
+               private static int CountStaticFields (TypeDefinition type) {
+                       int counter = 0;
+                       foreach (FieldDefinition field in type.Fields) {
+                               if (field.IsStatic || field.IsInitOnly || 
field.HasConstant)
+                                       counter++;
+                               //if the field is an array, we should take care
+                               //about their elements.
+                               ArrayType array = field.FieldType as ArrayType;
+                               if (array != null) {
+                                       for (int index = 0; index < 
array.Dimensions.Count; index++)  
+                                               //I can't calculate the array
+                                               //length, then, i add a
+                                               //default amount of elements 
+                                               //TODO: Perhaps we can do other
+                                               //approach with random nunbers?
+                                               counter+= 
DefaultAmountOfElements;
+                               }
+                       }
+                       return counter;
+               }
+
+               private static int CountInstanceFields (TypeDefinition type) {
+                       int counter = 0;
+                       foreach (FieldDefinition field in type.Fields) {
+                               if (!(field.IsStatic || field.IsInitOnly || 
field.HasConstant))
+                                       counter++;
+                               //I not take care about arrays here.
+                       }
+                       return counter;
+               }
+
                public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
                {
                        // rule does not apply if method as no code (e.g. 
abstract, p/invoke)
@@ -92,16 +130,20 @@
                        if (IsAutogeneratedByTools (method))
                                return runner.RuleSuccess;
                        
-                       // Extracting static methods from static constructors 
really
-                       // makes sense?         
-                       if (IsStaticConstructor (method))
-                               return runner.RuleSuccess;
-
                        // rule applies!
-
                        // success if the instruction count is below the 
defined threshold
-                       if (method.Body.Instructions.Count <= MaxInstructions)
-                               return runner.RuleSuccess;
+                       if (IsStaticConstructor (method)) {
+                               if (method.Body.Instructions.Count <= 
MaxInstructions + (CountStaticFields (method.DeclaringType as TypeDefinition) * 
AsignationRatio))
+                                       return runner.RuleSuccess;
+                       }
+                       else if (IsConstructor (method)) {
+                               if (method.Body.Instructions.Count <= 
MaxInstructions + (CountInstanceFields (method.DeclaringType as TypeDefinition) 
* AsignationRatio))
+                                       return runner.RuleSuccess;
+                       }
+                       else {
+                               if (method.Body.Instructions.Count <= 
MaxInstructions)
+                                       return runner.RuleSuccess;
+                       }
 
                        Location location = new Location (method);
                        Message message = new Message ("The method is too 
long.", location, MessageType.Error);

Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/ChangeLog     
2008-02-07 15:55:30 UTC (rev 95149)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/ChangeLog     
2008-02-07 16:04:56 UTC (rev 95150)
@@ -1,6 +1,12 @@
+2008-02-07  Nestor Salceda  <[EMAIL PROTECTED]>
+       
+       * AvoidLongMethodsRule.cs: The rule should be applied to static
+       constructors.  Now we add several extra instructions for each field for
+       initialize, specially if the field is an array.
+
 2008-02-06  Nestor Salceda  <[EMAIL PROTECTED]>
        
-       * AvoidLongMethods.cs: The rule shouldn't be applied to static
+       * AvoidLongMethodsRule.cs: The rule shouldn't be applied to static
        constructors.
 
 2008-01-16  Nestor Salceda  <[EMAIL PROTECTED]>

Modified: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs
  2008-02-07 15:55:30 UTC (rev 95149)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs
  2008-02-07 16:04:56 UTC (rev 95150)
@@ -51,10 +51,16 @@
 }
 
 namespace Test.Rules.Smells {
+       public class LongStaticConstructorWithFields {
+               static readonly int foo;
+               static string bar;
+               static object baz;
 
-       public class LongStaticConstructor {
-               static LongStaticConstructor () {
-                               Console.WriteLine ("I'm writting a test, and I 
will fill a screen with some useless code");
+               static LongStaticConstructorWithFields () {
+                       foo = 5;
+                       bar = "MyString";
+                       baz = new object ();
+                       Console.WriteLine ("I'm writting a test, and I will 
fill a screen with some useless code");
                        IList list = new ArrayList ();
                        list.Add ("Foo");
                        list.Add (4);
@@ -124,6 +130,78 @@
                }
        }
 
+       public class LongStaticConstructorWithoutFields {
+               static LongStaticConstructorWithoutFields () {
+                       Console.WriteLine ("I'm writting a test, and I will 
fill a screen with some useless code");
+                       IList list = new ArrayList ();
+                       list.Add ("Foo");
+                       list.Add (4);
+                       list.Add (6);
+
+                       IEnumerator listEnumerator = list.GetEnumerator ();
+                       while (listEnumerator.MoveNext ())
+                               Console.WriteLine (listEnumerator.Current);
+
+                       try {
+                               list.Add ("Bar");
+                               list.Add ('a');
+                       }
+                       catch (NotSupportedException exception) {
+                               Console.WriteLine (exception.Message);
+                               Console.WriteLine (exception);
+                       }
+
+                       foreach (object value in list) {
+                               Console.Write (value);
+                               Console.Write (Environment.NewLine);
+                       }
+                       
+                       int x = 0;
+
+                       for (int i = 0; i < 100; i++)
+                               x++;
+                       Console.WriteLine (x);
+       
+                       string useless = "Useless String";
+
+                       if (useless.Equals ("Other useless")) {
+                               useless = String.Empty;
+                               Console.WriteLine ("Other useless string");
+                       }
+                       
+                       useless = String.Concat (useless," 1");
+                       
+                       for (int j = 0; j < useless.Length; j++) {
+                               if (useless[j] == 'u')
+                                       Console.WriteLine ("I have detected an 
u char");
+                               else
+                                       Console.WriteLine ("I have detected an 
useless char");
+                       }
+                       
+                       try {
+                               foreach (string environmentVariable in 
Environment.GetEnvironmentVariables ().Keys)
+                                       Console.WriteLine (environmentVariable);
+                       }
+                       catch (System.Security.SecurityException exception) {
+                               Console.WriteLine (exception.Message);
+                               Console.WriteLine (exception);
+                       }
+
+                       Console.WriteLine ("I will add more useless code !!");
+                       
+                       try {
+                               if (!(File.Exists ("foo.txt"))) {
+                                       File.Create ("foo.txt");        
+                                       File.Delete ("foo.txt");
+                               }
+                       }
+                       catch (IOException exception) {
+                               Console.WriteLine (exception.Message);
+                               Console.WriteLine (exception);
+                       }
+               }
+       }
+
        public class MainWidget : Gtk.Bin {
                protected virtual void Build () 
                {
@@ -1077,10 +1155,20 @@
                }
 
                [Test]
-               public void LongStaticConstructorTest () 
+               public void LongStaticConstructorWithoutFieldsTest () 
                {
-                       MethodDefinition staticConstructor = 
assembly.MainModule.Types["Test.Rules.Smells.LongStaticConstructor"].Constructors.GetConstructor
 (true,Type.EmptyTypes);
+                       MethodDefinition staticConstructor = 
assembly.MainModule.Types["Test.Rules.Smells.LongStaticConstructorWithoutFields"].Constructors.GetConstructor
 (true,Type.EmptyTypes);
                        messageCollection = rule.CheckMethod 
(staticConstructor, new MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (1, messageCollection.Count);
+               }
+
+               
+               [Test]
+               public void LongStaticConstructorWithFieldsTest () 
+               {
+                       MethodDefinition staticConstructor = 
assembly.MainModule.Types["Test.Rules.Smells.LongStaticConstructorWithFields"].Constructors.GetConstructor
 (true,Type.EmptyTypes);
+                       messageCollection = rule.CheckMethod 
(staticConstructor, new MinimalRunner ());
                        Assert.IsNull (messageCollection);
                }
        }

Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/ChangeLog        
2008-02-07 15:55:30 UTC (rev 95149)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/ChangeLog        
2008-02-07 16:04:56 UTC (rev 95150)
@@ -1,3 +1,8 @@
+2008-02-07  Nestor Salceda  <[EMAIL PROTECTED]>
+       
+       * AvoidLongMethodsTest.cs: Added a test for the new strategy for static
+       constructors.
+
 2008-02-06  Nestor Salceda  <[EMAIL PROTECTED]>
 
        * AvoidLongMethodsTest.cs: Added a test for static constructors.  This

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

Reply via email to