Skip to content
Browse files

Add the ability to specify field names to exclude from LCOM4 analysis

  • Loading branch information...
1 parent 1238315 commit de756184cd822f2588b6a0e7e33842a90c7c2757 @alexvictoor alexvictoor committed Feb 12, 2013
Showing with 40 additions and 5 deletions.
  1. +9 −0 DependencyParser.Test/Lcom4AnalyzerTest.cs
  2. +21 −2 DependencyParser/Lcom4Analyzer.cs
  3. +10 −3 DependencyParser/Program.cs
View
9 DependencyParser.Test/Lcom4AnalyzerTest.cs
@@ -191,6 +191,15 @@ public void Should_Ignore_Empty_Virtual_Methods()
Assert.AreEqual(0, blocks.Count);
}
+ [Test]
+ public void Should_Take_In_Account_The_Ignorable_Field_Names()
+ {
+ var analyzer = new Lcom4Analyzer() { IgnorableFieldNames = new string[] {"Counter"} } ;
+
+ var blocks = analyzer.FindLcomBlocks(GetType("DependencyParser.Test.SimpleClass"));
+ Assert.AreEqual(2, blocks.Count);
+ }
+
private TypeDefinition GetType(string name)
{
string unit = Assembly.GetExecutingAssembly().Location;
View
23 DependencyParser/Lcom4Analyzer.cs
@@ -16,6 +16,20 @@ namespace DependencyParser {
/// </summary>
public class Lcom4Analyzer {
+ private IEnumerable<string> ignorableFieldNames = new string[] {};
+
+ public IEnumerable<string> IgnorableFieldNames
+ {
+ get
+ {
+ return ignorableFieldNames;
+ }
+ set
+ {
+ ignorableFieldNames = from n in value select n.ToLowerInvariant();
+ }
+ }
+
public HashSet<HashSet<MemberReference>> FindLcomBlocks(TypeDefinition t)
{
var memberBlocks = new Dictionary<MemberReference, HashSet<MemberReference>>();
@@ -46,8 +60,13 @@ public HashSet<HashSet<MemberReference>> FindLcomBlocks(TypeDefinition t)
switch (inst.OpCode.OperandType)
{
case OperandType.InlineField:
- FieldDefinition fd = inst.Operand as FieldDefinition;
- if (fd == null && inst.Operand is FieldReference)
+
+ FieldReference fr = inst.Operand as FieldReference;
+ if (fr==null || IgnorableFieldNames.Contains(fr.Name.ToLowerInvariant())) {
+ break;
+ }
+ FieldDefinition fd = fr as FieldDefinition;
+ if (fd == null)
{
fd = ((FieldReference) inst.Operand).Resolve();
}
View
13 DependencyParser/Program.cs
@@ -30,13 +30,15 @@ public static void Main(string[] args)
bool showHelp = false;
string assemblyName = "ASSEMBLY.DLL";
string outputPath = "output.xml";
-
- var p = new OptionSet()
+ IEnumerable<string> ignorableFieldNames = null;
+
+ var p = new OptionSet()
{
{ "a|assembly=", "the name of the assembly to scan", v => assemblyName = v },
{ "o|output=", "the path to the output XML", v => outputPath = v },
{ "h|help", "show this message and exit", v => showHelp = v != null },
- { "d|design", "flag that enables design analysis", d => designAnalysis = d != null }
+ { "d|design", "flag that enables design analysis", d => designAnalysis = d != null },
+ { "i|ignorable_fields=", "When design analysis is enabled, comma list of names of fields that should not be taken in account for LCOM4 analysis", list => ignorableFieldNames = list.Split(',') }
};
try
@@ -58,6 +60,11 @@ public static void Main(string[] args)
return;
}
+ if (ignorableFieldNames!=null)
+ {
+ lcom4Analyzer.IgnorableFieldNames = ignorableFieldNames;
+ }
+
var targetFolder = Path.GetDirectoryName(assemblyName);
using (var stream = new FileStream(outputPath, FileMode.Create))

0 comments on commit de75618

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