public void Analyze(MethodBody body, NodeBase analyzedNode) { if (analyzedNode is MethodNode) ((MethodNode)analyzedNode).CyclomaticComplexity = 0; if (body == null) return; foreach (var instruction in body.Instructions) { // IL cyclomatic complexity if (instruction.OpCode.FlowControl == FlowControl.Cond_Branch && analyzedNode is MethodNode) ((MethodNode)analyzedNode).CyclomaticComplexity++; var operand = ReadOperand(instruction); if (operand is MethodReference) { var md = ((MethodReference)operand).Resolve(); if (md != null && assemblies.Contains(md.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(md)) { if (md.IsGetter || md.IsSetter) { var propertyNode = mappings.propertyMappings[(IProperty)mappings.cecilMappings[md]]; analyzedNode.AddRelationship(propertyNode); } else { var methodNode = mappings.methodMappings[(IMethod)mappings.cecilMappings[md]]; analyzedNode.AddRelationship(methodNode); } } } else if (operand is FieldReference) { var fd = ((FieldReference)operand).Resolve(); if (fd != null && assemblies.Contains(fd.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(fd)) { var fieldNode = mappings.fieldMappings[(IField)mappings.cecilMappings[fd]]; analyzedNode.AddRelationship(fieldNode); } } } }
public static string GetInfoText(this NodeBase left, NodeBase top) { int item1 = left.GetUses(top); int item2 = top.GetUses(left); string text = GetText(item1, item2); return string.Format("{0} {1} {2}", left.Name, text, top.Name); }
public int GetUses(NodeBase value) { if (this == value) return -1; int uses; if (relationships.TryGetValue(value, out uses)) return uses; return 0; }
public void AddRelationship(NodeBase reference) { foreach (NodeBase pa in AncestorsAndSelf) { if (reference.AncestorsAndSelf.Contains(pa)) break; foreach (NodeBase pb in reference.AncestorsAndSelf) { if (AncestorsAndSelf.Contains(pb)) break; pa.AddRelationshipInternal(pb); } } }
public void AddEdge(NodeBase target) { // copies all ancestors of target into a hashset // duplicates are removed while (target != null) { if (!outgoingEdges.Add(target)) break; target = target.Parent; } }
void CreateEdges(NodeBase source) { // add edges to source while (source != null) { foreach (NodeBase n in outgoingEdges) { source.AddRelationship(n); } source = source.Parent; } outgoingEdges.Clear(); }
public static BitmapSource GetIcon(NodeBase node) { if (node is AssemblyNode) return GetIcon((AssemblyNode)node); if (node is NamespaceNode) return GetIcon((NamespaceNode)node); if (node is TypeNode) return GetIcon((TypeNode)node); if (node is MethodNode) return GetIcon((MethodNode)node); if (node is FieldNode) return GetIcon((FieldNode)node); if (node is PropertyNode) return GetIcon((PropertyNode)node); if (node is EventNode) return GetIcon((EventNode)node); return null; }
public void Analyze(MethodBody body, NodeBase analyzedNode) { if (analyzedNode is MethodNode) ((MethodNode)analyzedNode).CyclomaticComplexity = 0; if (body == null) return; foreach (var instruction in body.Instructions) { // IL cyclomatic complexity if (instruction.OpCode.FlowControl == FlowControl.Cond_Branch && analyzedNode is MethodNode) ((MethodNode)analyzedNode).CyclomaticComplexity++; var operand = ReadOperand(instruction); try { if (operand is MethodReference) { var md = ((MethodReference)operand).Resolve(); if (md != null && assemblies.Contains(md.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(md)) { if (md.IsGetter || md.IsSetter) { var propertyNode = mappings.propertyMappings[(IProperty)mappings.cecilMappings[md]]; mappings.AddEdge(propertyNode); } else if (md.IsAddOn || md.IsRemoveOn || md.IsFire || md.IsOther) { var eventNode = mappings.eventMappings[(IEvent)mappings.cecilMappings[md]]; mappings.AddEdge(eventNode); } else { var methodNode = mappings.methodMappings[(IMethod)mappings.cecilMappings[md]]; mappings.AddEdge(methodNode); } } } else if (operand is FieldReference) { var fd = ((FieldReference)operand).Resolve(); if (fd != null && assemblies.Contains(fd.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(fd)) { var fieldNode = mappings.fieldMappings[(IField)mappings.cecilMappings[fd]]; mappings.AddEdge(fieldNode); } } } catch (AssemblyResolutionException are) { LoggingService.InfoFormatted("CQA: Skipping operand reference: {0}\r\nException:\r\n{1}", operand, are); } } }
public static object GetInfoText(this NodeBase left, NodeBase top) { int item1 = left.GetUses(top); int item2 = top.GetUses(left); var uses = Tuple.Create(item1, item2); string text = colorizer.GetDescription(uses); return new TextBlock { Inlines = { left.Name, new Bold { Inlines = { " " + text + " " }, Foreground = colorizer.GetColorBrush(uses) }, top.Name } }; }
public MatrixTreeNode(NodeBase node) { this.node = node; }
public AnalysisTypeVisitor(AssemblyAnalyzer context, NodeBase node) { this.context = context; this.node = node; }
void AddRelationshipsForType(NodeBase node, IType type) { type.AcceptVisitor(new AnalysisTypeVisitor(this, node)); }
void AddRelationshipsForAttributes(IList<IAttribute> attributes, NodeBase node) { try { foreach (var attr in attributes) { if (attr.Constructor != null) AddEdge(methodMappings[attr.Constructor]); } } catch (NotSupportedException nse) { // HACK : workaround for bug in NR5's attribute blob parser. LoggingService.DebugFormatted("CQA: Skipping attributes of: {0}\r\nException:\r\n{1}", node.Name, nse); } }
void AddRelationshipsForTypes(IEnumerable<IType> directBaseTypes, NodeBase node) { foreach (var baseType in directBaseTypes) { AddRelationshipsForType(node, baseType); } }
void AddRelationshipsForTypeParameters(IList<ITypeParameter> typeParameters, NodeBase node) { foreach (var param in typeParameters) { AddRelationshipsForAttributes(param.Attributes, node); AddRelationshipsForType(node, param.EffectiveBaseClass); } }
public void AddChild(NodeBase child) { child.Parent = this; Children.Add(child); }
public void AddRelationship(NodeBase reference) { if (!relationships.ContainsKey(reference)) relationships[reference] = 0; relationships[reference]++; }
void AddRelationshipsForAttributes(IList<IAttribute> attributes, NodeBase node) { foreach (var attr in attributes) { node.AddRelationship(methodMappings[attr.Constructor]); } }