Skip to content
Browse files

Fix LCOM4 bug with derived classes

  • Loading branch information...
1 parent 28ee04b commit 7593e4f07c3c99b5fde195adcc4c86f2df0b945f @alexvictoor alexvictoor committed Jan 29, 2013
Showing with 47 additions and 2 deletions.
  1. +45 −0 DependencyParser.Test/Lcom4AnalyzerTest.cs
  2. +2 −2 DependencyParser/Lcom4Analyzer.cs
View
45 DependencyParser.Test/Lcom4AnalyzerTest.cs
@@ -84,6 +84,24 @@ public void Should_Not_Take_In_Account_ToString_Methods()
Assert.AreEqual(2, blocks.Count);
}
+ [Test]
+ public void Should_Not_Take_In_Account_Methods_From_Wrapped_Objects()
+ {
+ var analyzer = new Lcom4Analyzer();
+ var blocks = analyzer.FindLcomBlocks(getType("DependencyParser.Test.SimpleClassWithDelegation"));
+ Assert.AreEqual(1, blocks.Count);
+ Assert.AreEqual(3, blocks.ElementAt(0).Count);
+ }
+
+ [Test]
+ public void Should_Not_Take_In_Account_Inherited_Methods()
+ {
+ var analyzer = new Lcom4Analyzer();
+ var blocks = analyzer.FindLcomBlocks(getType("DependencyParser.Test.DerivedClass"));
+ Assert.AreEqual(1, blocks.Count);
+ Assert.AreEqual(3, blocks.ElementAt(0).Count);
+ }
+
private TypeDefinition getType(string name)
{
string unit = Assembly.GetExecutingAssembly().Location;
@@ -257,4 +275,31 @@ public override string ToString()
return fieldA + " " + fieldB;
}
}
+
+ public class SimpleClassWithDelegation {
+ private SimpleClassWithToString wrapped = new SimpleClassWithToString();
+
+ public void doA()
+ {
+ wrapped.doA();
+ }
+
+ public void doB()
+ {
+ wrapped.doB();
+ }
+ }
+
+ public class DerivedClass : SimpleClassWithTwoFields {
+
+ public void doC()
+ {
+ doA();
+ }
+ public void doD()
+ {
+ doA();
+ doA();
+ }
+ }
}
View
4 DependencyParser/Lcom4Analyzer.cs
@@ -82,15 +82,15 @@ public HashSet<HashSet<MemberReference>> FindLcomBlocks(TypeDefinition t)
private bool NeedToBeFiltered(TypeDefinition t, MethodDefinition method)
{
- return t != method.DeclaringType
+ return (!t.AllSuperTypes().Contains(method.DeclaringType)
||!method.HasBody
|| (method.IsGeneratedCode() && !method.IsSetter && !method.IsGetter)
|| method.IsConstructor
|| (method.IsSpecialName && !method.IsSetter && !method.IsGetter)
|| "Dispose" == method.Name
|| "ToString" == method.Name
|| "Equals" == method.Name
- || "GetHashCode" == method.Name;
+ || "GetHashCode" == method.Name);
}

0 comments on commit 7593e4f

Please sign in to comment.
Something went wrong with that request. Please try again.