Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C#

Static Code Analysis

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
15 Mar 2010CPOL16 min read 87.1K   1.1K   63  
A static code analyzer building method call networks + sample applications
This article describes the operation of a method-based static code analyzer for .NET that constructs in-memory method call networks of compiled assemblies. You will also see a concrete application of static code analysis to generate a website providing easy insights on a sample application.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Reflection;

namespace Arebis.CodeAnalysis.Static.Processors.Rules
{
	[CodeModelMatchingRule(
        "namerule", 
        "Matches methods by their name, their types name or their assemblies name.",
        "target=Target of the name.|like=Wildcard expression the name should match.|match=Regular expression the name should match.")]
	public class NameMatchingRule : BaseMatchingRule
	{
		private RuleTarget target;
		private Regex match;

		public override void Initialize(XmlNode ruleInstance)
		{
			this.target = (RuleTarget)Enum.Parse(typeof(RuleTarget), ruleInstance.Attributes["target"].Value, true);
            if (ruleInstance.Attributes["match"] != null)
                this.match = new Regex(ruleInstance.Attributes["match"].Value, RegexOptions.Compiled);
            else if (ruleInstance.Attributes["like"] != null)
                this.match = GetLikeRegex(ruleInstance.Attributes["like"].Value, RegexOptions.Compiled);
            else
                throw new InvalidOperationException(String.Format("Name matching rule must have either match or like attribute in \"{0}\".", ruleInstance.OuterXml));
		}

        /// <summary>
        /// Returns a RegEx to match the given pattern with wildcards * and ?.
        /// </summary>
        public static Regex GetLikeRegex(string pattern, RegexOptions options)
        {
            pattern = Regex.Escape(pattern);
            pattern = "^" + pattern.Replace("\\*", ".*").Replace("\\?", ".") + "$";
            return new Regex(pattern, options);
        }

		public override bool Matches(ModelMethod method)
		{
			switch (this.target)
			{
				case RuleTarget.Method:
					return this.MatchesOnMethod(method);
				case RuleTarget.Type:
					return this.MatchesOnType(method.DeclaringType);
				case RuleTarget.Assembly:
					return this.MatchesOnAssembly(method.DeclaringType.Assembly);
				default:
					throw new InvalidOperationException(String.Format("Invalid target '{0}' for {1}.", this.target, this.GetType().Name));
			}
		}

		public bool MatchesOnMethod(ModelMethod method)
		{
			return (this.match.IsMatch(method.Name));
		}

		public bool MatchesOnType(Type type)
		{
			return ((type.FullName != null) && (this.match.IsMatch(type.FullName)));
		}

		public bool MatchesOnAssembly(Assembly assembly)
		{
			return (this.match.IsMatch(assembly.GetName().Name));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AREBIS
Belgium Belgium
Senior Software Architect and independent consultant.

Comments and Discussions