Example #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="FullName">
 /// A <see cref="System.String"/>
 /// </param>
 /// <param name="namesp">
 /// A <see cref="NamespaceProperties"/>
 /// </param>
 /// <returns>
 /// A <see cref="StructProperties"/>
 /// </returns>
 internal StructProperties GetStructReference(string FullName, NamespaceProperties namesp)
 {
     if (namesp == null)
     {
         if (this.Structs.ContainsKey(FullName))
         {
             return(this.Structs[FullName]);
         }
     }
     if (namesp.Structs.ContainsKey(FullName))
     {
         return(namesp.Structs[FullName]);
     }
     foreach (var cls in namesp.Classes)
     {
         if (cls.Value.InnerStructs.ContainsKey(FullName))
         {
             return(cls.Value.InnerStructs[FullName]);
         }
         var ret = RecursiveGetStructReference(cls.Value, FullName);
         if (ret != null)
         {
             return(ret);
         }
     }
     return(null);
 }
Example #2
0
        private static StringBuilder GenerateNamespaceMetricText(NamespaceProperties item)
        {
            StringBuilder results = new StringBuilder();

            results.Append(GettextCatalog.GetString("\nName : ") + item.FullName);
            results.Append(GettextCatalog.GetString("\nTotal number of classes : ") + item.Classes.Count);
            results.Append(GettextCatalog.GetString("\nTotal number of methods : ") + item.MethodCount);
            results.Append(GettextCatalog.GetString("\nTotal number of fields : ") + item.FieldCount);
            results.Append(GettextCatalog.GetString("\nClass Coupling : ") + item.ClassCoupling);
            return(results);
        }
			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;
			}
Example #4
0
            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());
                }
            }
Example #5
0
            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);
            }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="FullName">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="namesp">
		/// A <see cref="NamespaceProperties"/>
		/// </param>
		/// <returns>
		/// A <see cref="StructProperties"/>
		/// </returns>
		internal StructProperties GetStructReference (string FullName, NamespaceProperties namesp)
		{
			if(namesp == null)
				if(this.Structs.ContainsKey(FullName))
					return this.Structs[FullName];
			if(namesp.Structs.ContainsKey(FullName))
				return namesp.Structs[FullName];
			foreach(var cls in namesp.Classes)
			{
				if(cls.Value.InnerStructs.ContainsKey(FullName))
					return cls.Value.InnerStructs[FullName];
				var ret = RecursiveGetStructReference(cls.Value, FullName);
				if(ret!=null)
					return ret;
			}
			return null;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="key">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="namesp">
		/// A <see cref="NamespaceProperties"/>
		/// </param>
		/// <returns>
		/// A <see cref="EnumProperties"/>
		/// </returns>
		internal EnumProperties GetEnumReference (string FullName, NamespaceProperties namesp)
		{
			if(namesp == null) {
				if(this.Enums.ContainsKey(FullName)) {
					return this.Enums[FullName];
				} else {
					return null;
				}
			}
			if(namesp.Enums.ContainsKey(FullName))
				return namesp.Enums[FullName];
			foreach(var cls in namesp.Classes)
			{
				if(cls.Value.InnerEnums.ContainsKey(FullName))
					return cls.Value.InnerEnums[FullName];
				var ret = RecursiveGetEnumReference(cls.Value, FullName);
				if(ret!=null)
					return ret;
			}
			return null;
		}
		private static StringBuilder GenerateNamespaceMetricText(NamespaceProperties item)
		{
			StringBuilder results = new StringBuilder();
			results.Append(GettextCatalog.GetString("\nName : ") + item.FullName);
			results.Append(GettextCatalog.GetString("\nTotal number of classes : ") + item.Classes.Count);
			results.Append(GettextCatalog.GetString("\nTotal number of methods : ") + item.MethodCount);
			results.Append(GettextCatalog.GetString("\nTotal number of fields : ") + item.FieldCount);
			results.Append(GettextCatalog.GetString("\nClass Coupling : ") + item.ClassCoupling);
			return results;
		}