public static void AddTypes (ProjectProperties projectprop, MetricsContext ctx) { ProjectDom dom = ProjectDomService.GetProjectDom (projectprop.Project); foreach (IType ob in dom.Types) { projectprop.AddInstance(ob); } }
public static void AddTypes (ProjectProperties projectprop, MetricsContext ctx) { var dom = TypeSystemService.GetCompilation (projectprop.Project); foreach (var ob in dom.MainAssembly.GetAllTypeDefinitions ()) { projectprop.AddInstance(ob); } }
public static void EvaluateOOMetrics (MetricsContext ctx, ProjectProperties projprop) { foreach(var cls in projprop.Classes) ObjectOrientedMetrics.ClassEvaluateInheritanceTree(ctx, cls.Value); foreach (var namesp in projprop.Namespaces){ foreach (var cls in namesp.Value.Classes){ ObjectOrientedMetrics.ClassEvaluateInheritanceTree(ctx, cls.Value); } } }
public static void EvaluateOOMetrics(MetricsContext ctx, ProjectProperties projprop) { foreach (var cls in projprop.Classes) { ObjectOrientedMetrics.ClassEvaluateInheritanceTree(ctx, cls.Value); } foreach (var namesp in projprop.Namespaces) { foreach (var cls in namesp.Value.Classes) { ObjectOrientedMetrics.ClassEvaluateInheritanceTree(ctx, cls.Value); } } }
public static void EvaluateComplexityMetrics (MetricsContext ctx, ProjectProperties project) { mctx = ctx; PrefixName = new Stack<string>(0); lock(mctx) { foreach (var file in project.Project.Files) { /*if(file.BuildAction != BuildAction.Compile) continue;*/ // Files not set to compile are sometimes not accessible if(file.Name.Contains("svn-base")) continue; string text=""; try { text = System.IO.File.ReadAllText(file.FilePath); } catch (System.UnauthorizedAccessException uae) { continue; } catch (System.IO.FileNotFoundException fnf) { // This exception arises in Nrefactory...WTF? 0_0 continue; } ProjProp = project; File = file; Mono.TextEditor.Document doc = new Mono.TextEditor.Document (); doc.Text = text; FileDoc = doc; FileText = new List<LineSegment>(); foreach(LineSegment segment in doc.Lines) FileText.Add(segment); using (ICSharpCode.NRefactory.IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(text))) { parser.Parse(); if (parser.Errors.Count > 0) { //Error handling TODO } else { foreach (var it in parser.CompilationUnit.Children) { ProcessNode (ctx, it); } } } } } }
private static void ProcessNode(MetricsContext ctx, ICSharpCode.OldNRefactory.Ast.INode node) { if (node is UsingStatement) { //TODO do something (something to do with afferent and efferent coupling of namespaces) } else if (node is NamespaceDeclaration) { try { PrefixName.Push(((NamespaceDeclaration)node).Name); LOCEvaluate.EvaluateNamespaceLOC(ctx, (NamespaceDeclaration)node); PrefixName.Pop(); } catch (Exception e) { } } else if (node is TypeDeclaration) { LOCEvaluate.EvaluateTypeLOC(ctx, null, (TypeDeclaration)node, node.StartLocation.Line); } }
private static void ClassEvaluateInheritanceTree (MetricsContext ctx, ClassProperties cls) { foreach (var ob in cls.Class.GetAllBaseTypeDefinitions ()) { if (ob.Kind == TypeKind.Class && ob != cls.Class && ob.Name!="Object") { cls.DepthOfInheritance++; ClassProperties tmp = ctx.GetInstanceOf (ob); // lock tmp here if(tmp != null) { tmp.FanOut++; cls.InheritedFieldCount += tmp.FieldCount; cls.InheritedMethodCount += tmp.MethodCount; if (ob.IsAbstract) cls.DataAbstractionCoupling++; } } foreach (var innercls in cls.InnerClasses) { ClassEvaluateInheritanceTree (ctx, innercls.Value); } } }
private static void ClassEvaluateInheritanceTree (MetricsContext ctx, ClassProperties cls) { foreach (IType ob in cls.Class.SourceProjectDom.GetInheritanceTree (cls.Class)) { if (ob.ClassType == MonoDevelop.Projects.Dom.ClassType.Class && ob != cls.Class && ob.Name!="Object") { cls.DepthOfInheritance++; ClassProperties tmp = ctx.GetInstanceOf (ob); // lock tmp here if(tmp != null) { tmp.FanOut++; cls.InheritedFieldCount += tmp.FieldCount; cls.InheritedMethodCount += tmp.MethodCount; if (ob.IsAbstract) cls.DataAbstractionCoupling++; } } foreach (var innercls in cls.InnerClasses) { ClassEvaluateInheritanceTree (ctx, innercls.Value); } } }
private static void ProcessMethod(MetricsContext ctx, ICSharpCode.OldNRefactory.Ast.INode method, IProperties parentClass) { if (method == null) { return; } StringBuilder methodName = new StringBuilder(""); string[] PrefixArray = PrefixName.ToArray(); for (int i = 0; i < PrefixArray.Length; i++) { methodName.Append(PrefixArray[PrefixArray.Length - i - 1] + "."); } List <string> methodParameterList = new List <string>(0); if (method is MethodDeclaration) { methodName.Append(((MethodDeclaration)method).Name); foreach (ParameterDeclarationExpression pde in ((MethodDeclaration)method).Parameters) { string type = pde.TypeReference.Type; if (type.Contains(".")) { type = type.Substring(type.LastIndexOf(".") + 1); } methodParameterList.Add(type); } } else if (method is ConstructorDeclaration) { methodName.Append(((ConstructorDeclaration)method).Name); foreach (ParameterDeclarationExpression pde in ((ConstructorDeclaration)method).Parameters) { string type = pde.TypeReference.Type; if (type.Contains(".")) { type = type.Substring(type.LastIndexOf(".") + 1); } methodParameterList.Add(type); } } StringBuilder MethodKey = new StringBuilder(); MethodKey.Append(methodName.ToString() + " "); foreach (string paramName in methodParameterList) { MethodKey.Append(paramName + " "); } try{ if (parentClass is ClassProperties) { if (!(parentClass as ClassProperties).Methods.ContainsKey(MethodKey.ToString())) { if (method is MethodDeclaration) { (parentClass as ClassProperties).Methods.Add(MethodKey.ToString(), new MethodProperties((MethodDeclaration)method, parentClass as ClassProperties)); } else if (method is ConstructorDeclaration) { (parentClass as ClassProperties).Methods.Add(MethodKey.ToString(), new MethodProperties((ConstructorDeclaration)method, parentClass as ClassProperties)); } } var currentMethodReference = (parentClass as ClassProperties).Methods[MethodKey.ToString()]; //Calculate all metrics here ASTVisitor.EvaluateComplexityMetrics(method, currentMethodReference); LOCEvaluate.EvaluateMethodLOC(currentMethodReference, FileText, FileDoc); currentMethodReference.FilePath = File.FilePath; } } catch (NullReferenceException ex) { LoggingService.LogError("Error in '" + methodName.ToString() + "'", ex); Console.WriteLine(MethodKey.ToString() + " hoo"); } }
internal static void EvaluateNamespaceLOC (MetricsContext ctx, NamespaceDeclaration node) { if(node == null) return; //MessageService.ShowMessage(node.ToString()); try { NamespaceProperties namespaceRef = ComplexityMetrics.ProjProp.GetNamespaceReference(node.Name); if(namespaceRef==null) return; Dictionary<int, ICSharpCode.NRefactory.Ast.INode> typeLocations = new Dictionary<int, ICSharpCode.NRefactory.Ast.INode>(); foreach(var childnode in node.Children){ if(childnode is TypeDeclaration) typeLocations.Add(childnode.StartLocation.Line, childnode); } if(namespaceRef.FilePath==null||namespaceRef.FilePath=="") namespaceRef.FilePath = ComplexityMetrics.File.FilePath; #region CommonLogic int startIndex = node.StartLocation.Line; int endIndex = node.EndLocation.Line; ulong totalLines = 0, totalRealLines = 0, totalCommentedLines = 0; int realLines = 0; bool isSingleLineComment = false; bool isMultipleLineComment = false; for(int i=startIndex;i<endIndex;i++) { string lineText = ComplexityMetrics.FileDoc.GetTextAt(ComplexityMetrics.FileText[i]).Trim(); if(isMultipleLineComment){ totalCommentedLines++; if(lineText.EndsWith("*/")) isMultipleLineComment = false; continue; } if(lineText.StartsWith ("/*")){ isMultipleLineComment = true; totalCommentedLines++; continue; } isSingleLineComment = lineText.StartsWith ("//"); if(isSingleLineComment) totalCommentedLines++; if (lineText.Length > 0 && !isSingleLineComment) { realLines++; if((typeLocations.ContainsKey(i)) && (typeLocations[i] is TypeDeclaration)) i = EvaluateTypeLOC(ctx, namespaceRef, (TypeDeclaration)typeLocations[i], i); if((typeLocations.ContainsKey(i+1)) &&(typeLocations[i+1] is TypeDeclaration)) i = EvaluateTypeLOC(ctx, namespaceRef, (TypeDeclaration)typeLocations[i+1], i); } } totalLines += (ulong)(startIndex-endIndex+1); totalRealLines += (ulong)realLines; namespaceRef.LOCReal += totalRealLines; namespaceRef.LOCComments += totalCommentedLines; #endregion CommonLogic } catch (Exception e) { Console.WriteLine(e.ToString()); } }
internal static int EvaluateTypeLOC (MetricsContext ctx, NamespaceProperties namespaceRef, TypeDeclaration node, int startIndex) { if(node==null) return -1; StringBuilder typeName = new StringBuilder("");; try { string[] prefixArray = ComplexityMetrics.PrefixName.ToArray(); for(int i=0;i<prefixArray.Length;i++) typeName.Append(prefixArray[prefixArray.Length-i-1]+"."); typeName.Append(node.Name); foreach(var templateDef in node.Templates) { foreach(var bases in templateDef.Bases) { if(bases.Type.Contains("constraint:")) continue; typeName.Append(" " + bases.Type.Substring(bases.Type.LastIndexOf(".")+1)); } } IProperties typeRef = null; switch(node.Type) { case ICSharpCode.NRefactory.Ast.ClassType.Class: typeRef = ComplexityMetrics.ProjProp.GetClassReference(typeName.ToString()); break; case ICSharpCode.NRefactory.Ast.ClassType.Enum: typeRef = ComplexityMetrics.ProjProp.GetEnumReference(typeName.ToString(), namespaceRef); break; case ICSharpCode.NRefactory.Ast.ClassType.Struct: typeRef = ComplexityMetrics.ProjProp.GetStructReference(typeName.ToString(), namespaceRef); break; case ICSharpCode.NRefactory.Ast.ClassType.Interface: typeRef = ComplexityMetrics.ProjProp.GetInterfaceReference(typeName.ToString(), namespaceRef); break; default: return node.EndLocation.Line; } if(typeRef==null) return node.EndLocation.Line; Dictionary<int, ICSharpCode.NRefactory.Ast.INode> childLocations = new Dictionary<int, ICSharpCode.NRefactory.Ast.INode>(0); foreach(ICSharpCode.NRefactory.Ast.INode childNode in node.Children) { if((childNode is TypeDeclaration) || (childNode is ConstructorDeclaration) || (childNode is MethodDeclaration)) childLocations.Add(childNode.StartLocation.Line, childNode); } if(typeRef.FilePath==null||typeRef.FilePath=="") typeRef.FilePath=ComplexityMetrics.File.FilePath; startIndex = node.StartLocation.Line; int endIndex = node.EndLocation.Line; ulong totalLines = 0, totalRealLines = 0, totalCommentedLines = 0; int realLines = 0; bool isSingleLineComment = false; bool isMultipleLineComment = false; for(int i=startIndex;i< endIndex;i++) { string lineText = ComplexityMetrics.FileDoc.GetTextAt(ComplexityMetrics.FileText[i]).Trim(); if(isMultipleLineComment){ totalCommentedLines++; if(lineText.EndsWith("*/")) isMultipleLineComment = false; continue; } if(lineText.StartsWith ("/*")){ isMultipleLineComment = true; totalCommentedLines++; continue; } isSingleLineComment = lineText.StartsWith ("//"); if(isSingleLineComment) totalCommentedLines++; if (lineText.Length > 0 && !isSingleLineComment) { realLines++; if(childLocations.ContainsKey(i)) { ComplexityMetrics.PrefixName.Push(node.Name); if((childLocations[i] is MethodDeclaration) || (childLocations[i] is ConstructorDeclaration)) { ComplexityMetrics.ProcessMethod(ctx, childLocations[i], typeRef); i = childLocations[i].EndLocation.Line; } else if(childLocations[i] is TypeDeclaration) { i = EvaluateTypeLOC(ctx, namespaceRef, (TypeDeclaration)childLocations[i], i); } ComplexityMetrics.PrefixName.Pop(); } } } totalLines += (ulong)(endIndex-startIndex+2); totalRealLines += (ulong)realLines; if(typeRef is ClassProperties) { ((ClassProperties)typeRef).LOCReal += totalRealLines; ((ClassProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is InterfaceProperties) { ((InterfaceProperties)typeRef).LOCReal += totalRealLines; ((InterfaceProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is EnumProperties) { ((EnumProperties)typeRef).LOCReal += totalRealLines; ((EnumProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is StructProperties) { ((StructProperties)typeRef).LOCReal += totalRealLines; ((StructProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is DelegateProperties) { ((DelegateProperties)typeRef).LOCReal += totalRealLines; ((DelegateProperties)typeRef).LOCComments += totalCommentedLines; } } catch (Exception e) { Console.WriteLine("Error in class " + typeName.ToString()); Console.WriteLine(e.ToString()); } return node.EndLocation.Line; }
internal static void EvaluateNamespaceLOC(MetricsContext ctx, NamespaceDeclaration node) { if (node == null) { return; } //MessageService.ShowMessage(node.ToString()); try { NamespaceProperties namespaceRef = ComplexityMetrics.ProjProp.GetNamespaceReference(node.Name); if (namespaceRef == null) { return; } Dictionary <int, ICSharpCode.OldNRefactory.Ast.INode> typeLocations = new Dictionary <int, ICSharpCode.OldNRefactory.Ast.INode>(); foreach (var childnode in node.Children) { if (childnode is TypeDeclaration) { typeLocations.Add(childnode.StartLocation.Line, childnode); } } if (namespaceRef.FilePath == null || namespaceRef.FilePath == "") { namespaceRef.FilePath = ComplexityMetrics.File.FilePath; } #region CommonLogic int startIndex = node.StartLocation.Line; int endIndex = node.EndLocation.Line; ulong totalLines = 0, totalRealLines = 0, totalCommentedLines = 0; int realLines = 0; bool isSingleLineComment = false; bool isMultipleLineComment = false; for (int i = startIndex; i < endIndex; i++) { string lineText = ComplexityMetrics.FileDoc.GetTextAt(ComplexityMetrics.FileText[i]).Trim(); if (isMultipleLineComment) { totalCommentedLines++; if (lineText.EndsWith("*/")) { isMultipleLineComment = false; } continue; } if (lineText.StartsWith("/*")) { isMultipleLineComment = true; totalCommentedLines++; continue; } isSingleLineComment = lineText.StartsWith("//"); if (isSingleLineComment) { totalCommentedLines++; } if (lineText.Length > 0 && !isSingleLineComment) { realLines++; if ((typeLocations.ContainsKey(i)) && (typeLocations[i] is TypeDeclaration)) { i = EvaluateTypeLOC(ctx, namespaceRef, (TypeDeclaration)typeLocations[i], i); } if ((typeLocations.ContainsKey(i + 1)) && (typeLocations[i + 1] is TypeDeclaration)) { i = EvaluateTypeLOC(ctx, namespaceRef, (TypeDeclaration)typeLocations[i + 1], i); } } } totalLines += (ulong)(startIndex - endIndex + 1); totalRealLines += (ulong)realLines; namespaceRef.LOCReal += totalRealLines; namespaceRef.LOCComments += totalCommentedLines; #endregion CommonLogic } catch (Exception e) { Console.WriteLine(e.ToString()); } }
internal static int EvaluateTypeLOC(MetricsContext ctx, NamespaceProperties namespaceRef, TypeDeclaration node, int startIndex) { if (node == null) { return(-1); } StringBuilder typeName = new StringBuilder("");; try { string[] prefixArray = ComplexityMetrics.PrefixName.ToArray(); for (int i = 0; i < prefixArray.Length; i++) { typeName.Append(prefixArray[prefixArray.Length - i - 1] + "."); } typeName.Append(node.Name); foreach (var templateDef in node.Templates) { foreach (var bases in templateDef.Bases) { if (bases.Type.Contains("constraint:")) { continue; } typeName.Append(" " + bases.Type.Substring(bases.Type.LastIndexOf(".") + 1)); } } IProperties typeRef = null; switch (node.Type) { case ICSharpCode.OldNRefactory.Ast.ClassType.Class: typeRef = ComplexityMetrics.ProjProp.GetClassReference(typeName.ToString()); break; case ICSharpCode.OldNRefactory.Ast.ClassType.Enum: typeRef = ComplexityMetrics.ProjProp.GetEnumReference(typeName.ToString(), namespaceRef); break; case ICSharpCode.OldNRefactory.Ast.ClassType.Struct: typeRef = ComplexityMetrics.ProjProp.GetStructReference(typeName.ToString(), namespaceRef); break; case ICSharpCode.OldNRefactory.Ast.ClassType.Interface: typeRef = ComplexityMetrics.ProjProp.GetInterfaceReference(typeName.ToString(), namespaceRef); break; default: return(node.EndLocation.Line); } if (typeRef == null) { return(node.EndLocation.Line); } Dictionary <int, ICSharpCode.OldNRefactory.Ast.INode> childLocations = new Dictionary <int, ICSharpCode.OldNRefactory.Ast.INode>(0); foreach (ICSharpCode.OldNRefactory.Ast.INode childNode in node.Children) { if ((childNode is TypeDeclaration) || (childNode is ConstructorDeclaration) || (childNode is MethodDeclaration)) { childLocations.Add(childNode.StartLocation.Line, childNode); } } if (typeRef.FilePath == null || typeRef.FilePath == "") { typeRef.FilePath = ComplexityMetrics.File.FilePath; } startIndex = node.StartLocation.Line; int endIndex = node.EndLocation.Line; ulong totalLines = 0, totalRealLines = 0, totalCommentedLines = 0; int realLines = 0; bool isSingleLineComment = false; bool isMultipleLineComment = false; for (int i = startIndex; i < endIndex; i++) { string lineText = ComplexityMetrics.FileDoc.GetTextAt(ComplexityMetrics.FileText[i]).Trim(); if (isMultipleLineComment) { totalCommentedLines++; if (lineText.EndsWith("*/")) { isMultipleLineComment = false; } continue; } if (lineText.StartsWith("/*")) { isMultipleLineComment = true; totalCommentedLines++; continue; } isSingleLineComment = lineText.StartsWith("//"); if (isSingleLineComment) { totalCommentedLines++; } if (lineText.Length > 0 && !isSingleLineComment) { realLines++; if (childLocations.ContainsKey(i)) { ComplexityMetrics.PrefixName.Push(node.Name); if ((childLocations[i] is MethodDeclaration) || (childLocations[i] is ConstructorDeclaration)) { ComplexityMetrics.ProcessMethod(ctx, childLocations[i], typeRef); i = childLocations[i].EndLocation.Line; } else if (childLocations[i] is TypeDeclaration) { i = EvaluateTypeLOC(ctx, namespaceRef, (TypeDeclaration)childLocations[i], i); } ComplexityMetrics.PrefixName.Pop(); } } } totalLines += (ulong)(endIndex - startIndex + 2); totalRealLines += (ulong)realLines; if (typeRef is ClassProperties) { ((ClassProperties)typeRef).LOCReal += totalRealLines; ((ClassProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is InterfaceProperties) { ((InterfaceProperties)typeRef).LOCReal += totalRealLines; ((InterfaceProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is EnumProperties) { ((EnumProperties)typeRef).LOCReal += totalRealLines; ((EnumProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is StructProperties) { ((StructProperties)typeRef).LOCReal += totalRealLines; ((StructProperties)typeRef).LOCComments += totalCommentedLines; } else if (typeRef is DelegateProperties) { ((DelegateProperties)typeRef).LOCReal += totalRealLines; ((DelegateProperties)typeRef).LOCComments += totalCommentedLines; } } catch (Exception e) { Console.WriteLine("Error in class " + typeName.ToString()); Console.WriteLine(e.ToString()); } return(node.EndLocation.Line); }
private static void ProcessMethod (MetricsContext ctx, ICSharpCode.NRefactory.Ast.INode method, IProperties parentClass) { if(method==null) return; StringBuilder methodName = new StringBuilder(""); string[] PrefixArray = PrefixName.ToArray(); for(int i=0;i<PrefixArray.Length;i++) methodName.Append(PrefixArray[PrefixArray.Length-i-1]+"."); List<string> methodParameterList = new List<string>(0); if(method is MethodDeclaration) { methodName.Append(((MethodDeclaration)method).Name); foreach(ParameterDeclarationExpression pde in ((MethodDeclaration)method).Parameters) { string type = pde.TypeReference.Type; if(type.Contains(".")) type = type.Substring(type.LastIndexOf(".")+1); methodParameterList.Add (type); } } else if(method is ConstructorDeclaration) { methodName.Append(((ConstructorDeclaration)method).Name); foreach(ParameterDeclarationExpression pde in ((ConstructorDeclaration)method).Parameters) { string type = pde.TypeReference.Type; if(type.Contains(".")) type = type.Substring(type.LastIndexOf(".")+1); methodParameterList.Add (type); } } StringBuilder MethodKey = new StringBuilder(); MethodKey.Append(methodName.ToString()+" "); foreach(string paramName in methodParameterList) MethodKey.Append(paramName+" "); try{ if(parentClass is ClassProperties) { if(!(parentClass as ClassProperties).Methods.ContainsKey(MethodKey.ToString())) { if(method is MethodDeclaration) (parentClass as ClassProperties).Methods.Add(MethodKey.ToString(), new MethodProperties((MethodDeclaration)method, parentClass as ClassProperties)); else if (method is ConstructorDeclaration) (parentClass as ClassProperties).Methods.Add(MethodKey.ToString(), new MethodProperties((ConstructorDeclaration)method, parentClass as ClassProperties)); } var currentMethodReference = (parentClass as ClassProperties).Methods[MethodKey.ToString()]; //Calculate all metrics here ASTVisitor.EvaluateComplexityMetrics (method, currentMethodReference); LOCEvaluate.EvaluateMethodLOC(currentMethodReference, FileText, FileDoc); currentMethodReference.FilePath = File.FilePath; } } catch (NullReferenceException ex){ LoggingService.LogError ("Error in '" + methodName.ToString() + "'", ex); Console.WriteLine(MethodKey.ToString()+" hoo"); } }
private static void ProcessNode (MetricsContext ctx, ICSharpCode.NRefactory.Ast.INode node) { if(node is UsingStatement) { //TODO do something (something to do with afferent and efferent coupling of namespaces) } else if (node is NamespaceDeclaration) { try { PrefixName.Push(((NamespaceDeclaration)node).Name); LOCEvaluate.EvaluateNamespaceLOC(ctx, (NamespaceDeclaration)node); PrefixName.Pop(); } catch (Exception e) { } } else if (node is TypeDeclaration) { LOCEvaluate.EvaluateTypeLOC(ctx, null, (TypeDeclaration)node, node.StartLocation.Line); } }