Author: spouliot
Date: 2008-02-16 14:41:19 -0500 (Sat, 16 Feb 2008)
New Revision: 95909
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/DeserializeOptionalFieldRule.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Gendarme.Rules.Serialization.mdp
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Makefile.am
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializableAttributeOnISerializableTypeRule.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializationConstructorRule.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/DeserializeOptionalFieldTest.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Makefile.am
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializableAttributeOnISerializableTypeTest.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializationConstructorTest.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Test.Rules.Serialization.mdp
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/UseCorrectSignatureForSerializationMethodsTest.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/UseCorrectSignatureForSerializationMethodsRule.cs
Log:
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/DeserializeOptionalFieldRule.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/DeserializeOptionalFieldRule.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/DeserializeOptionalFieldRule.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,99 @@
+//
+// Gendarme.Rules.Serialization.DeserializeOptionalFieldRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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 System.Linq;
+
+using Mono.Cecil;
+
+using Gendarme.Framework;
+using Gendarme.Framework.Rocks;
+
+namespace Gendarme.Rules.Serialization {
+
+ [Problem ("Some fields are marked with [OptionalField] but the type
does not provide special deserialization routines.")]
+ [Solution ("Add a deserialization routine, marked with
[OnDeserialized], and re-compute the correct value for the optional fields.")]
+ public class DeserializeOptionalFieldRule : Rule, ITypeRule {
+
+ private const string MessageOptional = "Optional fields '{0}'
is not deserialized.";
+ private const string MessageSerializable = "Optional fields
'{0}' in non-serializable type.";
+
+ private const string OptionalFieldAttribute =
"System.Runtime.Serialization.OptionalFieldAttribute";
+ private const string OnDeserializedAttribute =
"System.Runtime.Serialization.OnDeserializedAttribute";
+ private const string OnDeserializingAttribute =
"System.Runtime.Serialization.OnDeserializingAttribute";
+
+ public override void Initialize (IRunner runner)
+ {
+ base.Initialize (runner);
+
+ // the [OptionalField] and deserialization attributes
are only available
+ // since fx 2.0 so there's no point to execute it on
every methods if the
+ // assembly target runtime is earlier than 2.0
+ Runner.AnalyzeAssembly += delegate (object o,
RunnerEventArgs e) {
+ Active = (e.CurrentAssembly.Runtime >=
TargetRuntime.NET_2_0);
+ };
+ }
+
+ public RuleResult CheckType (TypeDefinition type)
+ {
+ // note: we cannot quickly return DoesNotApply since we
would miss cases
+ // where [OptionalField] is used on an
non-[Serializable] type
+
+ // look in methods for a deserialization candidates
+ bool deserialized_candidate = false;
+ bool deserializing_candidate = false;
+ foreach (MethodDefinition method in type.Methods) {
+ if (method.CustomAttributes.ContainsType
(OnDeserializedAttribute))
+ deserialized_candidate = true;
+ if (method.CustomAttributes.ContainsType
(OnDeserializingAttribute))
+ deserializing_candidate = true;
+ if (deserialized_candidate &&
deserializing_candidate)
+ break;
+ }
+
+ // check if we found some optional fields, if none then
it's all ok
+ foreach (FieldDefinition field in type.Fields) {
+ if (field.CustomAttributes.ContainsType
(OptionalFieldAttribute)) {
+ if (type.IsSerializable) {
+ // report if we didn't find a
deserialization method
+ if (!deserialized_candidate ||
!deserializing_candidate) {
+ // Medium since it's
possible that the optional fields don't need to be re-computed
+ string s =
String.Format (MessageOptional, field.Name);
+ Runner.Report (field,
Severity.Medium, Confidence.High, s);
+ }
+ } else {
+ // [OptionalField] without
[Serializable] is a bigger problem
+ string s = String.Format
(MessageSerializable, field.Name);
+ Runner.Report (field,
Severity.Critical, Confidence.High, s);
+ }
+ }
+ }
+
+ return Runner.CurrentRuleResult;
+ }
+ }
+}
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Gendarme.Rules.Serialization.mdp
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Gendarme.Rules.Serialization.mdp
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Gendarme.Rules.Serialization.mdp
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,27 @@
+<Project name="Gendarme.Rules.Serialization" fileversion="2.0" language="C#"
clr-version="Net_2_0" ctype="DotNetProject">
+ <Configurations active="Debug">
+ <Configuration name="Debug" ctype="DotNetProjectConfiguration">
+ <Output directory="bin/Debug" assembly="Gendarme.Rules.Serialization" />
+ <Build debugmode="True" target="Library" />
+ <Execution runwithwarnings="True" consolepause="False" runtime="MsNet"
clr-version="Net_2_0" />
+ <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True"
unsafecodeallowed="False" generateoverflowchecks="True" definesymbols="DEBUG"
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+ </Configuration>
+ <Configuration name="Release" ctype="DotNetProjectConfiguration">
+ <Output directory="bin/Release" assembly="Gendarme.Rules.Serialization"
/>
+ <Build debugmode="False" target="Library" />
+ <Execution runwithwarnings="True" consolepause="False" runtime="MsNet"
clr-version="Net_2_0" />
+ <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True"
unsafecodeallowed="False" generateoverflowchecks="True"
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+ </Configuration>
+ </Configurations>
+ <Contents>
+ <File name="DeserializeOptionalFieldRule.cs" subtype="Code"
buildaction="Compile" />
+ <File name="MissingSerializableAttributeOnISerializableTypeRule.cs"
subtype="Code" buildaction="Compile" />
+ <File name="UseCorrectSignatureForSerializationMethodsRule.cs"
subtype="Code" buildaction="Compile" />
+ <File name="MissingSerializationConstructorRule.cs" subtype="Code"
buildaction="Compile" />
+ </Contents>
+ <References>
+ <ProjectReference type="Gac" localcopy="True" refto="System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+ <ProjectReference type="Project" localcopy="True" refto="framework" />
+ <ProjectReference type="Assembly" localcopy="True"
refto="../../../lib/Mono.Cecil.dll" />
+ </References>
+</Project>
\ No newline at end of file
Added: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Makefile.am
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Makefile.am
2008-02-16 17:59:53 UTC (rev 95908)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Makefile.am
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,38 @@
+SUBDIRS=Test
+
+serialization_rulesdir=$(prefix)/lib/gendarme
+serialization_rules_SCRIPTS = ../../bin/Gendarme.Rules.Serialization.dll
+serialization_rules_DATA =
+EXTRA_DIST = $(serialization_rules_sources) $(serialization_rules_sources_in) \
+ Gendarme.Rules.Serialization.mdp Gendarme.Rules.Serialization.csproj
+CLEANFILES = $(serialization_rules_SCRIPTS) $(serialization_rules_SCRIPTS).mdb
Test.Rules.Serialization.dll
+DISTCLEANFILES = Makefile.in Gendarme.Rules.Serialization.xml TestResult.xml
+
+serialization_rules_sources_in = ../../AssemblyInfo.cs.in
+serialization_rules_generated_sources = $(serialization_rules_sources_in:.in=)
+serialization_rules_sources = DeserializeOptionalFieldRule.cs
MissingSerializableAttributeOnISerializableTypeRule.cs \
+ MissingSerializationConstructorRule.cs
UseCorrectSignatureForSerializationMethodsRule.cs
+
+serialization_rules_build_sources = $(addprefix $(srcdir)/,
$(serialization_rules_sources))
+serialization_rules_build_sources += $(serialization_rules_generated_sources)
+
+../../bin/Gendarme.Rules.Serialization.dll:
$(serialization_rules_build_sources)
+ $(GMCS) -debug -target:library
-r:$(top_builddir)/gendarme/bin/Mono.Cecil.dll
-r:../../bin/Gendarme.Framework.dll \
+ -out:$@ $(serialization_rules_build_sources)
+
+serialization_test_sources = DeserializeOptionalFieldTest.cs
MissingSerializableAttributeOnISerializableTypeTest.cs \
+ MissingSerializationConstructorTest.cs
UseCorrectSignatureForSerializationMethodsTest.cs
+
+serialization_test_build_sources = $(addprefix $(srcdir)/Test/,
$(serialization_test_sources))
+
+Test.Rules.Serialization.dll: $(serialization_test_build_sources)
$(serialization_rules_SCRIPTS)
+ $(GMCS) -target:library -r:$(top_builddir)/gendarme/bin/Mono.Cecil.dll
-pkg:mono-nunit -r:../../bin/Gendarme.Framework.dll \
+ -r:$(serialization_rules_SCRIPTS) -out:$@
$(serialization_test_build_sources)
+
+test: Test.Rules.Serialization.dll
+
+run-test: test
+ MONO_PATH=../../bin/:$(MONO_PATH) nunit-console2
Test.Rules.Serialization.dll
+
+self-test: $(serialization_rules_SCRIPTS)
+ mono --debug ../../bin/gendarme.exe $(serialization_rules_SCRIPTS)
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializableAttributeOnISerializableTypeRule.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializableAttributeOnISerializableTypeRule.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializableAttributeOnISerializableTypeRule.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,66 @@
+//
+//
Gendarme.Rules.Serialization.MissingSerializableAttributeOnISerializableTypeRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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 Mono.Cecil;
+
+using Gendarme.Framework;
+using Gendarme.Framework.Rocks;
+
+namespace Gendarme.Rules.Serialization {
+
+ [Problem ("The runtime won't consider this type as serializable unless
your add the [Serialization] attribute to its definition.")]
+ [Solution ("Add [Serialization] to the type definition.")]
+ public class MissingSerializableAttributeOnISerializableTypeRule :
Rule, ITypeRule {
+
+ private const string ISerializable =
"System.Runtime.Serialization.ISerializable";
+
+ public RuleResult CheckType (TypeDefinition type)
+ {
+ // rule does not apply if the type does not implements
ISerializable
+ if (!type.Implements (ISerializable))
+ return RuleResult.DoesNotApply;
+
+ // rule applies only if base type is serializable
+ if (type.BaseType.FullName != "System.Object") {
+ TypeDefinition base_type =
type.BaseType.Resolve ();
+ // in doubt don't report
+ if ((base_type == null) ||
!base_type.IsSerializable)
+ return RuleResult.DoesNotApply;
+ }
+
+ // rule applies, only Success or Failure from the point
on
+
+ // ok if the type has the [Serializable]
pseudo-attribute
+ if (type.IsSerializable)
+ return RuleResult.Success;
+
+ Runner.Report (type, Severity.Critical,
Confidence.Total, String.Empty);
+ return RuleResult.Failure;
+ }
+ }
+}
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializationConstructorRule.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializationConstructorRule.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/MissingSerializationConstructorRule.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,81 @@
+//
+// Gendarme.Rules.Serialization.MissingSerializationConstructorRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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 Mono.Cecil;
+
+using Gendarme.Framework;
+using Gendarme.Framework.Helpers;
+using Gendarme.Framework.Rocks;
+
+namespace Gendarme.Rules.Serialization {
+
+ [Problem ("The required constructor for ISerializable is not present in
this type.")]
+ [Solution ("Add a (private for sealed, protected otherwise)
serialization constructor for this type.")]
+ public class MissingSerializationConstructorRule : Rule, ITypeRule {
+
+ // non-localizable
+ private const string ISerializable =
"System.Runtime.Serialization.ISerializable";
+
+ // localizable
+ private const string NoSerializationCtorText = "The required
constructor for ISerializable is not present in this type.";
+ private const string CtorSealedTypeText = "The serialization
constructor should be private since this type is sealed.";
+ private const string CtorUnsealedTypeText = "The serialization
constructor should be protected (family) since this type is not sealed.";
+
+ public RuleResult CheckType (TypeDefinition type)
+ {
+ // rule does not apply to interfaces, delegates or
types that does not implement ISerializable
+ if (type.IsInterface || type.IsDelegate () ||
!type.Implements (ISerializable))
+ return RuleResult.DoesNotApply;
+
+ // rule applies, only Success or Failure from the point
on
+
+ // check if the type implements the serialization
constructor
+ MethodDefinition ctor = type.GetMethod
(MethodSignatures.SerializationConstructor);
+ if (ctor == null) {
+ // no serialization ctor
+ Runner.Report (type, Severity.High,
Confidence.Total, NoSerializationCtorText);
+ return RuleResult.Failure;
+ } else if (type.IsSealed) {
+ // with ctor: on a sealed type the ctor must be
private
+ if (!ctor.IsPrivate) {
+ Runner.Report (type, Severity.Low,
Confidence.Total, CtorSealedTypeText);
+ return RuleResult.Failure;
+ }
+ } else {
+ // with ctor: on a unsealed type the ctor must
be family
+ if (!ctor.IsFamily) {
+ Runner.Report (type, Severity.Low,
Confidence.Total, CtorUnsealedTypeText);
+ return RuleResult.Failure;
+ }
+ }
+
+ // everything is fine
+ return RuleResult.Success;
+ }
+ }
+}
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/DeserializeOptionalFieldTest.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/DeserializeOptionalFieldTest.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/DeserializeOptionalFieldTest.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,152 @@
+//
+// Unit tests for DeserializeOptionalFieldRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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.Reflection;
+using System.Runtime.Serialization;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Serialization;
+
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.Serialization {
+
+ [Serializable]
+ public class ClassWithOptionalFieldAndBothDeserializationAttributes {
+ [OptionalField]
+ private int optional = 1;
+
+ [OnDeserialized]
+ private void Deserialized (StreamingContext context)
+ {
+ optional = 0;
+ }
+
+ [OnDeserializing]
+ private void OnDeserializing (StreamingContext context)
+ {
+ optional = 0;
+ }
+ }
+
+ [Serializable]
+ public class ClassWithOptionalFieldAndOnDeserializingAttributes {
+ [OptionalField]
+ private int optional = 1;
+
+ [OnDeserializing]
+ private void OnDeserializing (StreamingContext context)
+ {
+ optional = 0;
+ }
+ }
+
+ [Serializable]
+ public class ClassWithOptionalFieldAndOnDeserializedAttributes {
+ [OptionalField]
+ private int optional = 1;
+
+ [OnDeserialized]
+ private void OnDeserialized (StreamingContext context)
+ {
+ optional = 0;
+ }
+ }
+
+ [Serializable]
+ public class ClassWithOptionalField {
+ [OptionalField]
+ private int optional;
+ }
+
+ [Serializable]
+ public class ClassWithoutOptionalField {
+ private int optional;
+ }
+
+ // we should warn that the type is *not* [Serializable]
+ public class NonSerializableClassWithOptionalField {
+ [OptionalField]
+ private int optional;
+ }
+
+ [TestFixture]
+ public class DeserializeOptionalFieldTest {
+
+ private ITypeRule rule;
+ private TestRunner runner;
+ private AssemblyDefinition assembly;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ rule = new DeserializeOptionalFieldRule ();
+ runner = new TestRunner (rule);
+ }
+
+ private TypeDefinition GetTest (string name)
+ {
+ string fullname = "Test.Rules.Serialization." + name;
+ return assembly.MainModule.Types [fullname];
+ }
+
+ [Test]
+ public void Success ()
+ {
+ TypeDefinition type = GetTest
("ClassWithOptionalFieldAndBothDeserializationAttributes");
+ Assert.AreEqual (RuleResult.Success, runner.CheckType
(type), "ClassWithOptionalFieldAndBothDeserializationAttributes");
+ Assert.AreEqual (0, runner.Defects.Count,
"ClassWithOptionalFieldAndBothDeserializationAttributes-Count");
+
+ type = GetTest ("ClassWithoutOptionalField");
+ Assert.AreEqual (RuleResult.Success, runner.CheckType
(type), "ClassWithoutOptionalField");
+ Assert.AreEqual (0, runner.Defects.Count,
"ClassWithoutOptionalField-Count");
+ }
+
+ [Test]
+ public void Failure ()
+ {
+ TypeDefinition type = GetTest
("ClassWithOptionalField");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "ClassWithOptionalField");
+ Assert.AreEqual (1, runner.Defects.Count,
"ClassWithOptionalField-Count");
+
+ type = GetTest
("ClassWithOptionalFieldAndOnDeserializedAttributes");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "ClassWithOptionalFieldAndOnDeserializedAttributes");
+ Assert.AreEqual (1, runner.Defects.Count,
"ClassWithOptionalFieldAndOnDeserializedAttributes-Count");
+
+ type = GetTest
("ClassWithOptionalFieldAndOnDeserializingAttributes");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "ClassWithOptionalFieldAndOnDeserializingAttributes");
+ Assert.AreEqual (1, runner.Defects.Count,
"ClassWithOptionalFieldAndOnDeserializingAttributes-Count");
+
+ type = GetTest
("NonSerializableClassWithOptionalField");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "NonSerializableClassWithOptionalField");
+ Assert.AreEqual (1, runner.Defects.Count,
"NonSerializableClassWithOptionalField-Count");
+ }
+ }
+}
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Makefile.am
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Makefile.am
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Makefile.am
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,4 @@
+EXTRA_DIST = *.cs Test.Rules.Serialization.mdp Tests.Rules.Serialization.csproj
+DISTCLEANFILES = Makefile.in
+
+all:
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializableAttributeOnISerializableTypeTest.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializableAttributeOnISerializableTypeTest.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializableAttributeOnISerializableTypeTest.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,104 @@
+//
+// Unit tests for MissingSerializableAttributeOnISerializableTypeRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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.Reflection;
+using System.Runtime.Serialization;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Serialization;
+
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.Serialization {
+
+ [Serializable]
+ public class ClassWithAttributeOnly {
+ }
+
+ public class ClassWithoutAttribute : ISerializable {
+
+ public void GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ }
+ }
+
+ [Serializable]
+ public class ClassWithAttribute : ISerializable {
+
+ public void GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ }
+ }
+
+ [TestFixture]
+ public class MissingSerializableAttributeOnISerializableTypeTest {
+
+ private ITypeRule rule;
+ private TestRunner runner;
+ private AssemblyDefinition assembly;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ rule = new
MissingSerializableAttributeOnISerializableTypeRule ();
+ runner = new TestRunner (rule);
+ }
+
+ private TypeDefinition GetTest (string name)
+ {
+ string fullname = "Test.Rules.Serialization." + name;
+ return assembly.MainModule.Types [fullname];
+ }
+
+ [Test]
+ public void DoesNotApply ()
+ {
+ TypeDefinition type = GetTest
("MissingSerializableAttributeOnISerializableTypeTest");
+ Assert.AreEqual (RuleResult.DoesNotApply,
runner.CheckType (type), "NoAttribute No Interface");
+
+ type = GetTest ("ClassWithAttributeOnly");
+ Assert.AreEqual (RuleResult.DoesNotApply,
runner.CheckType (type), "ClassWithAttributeOnly");
+ }
+
+ [Test]
+ public void Success ()
+ {
+ TypeDefinition type = GetTest ("ClassWithAttribute");
+ Assert.AreEqual (RuleResult.Success, runner.CheckType
(type), "ClassWithAttribute");
+ }
+
+ [Test]
+ public void Failure ()
+ {
+ TypeDefinition type = GetTest ("ClassWithoutAttribute");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "ClassWithoutAttribute");
+ }
+ }
+}
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializationConstructorTest.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializationConstructorTest.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/MissingSerializationConstructorTest.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,147 @@
+//
+// Unit tests for MissingSerializationConstructorRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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.Reflection;
+using System.Runtime.Serialization;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Serialization;
+
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.Serialization {
+
+ [Serializable]
+ public class ClassWithoutConstructor : ISerializable {
+
+ public void GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ }
+ }
+
+ [Serializable]
+ public class UnsealedClassWrongCtorVisibility : ISerializable {
+
+ public UnsealedClassWrongCtorVisibility (SerializationInfo
info, StreamingContext context)
+ {
+ }
+
+ public void GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ }
+ }
+
+ [Serializable]
+ public sealed class SealedClassWrongCtorVisibility : ISerializable {
+
+ protected SealedClassWrongCtorVisibility (SerializationInfo
info, StreamingContext context)
+ {
+ }
+
+ public void GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ }
+ }
+
+ [Serializable]
+ public class PerfectUnsealedClass : ISerializable {
+
+ protected PerfectUnsealedClass (SerializationInfo info,
StreamingContext context)
+ {
+ }
+
+ public void GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ }
+ }
+
+ [Serializable]
+ public sealed class PerfectSealedClass : ISerializable {
+
+ private PerfectSealedClass (SerializationInfo info,
StreamingContext context)
+ {
+ }
+
+ public void GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ }
+ }
+
+ [TestFixture]
+ public class MissingSerializationConstructorTest {
+
+ private ITypeRule rule;
+ private TestRunner runner;
+ private AssemblyDefinition assembly;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ rule = new MissingSerializationConstructorRule ();
+ runner = new TestRunner (rule);
+ }
+
+ private TypeDefinition GetTest (string name)
+ {
+ string fullname = "Test.Rules.Serialization." + name;
+ return assembly.MainModule.Types [fullname];
+ }
+
+ [Test]
+ public void DoesNotApply ()
+ {
+ TypeDefinition type = GetTest
("MissingSerializationConstructorTest");
+ Assert.AreEqual (RuleResult.DoesNotApply,
runner.CheckType (type), "! ISerializable");
+ }
+
+ [Test]
+ public void Success ()
+ {
+ TypeDefinition type = GetTest ("PerfectUnsealedClass");
+ Assert.AreEqual (RuleResult.Success, runner.CheckType
(type), "PerfectUnsealedClass");
+
+ type = GetTest ("PerfectSealedClass");
+ Assert.AreEqual (RuleResult.Success, runner.CheckType
(type), "PerfectSealedClass");
+ }
+
+ [Test]
+ public void Failure ()
+ {
+ TypeDefinition type = GetTest
("ClassWithoutConstructor");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "ClassWithoutConstructor");
+
+ type = GetTest ("UnsealedClassWrongCtorVisibility");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "UnsealedClassWrongCtorVisibility");
+
+ type = GetTest ("SealedClassWrongCtorVisibility");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckType
(type), "SealedClassWrongCtorVisibility");
+ }
+ }
+}
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Test.Rules.Serialization.mdp
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Test.Rules.Serialization.mdp
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/Test.Rules.Serialization.mdp
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,28 @@
+<Project name="Test" fileversion="2.0" language="C#" clr-version="Net_2_0"
ctype="DotNetProject">
+ <Configurations active="Debug">
+ <Configuration name="Debug" ctype="DotNetProjectConfiguration">
+ <Output directory="bin/Debug" assembly="Test" />
+ <Build debugmode="True" target="Library" />
+ <Execution runwithwarnings="True" consolepause="False" runtime="MsNet"
clr-version="Net_2_0" />
+ <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True"
unsafecodeallowed="False" generateoverflowchecks="True" definesymbols="DEBUG"
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+ </Configuration>
+ <Configuration name="Release" ctype="DotNetProjectConfiguration">
+ <Output directory="bin/Release" assembly="Test" />
+ <Build debugmode="False" target="Library" />
+ <Execution runwithwarnings="True" consolepause="False" runtime="MsNet"
clr-version="Net_2_0" />
+ <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True"
unsafecodeallowed="False" generateoverflowchecks="True"
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+ </Configuration>
+ </Configurations>
+ <Contents>
+ <File name="DeserializeOptionalFieldTest.cs" subtype="Code"
buildaction="Compile" />
+ <File name="MissingSerializableAttributeOnISerializableTypeTest.cs"
subtype="Code" buildaction="Compile" />
+ <File name="MissingSerializationConstructorTest.cs" subtype="Code"
buildaction="Compile" />
+ <File name="UseCorrectSignatureForSerializationMethodsTest.cs"
subtype="Code" buildaction="Compile" />
+ </Contents>
+ <References>
+ <ProjectReference type="Gac" localcopy="True" refto="System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+ <ProjectReference type="Project" localcopy="True" refto="framework" />
+ <ProjectReference type="Project" localcopy="True"
refto="Gendarme.Rules.Serialization" />
+ <ProjectReference type="Assembly" localcopy="True"
refto="../../../../lib/Mono.Cecil.dll" />
+ </References>
+</Project>
\ No newline at end of file
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/UseCorrectSignatureForSerializationMethodsTest.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/UseCorrectSignatureForSerializationMethodsTest.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/Test/UseCorrectSignatureForSerializationMethodsTest.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,172 @@
+//
+// Unit tests for ImplementSerializationEventsCorrectlyRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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.Reflection;
+using System.Runtime.Serialization;
+
+using Gendarme.Framework;
+using Gendarme.Framework.Rocks;
+using Gendarme.Rules.Serialization;
+
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.Serialization {
+
+ [Serializable]
+ public class BadClass {
+
+ [OnSerializing]
+ public void Serializing (StreamingContext context)
+ {
+ // bad visibility, should be private
+ }
+
+/* this compiles but nunit cause an exception when calling
System.Reflection.Assembly.GetExportedTypes() */
+#if false
+ [OnSerialized]
+ private void Serialized (SerializationInfo info)
+ {
+ // bad parameter type
+ }
+
+ [OnDeserializing]
+ private void Deserializing (SerializationInfo info,
StreamingContext context)
+ {
+ // bad parameter types (count)
+ }
+
+ [OnDeserialized]
+ private bool Deserializing (StreamingContext context)
+ {
+ // bad return value
+ return false;
+ }
+#endif
+ }
+
+ [Serializable]
+ public class OkClass {
+
+ [OnSerializing, OnDeserializing]
+ private void Lizing (StreamingContext context)
+ {
+ }
+
+ [OnSerialized, OnDeserialized]
+ private void Lized (StreamingContext context)
+ {
+ }
+ }
+
+ public class NotSerializableClass {
+
+ [OnSerializing, OnDeserializing]
+ private void Lizing (StreamingContext context)
+ {
+ }
+
+ [OnSerialized, OnDeserialized]
+ private void Lized (StreamingContext context)
+ {
+ }
+ }
+
+ [TestFixture]
+ public class UseCorrectSignatureForSerializationMethodsTest {
+
+ [OnSerializing]
+ private void Serializing (StreamingContext context)
+ {
+ // method is ok but it's type is not [Serializable]
+ }
+
+ private IRule rule;
+ private TestRunner runner;
+ private AssemblyDefinition assembly;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ rule = new
UseCorrectSignatureForSerializationMethodsRule ();
+ runner = new TestRunner (rule);
+ }
+
+ private MethodDefinition GetTest (string type, string method)
+ {
+ string fullname = "Test.Rules.Serialization." + type;
+ return assembly.MainModule.Types [fullname].GetMethod
(method);
+ }
+
+ [Test]
+ public void Ok ()
+ {
+ MethodDefinition method = GetTest ("OkClass", "Lizing");
+ Assert.AreEqual (RuleResult.Success, runner.CheckMethod
(method), "Lizing-Failure");
+ Assert.AreEqual (0, runner.Defects.Count,
"Lizing-Count");
+
+ method = GetTest ("OkClass", "Lized");
+ Assert.AreEqual (RuleResult.Success, runner.CheckMethod
(method), "Lized-Failure");
+ Assert.AreEqual (0, runner.Defects.Count,
"Lized-Count");
+ }
+
+ [Test]
+ public void BadSignatures ()
+ {
+ MethodDefinition method = GetTest ("BadClass",
"Serializing");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckMethod
(method), "Serializing-Failure");
+ Assert.AreEqual (1, runner.Defects.Count,
"Serializing-Count");
+#if false
+ method = GetTest ("BadClass", "Serialized");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckMethod
(method), "Serialized-Failure");
+ Assert.AreEqual (1, runner.Defects.Count,
"Serialized-Count");
+
+ method = GetTest ("BadClass", "Deserializing");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckMethod
(method), "Deserializing-Failure");
+ Assert.AreEqual (1, runner.Defects.Count,
"Deserializing-Count");
+
+ method = GetTest ("BadClass", "OnDeserialized");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckMethod
(method), "OnDeserialized-Failure");
+ Assert.AreEqual (1, runner.Defects.Count,
"OnDeserialized-Count");
+#endif
+ }
+
+ [Test]
+ public void NotSerializable ()
+ {
+ MethodDefinition method = GetTest
("NotSerializableClass", "Lizing");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckMethod
(method), "Lizing-Failure");
+ Assert.AreEqual (1, runner.Defects.Count,
"Lizing-Count");
+
+ method = GetTest ("NotSerializableClass", "Lized");
+ Assert.AreEqual (RuleResult.Failure, runner.CheckMethod
(method), "Lized-Failure");
+ Assert.AreEqual (1, runner.Defects.Count,
"Lized-Count");
+ }
+ }
+}
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/UseCorrectSignatureForSerializationMethodsRule.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/UseCorrectSignatureForSerializationMethodsRule.cs
2008-02-16 17:59:53 UTC (rev 95908)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/UseCorrectSignatureForSerializationMethodsRule.cs
2008-02-16 19:41:19 UTC (rev 95909)
@@ -0,0 +1,89 @@
+//
+// Gendarme.Rules.Serialization.MissingSerializationConstructorRule
+//
+// Authors:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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 Mono.Cecil;
+
+using Gendarme.Framework;
+using Gendarme.Framework.Helpers;
+using Gendarme.Framework.Rocks;
+
+namespace Gendarme.Rules.Serialization {
+
+ [Problem ("The method has the wrong signature, it should return
System.Void and have a single parameter of type
'System.Runtime.Serialization.StreamingContext' and be private.")]
+ [Solution ("Fix method signature to match the runtime requirements.")]
+ public class UseCorrectSignatureForSerializationMethodsRule : Rule,
IMethodRule {
+
+ private const string NotSerializableText = "The type of this
method is not marked as [Serializable].";
+ private const string WrongSignatureText = "The method has the
wrong signature, it should return System.Void and have a single parameter of
type 'System.Runtime.Serialization.StreamingContext' and be private.";
+
+ static string [] Attributes = {
+ "System.Runtime.Serialization.OnSerializingAttribute",
+ "System.Runtime.Serialization.OnSerializedAttribute",
+ "System.Runtime.Serialization.OnDeserializingAttribute",
+ "System.Runtime.Serialization.OnDeserializedAttribute"
+ };
+
+ public override void Initialize (IRunner runner)
+ {
+ base.Initialize (runner);
+
+ // the attributes are only available since fx 2.0 so
there's no point
+ // to execute it on every methods if the assembly
target runtime is
+ // earlier than 2.0
+ Runner.AnalyzeAssembly += delegate (object o,
RunnerEventArgs e) {
+ Active = (e.CurrentAssembly.Runtime >=
TargetRuntime.NET_2_0);
+ };
+ }
+
+ public RuleResult CheckMethod (MethodDefinition method)
+ {
+ // rule does not apply to constructor or to methods
without custom attributes
+ if (method.IsConstructor ||
(method.CustomAttributes.Count == 0))
+ return RuleResult.DoesNotApply;
+
+ // marked with any of On[Des|S]erializ[ed|ing]Attribute
?
+ if (!method.CustomAttributes.ContainsAnyType
(Attributes))
+ return RuleResult.DoesNotApply;
+
+ // rule apply!
+
+ // if the type is not marked as [Serializable] then
warn that this code is useless
+ bool serializable = (method.DeclaringType as
TypeDefinition).IsSerializable;
+ if (!serializable)
+ Runner.Report (method, Severity.Critical,
Confidence.Total, NotSerializableText);
+
+ // check if the method signature is correct, return if
it is
+ if (MethodSignatures.SerializationEventHandler.Matches
(method))
+ return Runner.CurrentRuleResult;
+
+ // but report an error if the signature isn't valid
+ Runner.Report (method, Severity.Critical,
Confidence.Total, WrongSignatureText);
+ return RuleResult.Failure;
+ }
+ }
+}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches