Author: nestor
Date: 2008-02-06 14:07:33 -0500 (Wed, 06 Feb 2008)
New Revision: 95061
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-06 Nestor Salceda <[EMAIL PROTECTED]>
* AvoidLongMethods.cs: The rule shouldn't be applied to static
constructors.
* AvoidLongMethodsTest.cs: Added a test for static constructors. This
rule shouldn't be applied to 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-06 18:53:04 UTC (rev 95060)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs
2008-02-06 19:07:33 UTC (rev 95061)
@@ -59,7 +59,7 @@
}
}
- private static bool IsAutogenerated (MethodDefinition method)
+ private static bool IsAutogeneratedByTools (MethodDefinition
method)
{
if (method.Parameters.Count != 0)
return false;
@@ -72,6 +72,11 @@
return false;
}
+ private static bool IsStaticConstructor (MethodDefinition
method)
+ {
+ return method.Name == MethodDefinition.Cctor;
+ }
+
public MessageCollection CheckMethod (MethodDefinition method,
Runner runner)
{
// rule does not apply if method as no code (e.g.
abstract, p/invoke)
@@ -82,16 +87,22 @@
if (method.IsGeneratedCode ())
return runner.RuleSuccess;
+ // rule does not apply to autogenerated code from some
+ // tools
+ 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;
- // success if the code is from some well known *large*
methods
- if (IsAutogenerated (method))
- return runner.RuleSuccess;
-
Location location = new Location (method);
Message message = new Message ("The method is too
long.", location, MessageType.Error);
return new MessageCollection (message);
Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/ChangeLog
2008-02-06 18:53:04 UTC (rev 95060)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/ChangeLog
2008-02-06 19:07:33 UTC (rev 95061)
@@ -1,3 +1,8 @@
+2008-02-06 Nestor Salceda <[EMAIL PROTECTED]>
+
+ * AvoidLongMethods.cs: The rule shouldn't be applied to static
+ constructors.
+
2008-01-16 Nestor Salceda <[EMAIL PROTECTED]>
* AvoidLargeClassesRule.cs: The rule shouldn't be applied to enums.
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-06 18:53:04 UTC (rev 95060)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs
2008-02-06 19:07:33 UTC (rev 95061)
@@ -52,6 +52,78 @@
namespace Test.Rules.Smells {
+ public class LongStaticConstructor {
+ static LongStaticConstructor () {
+ 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 ()
{
@@ -1003,5 +1075,13 @@
Assert.IsNotNull (messageCollection);
Assert.AreEqual (1, messageCollection.Count);
}
+
+ [Test]
+ public void LongStaticConstructorTest ()
+ {
+ MethodDefinition staticConstructor =
assembly.MainModule.Types["Test.Rules.Smells.LongStaticConstructor"].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-06 18:53:04 UTC (rev 95060)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Smells/Test/ChangeLog
2008-02-06 19:07:33 UTC (rev 95061)
@@ -1,3 +1,8 @@
+2008-02-06 Nestor Salceda <[EMAIL PROTECTED]>
+
+ * AvoidLongMethodsTest.cs: Added a test for static constructors. This
+ rule shouldn't be applied to static constructors.
+
2008-01-16 Nestor Salceda <[EMAIL PROTECTED]>
* AvoidLargeClassesTest.cs: Added tests for skip the enums. This rule
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches